( atom: Atom<T>, predicate: (value: T) => boolean, )
| 14 | * Wait for an atom to satisfy a predicate. |
| 15 | */ |
| 16 | export async function waitFor<T>( |
| 17 | atom: Atom<T>, |
| 18 | predicate: (value: T) => boolean, |
| 19 | ) { |
| 20 | if (predicate(store.get(atom))) { |
| 21 | return store.get(atom); |
| 22 | } |
| 23 | |
| 24 | return new Promise<T>((resolve) => { |
| 25 | const unsubscribe = store.sub(atom, () => { |
| 26 | const value = store.get(atom); |
| 27 | if (predicate(value)) { |
| 28 | unsubscribe(); |
| 29 | resolve(value); |
| 30 | } |
| 31 | }); |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Create a Jotai effect that runs when an atom changes. |
no test coverage detected
searching dependent graphs…