mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 02:45:07 +03:00
22 lines
581 B
TypeScript
22 lines
581 B
TypeScript
import type { Atom, AtomRegistry } from 'effect/unstable/reactivity'
|
|
|
|
/** Resolves once `atom`'s value in `registry` satisfies `predicate`. */
|
|
export const waitFor = <A>(
|
|
registry: AtomRegistry.AtomRegistry,
|
|
atom: Atom.Atom<A>,
|
|
predicate: (value: A) => boolean
|
|
): Promise<A> =>
|
|
new Promise((resolve) => {
|
|
const current = registry.get(atom)
|
|
if (predicate(current)) {
|
|
resolve(current)
|
|
return
|
|
}
|
|
const unsubscribe = registry.subscribe(atom, (value) => {
|
|
if (predicate(value)) {
|
|
unsubscribe()
|
|
resolve(value)
|
|
}
|
|
})
|
|
})
|