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
|
// Splits array into two arrays based on predicate
|
||||||
export declare const partition: <T>(f: (x: T) => boolean, xs: T[]) => T[][];
|
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
|
// Returns array with duplicate elements removed
|
||||||
export declare const uniq: <T>(xs: T[]) => T[];
|
export declare const uniq: <T>(xs: T[]) => T[];
|
||||||
|
|
||||||
|
|||||||
@@ -214,13 +214,12 @@ export const sendWrapped = async ({template, pubkeys, ...options}: SendWrappedOp
|
|||||||
|
|
||||||
return new MergedThunk(
|
return new MergedThunk(
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
uniq(pubkeys).map(async recipient =>
|
uniq(pubkeys).map(async recipient => {
|
||||||
publishThunk({
|
const event = await nip59.wrap(recipient, stamp(template))
|
||||||
event: await nip59.wrap(recipient, stamp(template)),
|
const relays = Router.get().PubkeyInbox(recipient).getUrls()
|
||||||
relays: Router.get().PubkeyInbox(recipient).getUrls(),
|
|
||||||
...options,
|
return publishThunk({event, relays, ...options})
|
||||||
}),
|
}),
|
||||||
),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,15 @@
|
|||||||
import type {Subscriber} from "svelte/store"
|
import type {Subscriber} from "svelte/store"
|
||||||
import {writable, get} from "svelte/store"
|
import {writable, get} from "svelte/store"
|
||||||
import {
|
import {
|
||||||
|
append,
|
||||||
|
reject,
|
||||||
|
spec,
|
||||||
TaskQueue,
|
TaskQueue,
|
||||||
ifLet,
|
ifLet,
|
||||||
ensurePlural,
|
ensurePlural,
|
||||||
dissoc,
|
|
||||||
remove,
|
remove,
|
||||||
defer,
|
defer,
|
||||||
sleep,
|
sleep,
|
||||||
assoc,
|
|
||||||
nth,
|
nth,
|
||||||
without,
|
without,
|
||||||
} from "@welshman/lib"
|
} from "@welshman/lib"
|
||||||
@@ -337,7 +338,7 @@ export const waitForThunkCompletion = (thunk: Thunk) =>
|
|||||||
|
|
||||||
// Thunk state
|
// Thunk state
|
||||||
|
|
||||||
export const thunks = writable<Record<string, AbstractThunk>>({})
|
export const thunks = writable<Thunk[]>([])
|
||||||
|
|
||||||
export const thunkQueue = new TaskQueue<Thunk>({
|
export const thunkQueue = new TaskQueue<Thunk>({
|
||||||
batchSize: 50,
|
batchSize: 50,
|
||||||
@@ -368,7 +369,7 @@ export const publishThunk = (options: ThunkOptions) => {
|
|||||||
|
|
||||||
repository.publish(thunk.event)
|
repository.publish(thunk.event)
|
||||||
|
|
||||||
thunks.update(assoc(thunk.event.id, thunk))
|
thunks.update($thunks => append(thunk, $thunks))
|
||||||
|
|
||||||
return thunk
|
return thunk
|
||||||
}
|
}
|
||||||
@@ -376,8 +377,8 @@ export const publishThunk = (options: ThunkOptions) => {
|
|||||||
export const abortThunk = (thunk: AbstractThunk) => {
|
export const abortThunk = (thunk: AbstractThunk) => {
|
||||||
for (const child of flattenThunks([thunk])) {
|
for (const child of flattenThunks([thunk])) {
|
||||||
child.controller.abort()
|
child.controller.abort()
|
||||||
thunks.update(dissoc(child.event.id))
|
|
||||||
repository.removeEvent(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]
|
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
|
* Returns array with duplicate elements removed
|
||||||
* @param xs - Array with possible duplicates
|
* @param xs - Array with possible duplicates
|
||||||
|
|||||||
Reference in New Issue
Block a user