handy online tools for AT Protocol boat.kelinci.net
atproto bluesky atcute typescript solidjs

refactor: touch up on promise queue

mary.my.id fb68e414 3559ea9f

verified
+1
package.json
··· 20 20 "@atcute/tid": "^1.0.2", 21 21 "@badrap/valita": "^0.4.4", 22 22 "@mary/array-fns": "npm:@jsr/mary__array-fns@^0.1.4", 23 + "@mary/ds-queue": "npm:@jsr/mary__ds-queue@^0.1.1", 23 24 "@mary/events": "npm:@jsr/mary__events@^0.2.0", 24 25 "@mary/solid-freeze": "npm:@externdefs/solid-freeze@^0.1.1", 25 26 "@mary/tar": "npm:@jsr/mary__tar@^0.2.4",
+8
pnpm-lock.yaml
··· 47 47 '@mary/array-fns': 48 48 specifier: npm:@jsr/mary__array-fns@^0.1.4 49 49 version: '@jsr/mary__array-fns@0.1.4' 50 + '@mary/ds-queue': 51 + specifier: npm:@jsr/mary__ds-queue@^0.1.1 52 + version: '@jsr/mary__ds-queue@0.1.1' 50 53 '@mary/events': 51 54 specifier: npm:@jsr/mary__events@^0.2.0 52 55 version: '@jsr/mary__events@0.2.0' ··· 581 584 582 585 '@jsr/mary__array-fns@0.1.4': 583 586 resolution: {integrity: sha512-+HbGYR9Ll5blEmAvVAoPejyGj01YeBbVmJ59qxaMDKt5i3F90ohYLA5a78y6AULDlet1IxYB+a/cMN+A0vGnDg==, tarball: https://npm.jsr.io/~/11/@jsr/mary__array-fns/0.1.4.tgz} 587 + 588 + '@jsr/mary__ds-queue@0.1.1': 589 + resolution: {integrity: sha512-eypx2HpssmbNM6e3mWUs0ZzyNrt9i2vhWKQtNduu+02AZuVT+1QZIm3doyxENC3cyScFppWPciatfG2rZxBOxQ==, tarball: https://npm.jsr.io/~/11/@jsr/mary__ds-queue/0.1.1.tgz} 584 590 585 591 '@jsr/mary__events@0.2.0': 586 592 resolution: {integrity: sha512-WcBRbtuTno3zcfXKd7SEeKr1lAJF+CQ8BCv+PEEMmNKNqFurkEksGxRB3UDPZxIxjJ7sAqMVTL26wRuMpAcIeA==, tarball: https://npm.jsr.io/~/11/@jsr/mary__events/0.2.0.tgz} ··· 1993 1999 '@jridgewell/sourcemap-codec': 1.5.0 1994 2000 1995 2001 '@jsr/mary__array-fns@0.1.4': {} 2002 + 2003 + '@jsr/mary__ds-queue@0.1.1': {} 1996 2004 1997 2005 '@jsr/mary__events@0.2.0': {} 1998 2006
+18 -6
src/lib/utils/promise-queue.ts
··· 1 + import Queue from '@mary/ds-queue'; 2 + 3 + interface QueueTask { 4 + deferred: PromiseWithResolvers<any>; 5 + fn: () => any; 6 + } 7 + 1 8 export class PromiseQueue { 2 - #queue: { deferred: PromiseWithResolvers<any>; fn: () => any }[] = []; 9 + #queue = new Queue<QueueTask>(); 3 10 4 11 #max: number; 5 12 #current = 0; ··· 11 18 add<T>(fn: () => Promise<T>): Promise<T> { 12 19 const deferred = Promise.withResolvers<T>(); 13 20 14 - this.#queue.push({ deferred, fn }); 21 + this.#queue.enqueue({ deferred, fn }); 15 22 this.#run(); 16 23 17 24 return deferred.promise; 18 25 } 19 26 20 27 async flush(): Promise<void> { 21 - while (this.#queue.length > 0) { 22 - await Promise.all(this.#queue.map((task) => task.deferred.promise)); 28 + while (this.#queue.size > 0) { 29 + // type assertion here because JSR omits the [Symbol.iterator] method declaration 30 + await Promise.all( 31 + Array.from(this.#queue as any as Iterable<QueueTask>, (task) => task.deferred.promise), 32 + ); 23 33 } 24 34 } 25 35 26 36 #run() { 27 - if (this.#queue.length > 0 && this.#current <= this.#max) { 28 - const { deferred, fn } = this.#queue.shift()!; 37 + let task: QueueTask | undefined; 38 + 39 + if (this.#current <= this.#max && (task = this.#queue.dequeue()) !== undefined) { 40 + const { deferred, fn } = task; 29 41 this.#current++; 30 42 31 43 const promise = new Promise((r) => r(fn()));
+2 -1
tsconfig.app.json
··· 1 1 { 2 2 "compilerOptions": { 3 3 "target": "ESNext", 4 - "useDefineForClassFields": false, 5 4 "module": "ESNext", 6 5 "lib": ["ESNext", "DOM", "DOM.Iterable"], 7 6 "types": [], ··· 12 11 "isolatedModules": true, 13 12 "moduleDetection": "force", 14 13 "noEmit": true, 14 + 15 15 "jsx": "preserve", 16 16 "jsxImportSource": "solid-js", 17 + "useDefineForClassFields": false, 17 18 18 19 "strict": true, 19 20 "noUnusedLocals": true,