From 9ab8b70bb9e674ee93f448d76939a7566c27f07d Mon Sep 17 00:00:00 2001 From: Jon Staab Date: Tue, 10 Dec 2024 08:21:14 -0800 Subject: [PATCH] Add a few thunk utils --- packages/app/src/thunk.ts | 14 ++++++++++++++ packages/net/src/Tracker.ts | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/packages/app/src/thunk.ts b/packages/app/src/thunk.ts index 1c2e484..b0a8c15 100644 --- a/packages/app/src/thunk.ts +++ b/packages/app/src/thunk.ts @@ -66,6 +66,9 @@ export type MergedThunk = { status: Readable } +export const isMergedThunk = (thunk: Thunk | MergedThunk): thunk is MergedThunk => + Boolean((thunk as any).thunks) + export const mergeThunks = (thunks: Thunk[]) => { const controller = new AbortController() @@ -101,6 +104,16 @@ export const mergeThunks = (thunks: Thunk[]) => { } } +export function* walkThunks(thunks: (Thunk | MergedThunk)[]): Iterable { + for (const thunk of thunks) { + if (isMergedThunk(thunk)) { + yield* walkThunks(thunk.thunks) + } else { + yield thunk + } + } +} + export const thunks = writable>({}) export const publishThunk = (request: ThunkRequest) => { @@ -208,3 +221,4 @@ thunkWorker.addGlobalHandler((thunk: Thunk) => { }) }) }) + diff --git a/packages/net/src/Tracker.ts b/packages/net/src/Tracker.ts index 5df36c1..0294ba0 100644 --- a/packages/net/src/Tracker.ts +++ b/packages/net/src/Tracker.ts @@ -29,6 +29,13 @@ export class Tracker extends Emitter { 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) => { const seen = this.relaysById.has(eventId)