Add a few thunk utils

This commit is contained in:
Jon Staab
2024-12-10 08:21:14 -08:00
parent caccc43f03
commit 9ab8b70bb9
2 changed files with 21 additions and 0 deletions
+14
View File
@@ -66,6 +66,9 @@ export type MergedThunk = {
status: Readable<ThunkStatusByUrl>
}
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<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 publishThunk = (request: ThunkRequest) => {
@@ -208,3 +221,4 @@ thunkWorker.addGlobalHandler((thunk: Thunk) => {
})
})
})