Add global thunks store, add publishThunks

This commit is contained in:
Jon Staab
2024-11-12 15:48:00 -08:00
parent ea7f38789b
commit 15513983e4
+23
View File
@@ -101,6 +101,8 @@ export const mergeThunks = (thunks: Thunk[]) => {
}
}
export const thunks = writable<Record<string, Thunk | MergedThunk>>({})
export const publishThunk = (request: ThunkRequest) => {
const thunk = makeThunk(request)
@@ -108,6 +110,8 @@ export const publishThunk = (request: ThunkRequest) => {
repository.publish(thunk.event)
thunks.update(assoc(thunk.event.id, thunk))
thunk.controller.signal.addEventListener('abort', () => {
repository.removeEvent(thunk.event.id)
})
@@ -115,6 +119,25 @@ export const publishThunk = (request: ThunkRequest) => {
return thunk
}
export const publishThunks = (requests: ThunkRequest[]) => {
const newThunks = requests.map(makeThunk)
const mergedThunk = mergeThunks(newThunks)
for (const thunk of newThunks) {
thunkWorker.push(thunk)
repository.publish(thunk.event)
thunks.update(assoc(thunk.event.id, mergedThunk))
thunk.controller.signal.addEventListener('abort', () => {
repository.removeEvent(thunk.event.id)
})
}
return mergedThunk
}
export const thunkWorker = new Worker<Thunk>()
thunkWorker.addGlobalHandler((thunk: Thunk) => {