Improve feed loader, wait for db before executing reads/updates, make taskQueue subscribable, add race, rename auth methods, fix failed wasm verify, fix localhost urls

This commit is contained in:
Jon Staab
2025-04-09 15:39:07 -07:00
parent 1bcc57d695
commit 859f7fa68f
14 changed files with 155 additions and 32 deletions
+13
View File
@@ -6,6 +6,7 @@ export type TaskQueueOptions<Item> = {
}
export class TaskQueue<Item> {
_subs: ((item: Item) => void)[] = []
items: Item[] = []
isPaused = false
isProcessing = false
@@ -21,6 +22,14 @@ export class TaskQueue<Item> {
this.items = remove(item, this.items)
}
subscribe(subscriber: (item: Item) => void) {
this._subs.push(subscriber)
return () => {
this._subs = remove(subscriber, this._subs)
}
}
async process() {
if (this.isProcessing || this.isPaused || this.items.length === 0) {
return
@@ -32,6 +41,10 @@ export class TaskQueue<Item> {
for (const item of this.items.splice(0, this.options.batchSize)) {
try {
for (const subscriber of this._subs) {
subscriber(item)
}
await this.options.processItem(item)
} catch (e) {
console.error(e)
+26
View File
@@ -1038,6 +1038,32 @@ export const batcher = <T, U>(t: number, execute: (request: T[]) => U[] | Promis
})
}
/**
* Returns a promise that resolves after some proportion of promises complete
* @param threshold - number between 0 and 1 for how many promises to wait for
* @param promises - array of promises
* @returns promise
*/
export const race = (threshold: number, promises: Promise<unknown>[]) => {
let count = 0
if (threshold === 0) {
return Promise.resolve()
}
return new Promise<void>((resolve, reject) => {
promises.forEach(p => {
p.then(() => {
count++
if (count >= threshold * promises.length) {
resolve()
}
}).catch(reject)
})
})
}
// ----------------------------------------------------------------------------
// URLs
// ----------------------------------------------------------------------------