Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
/**
* An externally resolvable/rejectable "promise". Use it to resolve/reject
* promise at any time.
*
* ```ts
* const future = new Defer();
*
* future.promise.then(value => console.log(value));
*
* future.resolve(123);
* ```
*/
export declare class Defer<T> {
readonly resolve: (data: T) => void;
readonly reject: (error: any) => void;
readonly promise: Promise<T>;
}
+25
View File
@@ -0,0 +1,25 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Defer = void 0;
/**
* An externally resolvable/rejectable "promise". Use it to resolve/reject
* promise at any time.
*
* ```ts
* const future = new Defer();
*
* future.promise.then(value => console.log(value));
*
* future.resolve(123);
* ```
*/
class Defer {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
exports.Defer = Defer;
//# sourceMappingURL=Defer.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"Defer.js","sourceRoot":"","sources":["../../src/thingies/Defer.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;GAWG;AACH,MAAa,KAAK;IAAlB;QAGkB,YAAO,GAAe,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACtE,IAAY,CAAC,OAAO,GAAG,OAAO,CAAC;YAC/B,IAAY,CAAC,MAAM,GAAG,MAAM,CAAC;QAChC,CAAC,CAAC,CAAC;IACL,CAAC;CAAA;AAPD,sBAOC"}
+3
View File
@@ -0,0 +1,3 @@
import type { Code } from './types';
/** Limits concurrency of async code. */
export declare const concurrency: (limit: number) => <T = unknown>(code: Code<T>) => Promise<T>;
+42
View File
@@ -0,0 +1,42 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.concurrency = void 0;
const go_1 = require("./go");
class Task {
constructor(code) {
this.code = code;
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
});
}
}
/** Limits concurrency of async code. */
const concurrency = (limit) => {
let workers = 0;
const queue = new Set();
const work = async () => {
const task = queue.values().next().value;
if (task)
queue.delete(task);
else
return;
workers++;
try {
task.resolve(await task.code());
}
catch (error) {
task.reject(error);
}
finally {
workers--, queue.size && (0, go_1.go)(work);
}
};
return async (code) => {
const task = new Task(code);
queue.add(task);
return workers < limit && (0, go_1.go)(work), task.promise;
};
};
exports.concurrency = concurrency;
//# sourceMappingURL=concurrency.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"concurrency.js","sourceRoot":"","sources":["../../src/thingies/concurrency.ts"],"names":[],"mappings":";;;AAAA,6BAAwB;AAGxB,MAAM,IAAI;IAOR,YAA4B,IAAa;QAAb,SAAI,GAAJ,IAAI,CAAS;QAJzB,YAAO,GAAG,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YAC1D,IAAY,CAAC,OAAO,GAAG,OAAO,CAAC;YAC/B,IAAY,CAAC,MAAM,GAAG,MAAM,CAAC;QAChC,CAAC,CAAC,CAAC;IACyC,CAAC;CAC9C;AAED,wCAAwC;AACjC,MAAM,WAAW,GAAG,CAAC,KAAa,EAAE,EAAE;IAC3C,IAAI,OAAO,GAAG,CAAC,CAAC;IAChB,MAAM,KAAK,GAAG,IAAI,GAAG,EAAQ,CAAC;IAC9B,MAAM,IAAI,GAAG,KAAK,IAAI,EAAE;QACtB,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,CAAC,KAAK,CAAC;QACzC,IAAI,IAAI;YAAE,KAAK,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;;YACxB,OAAO;QACZ,OAAO,EAAE,CAAC;QACV,IAAI,CAAC;YACH,IAAI,CAAC,OAAO,CAAC,MAAM,IAAI,CAAC,IAAI,EAAE,CAAC,CAAC;QAClC,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;gBAAS,CAAC;YACT,OAAO,EAAE,EAAE,KAAK,CAAC,IAAI,IAAI,IAAA,OAAE,EAAC,IAAI,CAAC,CAAC;QACpC,CAAC;IACH,CAAC,CAAC;IACF,OAAO,KAAK,EAAe,IAAa,EAAc,EAAE;QACtD,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC;QAC5B,KAAK,CAAC,GAAG,CAAC,IAAqB,CAAC,CAAC;QACjC,OAAO,OAAO,GAAG,KAAK,IAAI,IAAA,OAAE,EAAC,IAAI,CAAC,EAAE,IAAI,CAAC,OAAO,CAAC;IACnD,CAAC,CAAC;AACJ,CAAC,CAAC;AArBW,QAAA,WAAW,eAqBtB"}
+3
View File
@@ -0,0 +1,3 @@
import type { Code } from './types';
/** Executes code concurrently. */
export declare const go: <T>(code: Code<T>) => void;
+9
View File
@@ -0,0 +1,9 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.go = void 0;
/** Executes code concurrently. */
const go = (code) => {
code().catch(() => { });
};
exports.go = go;
//# sourceMappingURL=go.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"go.js","sourceRoot":"","sources":["../../src/thingies/go.ts"],"names":[],"mappings":";;;AAEA,kCAAkC;AAC3B,MAAM,EAAE,GAAG,CAAI,IAAa,EAAQ,EAAE;IAC3C,IAAI,EAAE,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC;AACzB,CAAC,CAAC;AAFW,QAAA,EAAE,MAEb"}
+4
View File
@@ -0,0 +1,4 @@
export * from './concurrency';
export * from './Defer';
export * from './go';
export * from './of';
+8
View File
@@ -0,0 +1,8 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
tslib_1.__exportStar(require("./concurrency"), exports);
tslib_1.__exportStar(require("./Defer"), exports);
tslib_1.__exportStar(require("./go"), exports);
tslib_1.__exportStar(require("./of"), exports);
//# sourceMappingURL=index.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/thingies/index.ts"],"names":[],"mappings":";;;AAAA,wDAA8B;AAC9B,kDAAwB;AACxB,+CAAqB;AACrB,+CAAqB"}
+10
View File
@@ -0,0 +1,10 @@
/**
* Given a promise awaits it and returns a 3-tuple, with the following members:
*
* - First entry is either the resolved value of the promise or `undefined`.
* - Second entry is either the error thrown by promise or `undefined`.
* - Third entry is a boolean, truthy if promise was resolved and falsy if rejected.
*
* @param promise Promise to convert to 3-tuple.
*/
export declare const of: <T, E = unknown>(promise: Promise<T>) => Promise<[T | undefined, E | undefined, boolean]>;
+22
View File
@@ -0,0 +1,22 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.of = void 0;
/**
* Given a promise awaits it and returns a 3-tuple, with the following members:
*
* - First entry is either the resolved value of the promise or `undefined`.
* - Second entry is either the error thrown by promise or `undefined`.
* - Third entry is a boolean, truthy if promise was resolved and falsy if rejected.
*
* @param promise Promise to convert to 3-tuple.
*/
const of = async (promise) => {
try {
return [await promise, undefined, true];
}
catch (error) {
return [undefined, error, false];
}
};
exports.of = of;
//# sourceMappingURL=of.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"of.js","sourceRoot":"","sources":["../../src/thingies/of.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;GAQG;AACI,MAAM,EAAE,GAAG,KAAK,EAAkB,OAAmB,EAAoD,EAAE;IAChH,IAAI,CAAC;QACH,OAAO,CAAC,MAAM,OAAO,EAAE,SAAS,EAAE,IAAI,CAAC,CAAC;IAC1C,CAAC;IAAC,OAAO,KAAc,EAAE,CAAC;QACxB,OAAO,CAAC,SAAS,EAAE,KAAU,EAAE,KAAK,CAAC,CAAC;IACxC,CAAC;AACH,CAAC,CAAC;AANW,QAAA,EAAE,MAMb"}
+1
View File
@@ -0,0 +1 @@
export type Code<T = unknown> = () => Promise<T>;
+3
View File
@@ -0,0 +1,3 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
//# sourceMappingURL=types.js.map
+1
View File
@@ -0,0 +1 @@
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/thingies/types.ts"],"names":[],"mappings":""}