mirror of
https://github.com/linsa-io/linsa.git
synced 2026-01-12 12:20:23 +01:00
chore: subscribe co value
This commit is contained in:
@@ -74,3 +74,4 @@ export * from "./force-graph"
|
||||
export * from "./env"
|
||||
export * from "./slug"
|
||||
export * from "./url"
|
||||
export * from "./jazz"
|
||||
|
||||
39
web/app/lib/utils/jazz.ts
Normal file
39
web/app/lib/utils/jazz.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
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()
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user