Add global thunks store, add publishThunks
This commit is contained in:
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user