@gershy/util-throttler 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,10 @@
1
+ import '../sideEffects.js';
2
+ import '@gershy/clearing';
3
+ export default class Throttler {
4
+ private limit;
5
+ private active;
6
+ private pending;
7
+ constructor(limit: number);
8
+ private finish;
9
+ do<T>(fn: () => Promise<T>): Promise<T>;
10
+ }
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ require("@gershy/clearing");
4
+ // TODO: @gershy/throttler
5
+ class Throttler {
6
+ limit;
7
+ active;
8
+ pending;
9
+ constructor(limit) {
10
+ if (limit < 1)
11
+ throw Error('limit too low')[mod]({ limit });
12
+ if (Math.floor(limit) !== limit)
13
+ throw Error('limit invalid')[mod]({ limit });
14
+ this.limit = limit;
15
+ this.active = new Set();
16
+ this.pending = [];
17
+ }
18
+ finish(donePrm) {
19
+ this.active[rem](donePrm);
20
+ const next = this.pending.shift();
21
+ if (!next)
22
+ return;
23
+ // Safely run `fn` - wire resolve+reject to the `Promise.later` value
24
+ const { prm, fn } = next;
25
+ this.do(fn).then(val => prm.resolve(val), err => prm.reject(err));
26
+ }
27
+ do(fn) {
28
+ if (this.active.size < this.limit) {
29
+ const prm = fn(); // Synchronous throws will propagate!
30
+ this.active.add(prm);
31
+ return prm.finally(() => this.finish(prm));
32
+ }
33
+ const prm = Promise[later]();
34
+ this.pending.push({ prm, fn });
35
+ return prm;
36
+ }
37
+ }
38
+ exports.default = Throttler;
@@ -0,0 +1 @@
1
+ {"type":"commonjs"}
@@ -0,0 +1,10 @@
1
+ import '../sideEffects.js';
2
+ import '@gershy/clearing';
3
+ export default class Throttler {
4
+ private limit;
5
+ private active;
6
+ private pending;
7
+ constructor(limit: number);
8
+ private finish;
9
+ do<T>(fn: () => Promise<T>): Promise<T>;
10
+ }
@@ -0,0 +1,35 @@
1
+ import '@gershy/clearing';
2
+ // TODO: @gershy/throttler
3
+ export default class Throttler {
4
+ limit;
5
+ active;
6
+ pending;
7
+ constructor(limit) {
8
+ if (limit < 1)
9
+ throw Error('limit too low')[mod]({ limit });
10
+ if (Math.floor(limit) !== limit)
11
+ throw Error('limit invalid')[mod]({ limit });
12
+ this.limit = limit;
13
+ this.active = new Set();
14
+ this.pending = [];
15
+ }
16
+ finish(donePrm) {
17
+ this.active[rem](donePrm);
18
+ const next = this.pending.shift();
19
+ if (!next)
20
+ return;
21
+ // Safely run `fn` - wire resolve+reject to the `Promise.later` value
22
+ const { prm, fn } = next;
23
+ this.do(fn).then(val => prm.resolve(val), err => prm.reject(err));
24
+ }
25
+ do(fn) {
26
+ if (this.active.size < this.limit) {
27
+ const prm = fn(); // Synchronous throws will propagate!
28
+ this.active.add(prm);
29
+ return prm.finally(() => this.finish(prm));
30
+ }
31
+ const prm = Promise[later]();
32
+ this.pending.push({ prm, fn });
33
+ return prm;
34
+ }
35
+ }
@@ -0,0 +1 @@
1
+ {"type":"module"}
@@ -0,0 +1,2 @@
1
+ declare global {}
2
+ export {};
package/license ADDED
@@ -0,0 +1,15 @@
1
+ ISC License
2
+
3
+ Copyright (c) 2026 Gershom Maes
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
12
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
14
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15
+ PERFORMANCE OF THIS SOFTWARE.
package/package.json ADDED
@@ -0,0 +1 @@
1
+ {"name":"@gershy/util-throttler","version":"0.0.1","description":"TODO","keywords":["TODO"],"author":"Gershom Maes","repository":{"type":"git","url":"git+https://github.com/gershy/utilThrottler.git"},"bugs":{"url":"https://github.com/gershy/utilThrottler/issues"},"homepage":"https://github.com/gershy/utilThrottler#readme","license":"ISC","peerDependencies":{"@gershy/clearing":"^0.0.17"},"devDependencies":{"@types/node":"^24.10.1","tsx":"^4.21.0","typescript":"^5.9.3"},"type":"module","files":["cmp"],"sideEffects":false,"types":"./cmp/mjs/main.d.ts","exports":{".":{"import":"./cmp/mjs/main.js","require":"./cmp/cjs/main.js"}},"scripts":{"test":"npm run ts.check && npx tsx ./src/main.test.ts","ts.check":"npx tsc --noEmit","build.cjs":"tsc -p build/tsconfig.cjs.json","build.mjs":"tsc -p build/tsconfig.mjs.json","build":"node ./build/act.js removeCmp && npm run build.cjs && npm run build.mjs && node ./build/act.js finalizeExportVariants","git.pub":"npm run test && git add --all && git commit -m \"automated\" && git push","npm.login":"npm login","npm.pub":"npm run test && npm run build && npm publish --access public"}}
package/readme.md ADDED
@@ -0,0 +1,3 @@
1
+ # TODO
2
+
3
+ Looks like someone needs to write this readme...