Make thunks an array instead of an object

This commit is contained in:
Jon Staab
2025-10-17 10:32:08 -07:00
parent 642a4ce517
commit 543dbda64f
4 changed files with 34 additions and 12 deletions
+6 -7
View File
@@ -214,13 +214,12 @@ export const sendWrapped = async ({template, pubkeys, ...options}: SendWrappedOp
return new MergedThunk(
await Promise.all(
uniq(pubkeys).map(async recipient =>
publishThunk({
event: await nip59.wrap(recipient, stamp(template)),
relays: Router.get().PubkeyInbox(recipient).getUrls(),
...options,
}),
),
uniq(pubkeys).map(async recipient => {
const event = await nip59.wrap(recipient, stamp(template))
const relays = Router.get().PubkeyInbox(recipient).getUrls()
return publishThunk({event, relays, ...options})
}),
),
)
}
+6 -5
View File
@@ -1,14 +1,15 @@
import type {Subscriber} from "svelte/store"
import {writable, get} from "svelte/store"
import {
append,
reject,
spec,
TaskQueue,
ifLet,
ensurePlural,
dissoc,
remove,
defer,
sleep,
assoc,
nth,
without,
} from "@welshman/lib"
@@ -337,7 +338,7 @@ export const waitForThunkCompletion = (thunk: Thunk) =>
// Thunk state
export const thunks = writable<Record<string, AbstractThunk>>({})
export const thunks = writable<Thunk[]>([])
export const thunkQueue = new TaskQueue<Thunk>({
batchSize: 50,
@@ -368,7 +369,7 @@ export const publishThunk = (options: ThunkOptions) => {
repository.publish(thunk.event)
thunks.update(assoc(thunk.event.id, thunk))
thunks.update($thunks => append(thunk, $thunks))
return thunk
}
@@ -376,8 +377,8 @@ export const publishThunk = (options: ThunkOptions) => {
export const abortThunk = (thunk: AbstractThunk) => {
for (const child of flattenThunks([thunk])) {
child.controller.abort()
thunks.update(dissoc(child.event.id))
repository.removeEvent(child.event.id)
thunks.update($thunks => reject(spec({id: child.event.id}), $thunks))
}
}
+16
View File
@@ -568,6 +568,22 @@ export const partition = <T>(f: (x: T) => boolean, xs: T[]) => {
return [a, b]
}
/**
* Keeps items based on predicate
* @param f - Whether to remove an item from the array
* @param xs - Array of items to filter
* @returns Filtered array
*/
export const filter = <T>(f: (x: T) => any, xs: T[]) => xs.filter(f)
/**
* Removes items based on predicate
* @param f - Whether to remove an item from the array
* @param xs - Array of items to filter
* @returns Filtered array
*/
export const reject = <T>(f: (x: T) => any, xs: T[]) => xs.filter(complement(f))
/**
* Returns array with duplicate elements removed
* @param xs - Array with possible duplicates