mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
40 lines
718 B
TypeScript
40 lines
718 B
TypeScript
import {
|
|
Account,
|
|
CoValue,
|
|
CoValueClass,
|
|
DepthsIn,
|
|
ID,
|
|
subscribeToCoValue,
|
|
} from "jazz-tools"
|
|
|
|
export function waitForCoValue<T extends CoValue>(
|
|
coMap: CoValueClass<T>,
|
|
valueId: ID<T>,
|
|
account: Account,
|
|
predicate: (value: T) => boolean,
|
|
depth: DepthsIn<T>,
|
|
) {
|
|
return new Promise<T>((resolve) => {
|
|
function subscribe() {
|
|
const unsubscribe = subscribeToCoValue(
|
|
coMap,
|
|
valueId,
|
|
account,
|
|
depth,
|
|
(value) => {
|
|
if (predicate(value)) {
|
|
resolve(value)
|
|
unsubscribe()
|
|
}
|
|
},
|
|
() => {
|
|
unsubscribe()
|
|
setTimeout(subscribe, 100)
|
|
},
|
|
)
|
|
}
|
|
|
|
subscribe()
|
|
})
|
|
}
|