diff --git a/docs/lib/tools.md b/docs/lib/tools.md index 2e7e85a..ad03c27 100644 --- a/docs/lib/tools.md +++ b/docs/lib/tools.md @@ -260,6 +260,12 @@ export declare const flatten: (xs: T[][]) => T[]; // Splits array into two arrays based on predicate export declare const partition: (f: (x: T) => boolean, xs: T[]) => T[][]; +// A function that filters an array, keeping items that pass the predicate +export declare const filter: (f: (x: T) => any, xs: T[]) => T[]; + +// A function that filters an array, removing items that pass the predicate +export declare const reject: (f: (x: T) => any, xs: T[]) => T[]; + // Returns array with duplicate elements removed export declare const uniq: (xs: T[]) => T[]; diff --git a/packages/app/src/commands.ts b/packages/app/src/commands.ts index 3f4a440..0e8b504 100644 --- a/packages/app/src/commands.ts +++ b/packages/app/src/commands.ts @@ -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}) + }), ), ) } diff --git a/packages/app/src/thunk.ts b/packages/app/src/thunk.ts index c6a6d61..5d5415c 100644 --- a/packages/app/src/thunk.ts +++ b/packages/app/src/thunk.ts @@ -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>({}) +export const thunks = writable([]) export const thunkQueue = new TaskQueue({ 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)) } } diff --git a/packages/lib/src/Tools.ts b/packages/lib/src/Tools.ts index 696f6d9..5b0b96c 100644 --- a/packages/lib/src/Tools.ts +++ b/packages/lib/src/Tools.ts @@ -568,6 +568,22 @@ export const partition = (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 = (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 = (f: (x: T) => any, xs: T[]) => xs.filter(complement(f)) + /** * Returns array with duplicate elements removed * @param xs - Array with possible duplicates