Make thunks an array instead of an object
This commit is contained in:
@@ -260,6 +260,12 @@ export declare const flatten: <T>(xs: T[][]) => T[];
|
||||
// Splits array into two arrays based on predicate
|
||||
export declare const partition: <T>(f: (x: T) => boolean, xs: T[]) => T[][];
|
||||
|
||||
// A function that filters an array, keeping items that pass the predicate
|
||||
export declare const filter: <T>(f: (x: T) => any, xs: T[]) => T[];
|
||||
|
||||
// A function that filters an array, removing items that pass the predicate
|
||||
export declare const reject: <T>(f: (x: T) => any, xs: T[]) => T[];
|
||||
|
||||
// Returns array with duplicate elements removed
|
||||
export declare const uniq: <T>(xs: T[]) => T[];
|
||||
|
||||
|
||||
@@ -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})
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user