Add a few thunk utils
This commit is contained in:
@@ -66,6 +66,9 @@ export type MergedThunk = {
|
|||||||
status: Readable<ThunkStatusByUrl>
|
status: Readable<ThunkStatusByUrl>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const isMergedThunk = (thunk: Thunk | MergedThunk): thunk is MergedThunk =>
|
||||||
|
Boolean((thunk as any).thunks)
|
||||||
|
|
||||||
export const mergeThunks = (thunks: Thunk[]) => {
|
export const mergeThunks = (thunks: Thunk[]) => {
|
||||||
const controller = new AbortController()
|
const controller = new AbortController()
|
||||||
|
|
||||||
@@ -101,6 +104,16 @@ export const mergeThunks = (thunks: Thunk[]) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function* walkThunks(thunks: (Thunk | MergedThunk)[]): Iterable<Thunk> {
|
||||||
|
for (const thunk of thunks) {
|
||||||
|
if (isMergedThunk(thunk)) {
|
||||||
|
yield* walkThunks(thunk.thunks)
|
||||||
|
} else {
|
||||||
|
yield thunk
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const thunks = writable<Record<string, Thunk | MergedThunk>>({})
|
export const thunks = writable<Record<string, Thunk | MergedThunk>>({})
|
||||||
|
|
||||||
export const publishThunk = (request: ThunkRequest) => {
|
export const publishThunk = (request: ThunkRequest) => {
|
||||||
@@ -208,3 +221,4 @@ thunkWorker.addGlobalHandler((thunk: Thunk) => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,13 @@ export class Tracker extends Emitter {
|
|||||||
this.emit('update')
|
this.emit('update')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeRelay = (eventId: string, relay: string) => {
|
||||||
|
this.relaysById.get(eventId)?.delete(relay)
|
||||||
|
this.idsByRelay.get(relay)?.delete(eventId)
|
||||||
|
|
||||||
|
this.emit('update')
|
||||||
|
}
|
||||||
|
|
||||||
track = (eventId: string, relay: string) => {
|
track = (eventId: string, relay: string) => {
|
||||||
const seen = this.relaysById.has(eventId)
|
const seen = this.relaysById.has(eventId)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user