task-serializer
Advanced tools
Comparing version 2.1.2 to 2.1.3
@@ -1,8 +0,7 @@ | ||
declare type Resolver = (value?: any) => void; | ||
interface Promolve { | ||
promise: Promise<any>; | ||
resolve: Resolver; | ||
interface Promolve<T = void> { | ||
promise: Promise<T>; | ||
resolve: (value: T) => void; | ||
} | ||
declare function makePromolve(): Promolve; | ||
export type { Resolver, Promolve }; | ||
declare function makePromolve<T = void>(): Promolve<T>; | ||
export type { Promolve }; | ||
export { makePromolve }; |
@@ -5,5 +5,5 @@ "use strict"; | ||
function makePromolve() { | ||
// @ts-expect-error | ||
let pr = {}; | ||
pr.promise = new Promise(r => pr.resolve = r); | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const pr = {}; | ||
pr.promise = new Promise((r) => (pr.resolve = r)); | ||
return pr; | ||
@@ -10,0 +10,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import type { Resolver, Promolve } from './lib'; | ||
import type { Promolve } from './lib'; | ||
import { EmptyCallback, TaskCallback } from './uif-common'; | ||
@@ -6,5 +6,5 @@ declare class Semaphore { | ||
_count: number; | ||
_resolveq: Resolver[]; | ||
_resolveq: ((_: void) => void)[]; | ||
constructor(concurrentLimit?: number); | ||
wait(): Promise<any>; | ||
wait(): Promise<void>; | ||
signal(): void; | ||
@@ -27,4 +27,4 @@ getCount(): number; | ||
static _makepr(): Promolve; | ||
addTask(func: Function, ...args: any[]): void; | ||
addTask(prom: Promise<any>): void; | ||
addTask(func: (...args: any[]) => unknown, ...args: any[]): void; | ||
addTask(prom: Promise<unknown>): void; | ||
addEnd(): void; | ||
@@ -31,0 +31,0 @@ getWorkingCount(): number; |
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -33,7 +33,5 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
} | ||
let res; | ||
let p = new Promise((r) => { res = r; }); | ||
// @ts-expect-error : Variable 'res' is used before being assigned. | ||
this._resolveq.push(res); | ||
return p; | ||
const pr = lib_1.makePromolve(); | ||
this._resolveq.push(pr.resolve); | ||
return pr.promise; | ||
}); | ||
@@ -43,13 +41,20 @@ } | ||
if (this._resolveq.length) | ||
(this._resolveq.splice(0, 1)[0])(); // resolve it | ||
this._resolveq.splice(0, 1)[0](); | ||
// resolve it | ||
else | ||
this._count++; | ||
} | ||
getCount() { return this._count; } | ||
getconcurrentLimit() { return this._concurrentLimit; } | ||
getWaitingCount() { return this._resolveq.length; } | ||
getCount() { | ||
return this._count; | ||
} | ||
getconcurrentLimit() { | ||
return this._concurrentLimit; | ||
} | ||
getWaitingCount() { | ||
return this._resolveq.length; | ||
} | ||
} | ||
class TaskSerializer { | ||
constructor(concurrentLimit) { | ||
this._usingConcurrentLimit = (concurrentLimit > 0); | ||
this._usingConcurrentLimit = concurrentLimit > 0; | ||
this._sem = null; | ||
@@ -72,4 +77,4 @@ if (this._usingConcurrentLimit) | ||
addTask(...args) { | ||
let func = args.shift(); | ||
let p = (() => __awaiter(this, void 0, void 0, function* () { | ||
const func = args.shift(); | ||
const p = (() => __awaiter(this, void 0, void 0, function* () { | ||
if (this._usingConcurrentLimit) | ||
@@ -80,3 +85,3 @@ // @ts-expect-error: Object is possibly 'null'. | ||
let result; | ||
if (func instanceof Function) | ||
if (typeof func == 'function') | ||
result = yield func(...args); | ||
@@ -87,2 +92,4 @@ else if (func instanceof Promise) { | ||
result = func; // OK | ||
if (args.length > 1) | ||
throw new Error('addTask, extra parameters given after Promise type'); | ||
} | ||
@@ -106,4 +113,5 @@ else | ||
this._sem.signal(); | ||
if (this._endFlag | ||
&& this.getWaitingCount() == 0 && this.getWorkingCount() == 0) { | ||
if (this._endFlag && | ||
this.getWaitingCount() == 0 && | ||
this.getWorkingCount() == 0) { | ||
if (this._onEmptyCallback) | ||
@@ -116,5 +124,6 @@ this._onEmptyCallback(); | ||
// eslint-disable-next-line no-unused-vars | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
p.catch((e) => { }); // unhandledRejection-defuse, without this, boom! | ||
// if (this._taskq) | ||
// this._taskq.push(p);// defused | ||
// if (this._taskq) | ||
// this._taskq.push(p);// defused | ||
//return p; | ||
@@ -124,3 +133,3 @@ } | ||
this._endFlag = true; | ||
// this section required in case addEnd() is called after all | ||
// this section required in case addEnd() is called after all | ||
// tasks have already finished, c.f. addTask() similar code. | ||
@@ -142,9 +151,19 @@ if (this.getWaitingCount() == 0 && this.getWorkingCount() == 0) { | ||
} | ||
getResolvedCount() { return this._numResolved; } | ||
getRejectedCount() { return this._numRejected; } | ||
onEmpty(callback) { this._onEmptyCallback = callback; } | ||
onTaskResolved(callback) { this._onTaskResolvedCallback = callback; } | ||
onTaskRejected(callback) { this._onTaskRejectedCallback = callback; } | ||
getResolvedCount() { | ||
return this._numResolved; | ||
} | ||
getRejectedCount() { | ||
return this._numRejected; | ||
} | ||
onEmpty(callback) { | ||
this._onEmptyCallback = callback; | ||
} | ||
onTaskResolved(callback) { | ||
this._onTaskResolvedCallback = callback; | ||
} | ||
onTaskRejected(callback) { | ||
this._onTaskRejectedCallback = callback; | ||
} | ||
} | ||
exports.TaskSerializer = TaskSerializer; | ||
//# sourceMappingURL=task-serializer.js.map |
import type { Promolve } from './lib'; | ||
import { Common, CommonCtorParams } from './uif-common'; | ||
declare class AsyncIter extends Common { | ||
_q: any[]; | ||
_qe: any[]; | ||
_q: unknown[]; | ||
_qe: unknown[]; | ||
_nextpr: Promolve; | ||
@@ -10,18 +10,15 @@ _emptyFlag: boolean; | ||
next(): Promise<{ | ||
done: boolean; | ||
value: any[]; | ||
done: false; | ||
value: any; | ||
} | { | ||
done: boolean; | ||
value?: undefined; | ||
done: true; | ||
}>; | ||
}; | ||
constructor(...args: CommonCtorParams); | ||
_reset_nextpr(): void; | ||
_createAsyncIterable(): { | ||
next(): Promise<{ | ||
done: boolean; | ||
value: any[]; | ||
done: false; | ||
value: any; | ||
} | { | ||
done: boolean; | ||
value?: undefined; | ||
done: true; | ||
}>; | ||
@@ -31,15 +28,13 @@ }; | ||
next(): Promise<{ | ||
done: boolean; | ||
value: any[]; | ||
done: false; | ||
value: any; | ||
} | { | ||
done: boolean; | ||
value?: undefined; | ||
done: true; | ||
}>; | ||
}; | ||
next(): Promise<{ | ||
done: boolean; | ||
value: any[]; | ||
done: false; | ||
value: any; | ||
} | { | ||
done: boolean; | ||
value?: undefined; | ||
done: true; | ||
}>; | ||
@@ -46,0 +41,0 @@ getCountResolvedNotRead(): number; |
@@ -36,14 +36,16 @@ "use strict"; | ||
} | ||
_reset_nextpr() { | ||
this._nextpr = lib_1.makePromolve(); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
_createAsyncIterable() { | ||
let that = this; | ||
// eslint-disable-next-line @typescript-eslint/no-this-alias | ||
const that = this; | ||
return { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
next() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
for (let iter = 0;; iter++) { | ||
if (that._qe.length) // errors have priority | ||
if (that._qe.length) | ||
// errors have priority | ||
throw that._qe.splice(0, 1)[0]; | ||
if (that._q.length) // "normal" return | ||
if (that._q.length) | ||
// "normal" return | ||
return { done: false, value: that._q.splice(0, 1) }; | ||
@@ -61,15 +63,25 @@ // empty flag may be set before q,qe are drained so check this last | ||
}); | ||
} | ||
}, | ||
}; | ||
} | ||
[Symbol.asyncIterator]() { return this._asyncIterable; } | ||
next() { return this._asyncIterable.next(); } | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
[Symbol.asyncIterator]() { | ||
return this._asyncIterable; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types | ||
next() { | ||
return this._asyncIterable.next(); | ||
} | ||
// informationals | ||
// getCountWaiting(){return this._ts.getWaitingCount();} | ||
// getCountWorking(){return this._ts.getWorkingCount();} | ||
getCountResolvedNotRead() { return this._q.length; } | ||
getCountRejectedNotRead() { return this._qe.length; } | ||
getCountFinishedNotRead() { return this._q.length + this._qe.length; } | ||
getCountResolvedNotRead() { | ||
return this._q.length; | ||
} | ||
getCountRejectedNotRead() { | ||
return this._qe.length; | ||
} | ||
getCountFinishedNotRead() { | ||
return this._q.length + this._qe.length; | ||
} | ||
} | ||
exports.AsyncIter = AsyncIter; | ||
//# sourceMappingURL=uif-async-iter.js.map |
@@ -12,7 +12,13 @@ "use strict"; | ||
} | ||
onTaskResolved(cb) { this._ts.onTaskResolved(cb); } | ||
onTaskRejected(cb) { this._ts.onTaskRejected(cb); } | ||
onEmpty(cb) { this._ts.onEmpty(cb); } | ||
onTaskResolved(cb) { | ||
this._ts.onTaskResolved(cb); | ||
} | ||
onTaskRejected(cb) { | ||
this._ts.onTaskRejected(cb); | ||
} | ||
onEmpty(cb) { | ||
this._ts.onEmpty(cb); | ||
} | ||
} | ||
exports.Callbacks = Callbacks; | ||
//# sourceMappingURL=uif-callbacks.js.map |
@@ -15,4 +15,4 @@ import { TaskSerializer } from './task-serializer'; | ||
constructor(...args: CommonCtorParams); | ||
addTask(func: Function, ...args: any[]): void; | ||
addTask(prom: Promise<any>): void; | ||
addTask(func: (...args: any[]) => unknown, ...args: any[]): void; | ||
addTask(prom: Promise<unknown>): void; | ||
addEnd(): void; | ||
@@ -19,0 +19,0 @@ getCountWaiting(): number; |
@@ -6,3 +6,3 @@ "use strict"; | ||
const task_serializer_1 = require("./task-serializer"); | ||
// interface CtorArg {concurrentTaskLimit:number} | ||
// interface CtorArg {concurrentTaskLimit:number} | ||
// interface CommonCtor{ | ||
@@ -18,4 +18,4 @@ // new (carg?:number):any | ||
concurrentTaskLimit = args[0]; | ||
else if (typeof args[0] == "object" && | ||
typeof args[0].concurrentTaskLimit == "number") | ||
else if (typeof args[0] == 'object' && | ||
typeof args[0].concurrentTaskLimit == 'number') | ||
concurrentTaskLimit = args[0].concurrentTaskLimit; | ||
@@ -28,2 +28,3 @@ else | ||
addTask(...args) { | ||
//this._ts.addTask(...args); | ||
this._ts.addTask(args[0], ...args.slice(1)); | ||
@@ -34,9 +35,19 @@ } | ||
} | ||
getCountWaiting() { return this._ts.getWaitingCount(); } | ||
getCountWorking() { return this._ts.getWorkingCount(); } | ||
getCountResolvedTotal() { return this._ts.getResolvedCount(); } | ||
getCountRejectedTotal() { return this._ts.getRejectedCount(); } | ||
getCountFinishedTotal() { return this._ts.getFinishedCount(); } | ||
getCountWaiting() { | ||
return this._ts.getWaitingCount(); | ||
} | ||
getCountWorking() { | ||
return this._ts.getWorkingCount(); | ||
} | ||
getCountResolvedTotal() { | ||
return this._ts.getResolvedCount(); | ||
} | ||
getCountRejectedTotal() { | ||
return this._ts.getRejectedCount(); | ||
} | ||
getCountFinishedTotal() { | ||
return this._ts.getFinishedCount(); | ||
} | ||
} | ||
exports.Common = Common; | ||
//# sourceMappingURL=uif-common.js.map |
@@ -6,15 +6,15 @@ import type { Promolve } from './lib'; | ||
_error: Promolve; | ||
_qresults: any[]; | ||
_qerrors: any[]; | ||
_qresults: unknown[]; | ||
_qerrors: unknown[]; | ||
_empty: Promolve; | ||
_symTaskResolved: Symbol; | ||
_symTaskRejected: Symbol; | ||
_symAllRead: Symbol; | ||
_symTaskResolved: symbol; | ||
_symTaskRejected: symbol; | ||
_symAllRead: symbol; | ||
constructor(...args: CommonCtorParams); | ||
getTaskResolvedValue(): any; | ||
getTaskRejectedValue(): any; | ||
symbolAllRead(): Symbol; | ||
symbolTaskResolved(): Symbol; | ||
symbolTaskRejected(): Symbol; | ||
nextSymbol(): Promise<Symbol>; | ||
symbolAllRead(): symbol; | ||
symbolTaskResolved(): symbol; | ||
symbolTaskRejected(): symbol; | ||
nextSymbol(): Promise<symbol>; | ||
getCountResolvedNotRead(): number; | ||
@@ -21,0 +21,0 @@ getCountRejectedNotRead(): number; |
@@ -30,2 +30,3 @@ "use strict"; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
getTaskResolvedValue() { | ||
@@ -38,2 +39,3 @@ if (!this._qresults.length) | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
getTaskRejectedValue() { | ||
@@ -46,17 +48,34 @@ if (!this._qerrors.length) | ||
} | ||
symbolAllRead() { return this._symAllRead; } | ||
symbolTaskResolved() { return this._symTaskResolved; } | ||
symbolTaskRejected() { return this._symTaskRejected; } | ||
symbolAllRead() { | ||
return this._symAllRead; | ||
} | ||
symbolTaskResolved() { | ||
return this._symTaskResolved; | ||
} | ||
symbolTaskRejected() { | ||
return this._symTaskRejected; | ||
} | ||
nextSymbol() { | ||
// this promise can be safely abandoned | ||
// Note: the order of promises ensures that this._symAllRead | ||
// won't be returned until all task results are actually read. | ||
return Promise.race([ | ||
this._error.promise.then(() => { return this._symTaskRejected; }), | ||
this._result.promise.then(() => { return this._symTaskResolved; }), | ||
this._empty.promise.then(() => { return this._symAllRead; }), | ||
this._error.promise.then(() => { | ||
return this._symTaskRejected; | ||
}), | ||
this._result.promise.then(() => { | ||
return this._symTaskResolved; | ||
}), | ||
this._empty.promise.then(() => { | ||
return this._symAllRead; | ||
}), | ||
]); | ||
} | ||
// informationals | ||
getCountResolvedNotRead() { return this._qresults.length; } | ||
getCountRejectedNotRead() { return this._qerrors.length; } | ||
getCountResolvedNotRead() { | ||
return this._qresults.length; | ||
} | ||
getCountRejectedNotRead() { | ||
return this._qerrors.length; | ||
} | ||
getCountFinishedNotRead() { | ||
@@ -63,0 +82,0 @@ return this._qresults.length + this._qerrors.length; |
import type { Promolve } from './lib'; | ||
import { Common, CommonCtorParams } from './uif-common'; | ||
declare class WaitAll extends Common { | ||
_results: any[]; | ||
_results: unknown[]; | ||
_error: Promolve; | ||
_empty: Promolve; | ||
constructor(...args: CommonCtorParams); | ||
waitAll(): Promise<any[]>; | ||
waitAll(): Promise<any | any[]>; | ||
waitAllSettled(): Promise<PromiseSettledResult<any>[]>; | ||
} | ||
export { WaitAll }; |
@@ -27,4 +27,4 @@ "use strict"; | ||
// eslint-disable-next-line no-unused-vars | ||
let p = Promise.reject(err); | ||
p.catch(e => e); //defuse | ||
const p = Promise.reject(err); | ||
p.catch((e) => e); //defuse | ||
this._results.push(p); | ||
@@ -37,2 +37,3 @@ this._error.resolve(); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
waitAll() { | ||
@@ -44,6 +45,6 @@ return __awaiter(this, void 0, void 0, function* () { | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
waitAllSettled() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield this._empty.promise; | ||
// @ts-ignore | ||
return yield Promise.allSettled(this._results); | ||
@@ -50,0 +51,0 @@ }); |
{ | ||
"name": "task-serializer", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "Serialize tasks/promises for integrated control. Option for limiting number of concurrent tasks.", | ||
@@ -23,3 +23,3 @@ "main": "dist/index.js", | ||
"test": "node ./tests-js/script-test.js;node ./tests-ts/script-test.js", | ||
"pre-publish": "node scripts-ts/pre-publish-test.js ./tests-js ./examples-js ./tests-ts ./examples-ts", | ||
"prepublishOnly": "node scripts/check-package-depends.js && node scripts-ts/pre-publish-test.js ./tests-js ./examples-js ./tests-ts ./examples-ts", | ||
"build-dist": "tsc", | ||
@@ -51,3 +51,9 @@ "build-tests": "tsc -p tsconfig.tests.json", | ||
"@types/node": "^14.14.14", | ||
"@typescript-eslint/eslint-plugin": "^4.11.0", | ||
"@typescript-eslint/parser": "^4.11.0", | ||
"eslint": "^7.16.0", | ||
"eslint-config-prettier": "^7.1.0", | ||
"eslint-plugin-prettier": "^3.3.0", | ||
"mini-preproc": "^1.1.2", | ||
"prettier": "2.2.1", | ||
"typescript": "^4.1.3" | ||
@@ -54,0 +60,0 @@ }, |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const filenames = [ | ||
"demo-async-iter", | ||
"demo-callbacks", | ||
"demo-next-symbol", | ||
"demo-wait-all", | ||
"demo-lib", | ||
'demo-async-iter', | ||
'demo-callbacks', | ||
'demo-next-symbol', | ||
'demo-wait-all', | ||
'demo-lib', | ||
]; | ||
exports.default = filenames; | ||
//# sourceMappingURL=demo-filenames.js.map |
@@ -18,8 +18,8 @@ "use strict"; | ||
assert.strict(Array.isArray(filenames)); | ||
assert.strictEqual(typeof filenames[0], "string"); | ||
assert.strictEqual(typeof srcdir, "string"); | ||
assert.strictEqual(typeof dstdir, "string"); | ||
assert.strictEqual(typeof isTS, "boolean"); | ||
assert.strictEqual(typeof nonodeSubdir, "string"); | ||
assert.strictEqual(typeof nodeSubdir, "string"); | ||
assert.strictEqual(typeof filenames[0], 'string'); | ||
assert.strictEqual(typeof srcdir, 'string'); | ||
assert.strictEqual(typeof dstdir, 'string'); | ||
assert.strictEqual(typeof isTS, 'boolean'); | ||
assert.strictEqual(typeof nonodeSubdir, 'string'); | ||
assert.strictEqual(typeof nodeSubdir, 'string'); | ||
let ext = isTS ? '.ts' : '.js'; | ||
@@ -38,4 +38,4 @@ let subdirs = [nonodeSubdir, nodeSubdir]; | ||
.pipe(mini_preproc_1.createPreprocStream({ | ||
"RELEASE": true, | ||
"NODEJS": isnodejs | ||
RELEASE: true, | ||
NODEJS: isnodejs, | ||
}, { strip: true })) | ||
@@ -42,0 +42,0 @@ .on('error', (e) => { |
@@ -29,3 +29,3 @@ "use strict"; | ||
const demo_filenames_1 = __importDefault(require("./demo-filenames")); | ||
// mini-preproc must be installed for gen-examples module to load | ||
// mini-preproc must be installed for gen-examples module to load | ||
cp.execSync('npm install --save-dev mini-preproc'); | ||
@@ -72,19 +72,19 @@ const gen_examples_1 = __importDefault(require("./gen-examples")); | ||
return { | ||
"extends": `@tsconfig/${name}/tsconfig.json`, | ||
"compilerOptions": { | ||
"declaration": true, | ||
"noImplicitAny": true, | ||
"skipLibCheck": false, | ||
"sourceMap": true, | ||
"lib": [ | ||
"es2015", | ||
"es2016", | ||
"es2017", | ||
"es2018", | ||
"es2019", | ||
"es2020", | ||
"esnext" | ||
] | ||
extends: `@tsconfig/${name}/tsconfig.json`, | ||
compilerOptions: { | ||
declaration: true, | ||
noImplicitAny: true, | ||
skipLibCheck: false, | ||
sourceMap: true, | ||
lib: [ | ||
'es2015', | ||
'es2016', | ||
'es2017', | ||
'es2018', | ||
'es2019', | ||
'es2020', | ||
'esnext', | ||
], | ||
}, | ||
"include": ainclude | ||
include: ainclude, | ||
}; | ||
@@ -95,7 +95,9 @@ } | ||
`./tsconfig.${tsconfMnem[0]}.json`, | ||
`./tsconfig.${tsconfMnem[1]}.json` | ||
`./tsconfig.${tsconfMnem[1]}.json`, | ||
]; | ||
async function setupTypescript(tsDstDir_) { | ||
let tsDstDir = tsDstDir_; | ||
if (tsDstDir[0] !== '/' && tsDstDir.length > 1 && tsDstDir.slice(0, 2) !== './') | ||
if (tsDstDir[0] !== '/' && | ||
tsDstDir.length > 1 && | ||
tsDstDir.slice(0, 2) !== './') | ||
tsDstDir = './' + tsDstDir; | ||
@@ -107,3 +109,5 @@ let proms = []; | ||
rootRelFns.push(`${tsDstDir}/${tsconfMnem[i]}/${f}.ts`); | ||
proms.push(fs_1.promises.writeFile(tsconfFns[i], JSON.stringify(tsconfig(tsconfMnem[i], rootRelFns), null, 2)).then(() => { | ||
proms.push(fs_1.promises | ||
.writeFile(tsconfFns[i], JSON.stringify(tsconfig(tsconfMnem[i], rootRelFns), null, 2)) | ||
.then(() => { | ||
console.log(`[OK] wrote ${tsconfFns[i]}`); | ||
@@ -123,3 +127,3 @@ })); | ||
} | ||
})() | ||
})(), | ||
]); | ||
@@ -139,4 +143,4 @@ } | ||
assert.strict(process.argv.length >= 4); | ||
assert.strictEqual(typeof process.argv[2], "string"); | ||
assert.strictEqual(typeof process.argv[3], "string"); | ||
assert.strictEqual(typeof process.argv[2], 'string'); | ||
assert.strictEqual(typeof process.argv[3], 'string'); | ||
let doTS = true; | ||
@@ -147,4 +151,4 @@ if (process.argv.length == 4) | ||
assert.strict(process.argv.length == 6); | ||
assert.strictEqual(typeof process.argv[4], "string"); | ||
assert.strictEqual(typeof process.argv[5], "string"); | ||
assert.strictEqual(typeof process.argv[4], 'string'); | ||
assert.strictEqual(typeof process.argv[5], 'string'); | ||
} | ||
@@ -155,6 +159,8 @@ await Promise.all([ | ||
// @ts-ignore | ||
await gen_examples_1.default(demo_filenames_1.default, ...process.argv.slice(2, 4), false, ...tsconfMnem) | ||
.then(() => { console.log('[OK] genExamples js'); }); //js | ||
await demos(jsDstDir) | ||
.then(() => { console.log('[OK] js demos done'); }); | ||
await gen_examples_1.default(demo_filenames_1.default, process.argv[2], process.argv[3], false, tsconfMnem[0], tsconfMnem[1]).then(() => { | ||
console.log('[OK] genExamples js'); | ||
}); //js | ||
await demos(jsDstDir).then(() => { | ||
console.log('[OK] js demos done'); | ||
}); | ||
})(), | ||
@@ -167,17 +173,27 @@ (async () => { | ||
// @ts-ignore | ||
gen_examples_1.default(demo_filenames_1.default, ...process.argv.slice(4, 6), true, ...tsconfMnem) | ||
.then(() => { console.log('[OK] genExamples ts'); }), | ||
setupTypescript(tsDstDir) | ||
.then(() => { console.log('[OK] setupTypescript'); }), | ||
gen_examples_1.default(demo_filenames_1.default, process.argv[4], process.argv[5], true, tsconfMnem[0], tsconfMnem[1]).then(() => { | ||
console.log('[OK] genExamples ts'); | ||
}), | ||
setupTypescript(tsDstDir).then(() => { | ||
console.log('[OK] setupTypescript'); | ||
}), | ||
]); | ||
await Promise.all([ | ||
exec(`tsc -p ${tsconfFns[0]}`), | ||
exec(`tsc -p ${tsconfFns[1]}`) | ||
]).then(() => { console.log('ready to start ts demos'); }); | ||
exec(`tsc -p ${tsconfFns[1]}`), | ||
]).then(() => { | ||
console.log('ready to start ts demos'); | ||
}); | ||
await demos(tsDstDir); | ||
})() | ||
})(), | ||
]); | ||
})() | ||
.then(() => { console.log('PASS'); process.exitCode = 0; }) | ||
.catch((e) => { console.error(e.message); process.exitCode = 1; }); | ||
.then(() => { | ||
console.log('PASS'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.error(e.message); | ||
process.exitCode = 1; | ||
}); | ||
//# sourceMappingURL=post-install-demo.js.map |
@@ -6,29 +6,37 @@ 'use strict'; | ||
/* eslint-disable no-constant-condition */ | ||
const {AsyncIter}=require('../dist/uif-async-iter.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {AsyncIter} = require('../dist/uif-async-iter.js'); | ||
//--ENDIF | ||
//--IF{{NODEJS}} | ||
const {exitOnBeforeExit,producer}=require('./demo-lib.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {exitOnBeforeExit, producer} = require('./demo-lib.js'); | ||
//--ELSE | ||
//--const {producer}=require('./demo-lib.js'); | ||
//--ENDIF | ||
async function consumer(ai){ | ||
do{ | ||
try{ | ||
for await(const res of ai){ | ||
console.log(' '+JSON.stringify(res)); | ||
async function consumer(ai) { | ||
do { | ||
try { | ||
for await (const res of ai) { | ||
console.log(' ' + JSON.stringify(res)); | ||
} | ||
break; | ||
}catch(e){ | ||
console.log(' '+'error: '+e.message); | ||
} catch (e) { | ||
console.log(' ' + 'error: ' + e.message); | ||
} | ||
}while(true); | ||
} while (true); | ||
} | ||
async function main(){ | ||
let ai=new AsyncIter({concurrentTaskLimit:2}); | ||
await Promise.all([producer(ai),consumer(ai)]); | ||
async function main() { | ||
let ai = new AsyncIter({concurrentTaskLimit: 2}); | ||
await Promise.all([producer(ai), consumer(ai)]); | ||
} | ||
//--IF{{NODEJS}} | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -35,0 +43,0 @@ //--ELSE |
@@ -5,18 +5,20 @@ 'use strict'; | ||
//--ELSE | ||
const {Callbacks}=require('../dist/uif-callbacks.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {Callbacks} = require('../dist/uif-callbacks.js'); | ||
//--ENDIF | ||
//--IF{{NODEJS}} | ||
const {exitOnBeforeExit,producer}=require('./demo-lib.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {exitOnBeforeExit, producer} = require('./demo-lib.js'); | ||
//--ELSE | ||
//--const {producer}=require('./demo-lib.js'); | ||
//--ENDIF | ||
async function consumer(ts){ | ||
await new Promise((resolve)=>{ | ||
ts.onTaskResolved((resolvedValue)=>{ | ||
async function consumer(ts) { | ||
await new Promise((resolve) => { | ||
ts.onTaskResolved((resolvedValue) => { | ||
console.log(`onTaskResolved ${resolvedValue}`); | ||
}); | ||
ts.onTaskRejected((rejectedValue)=>{ | ||
ts.onTaskRejected((rejectedValue) => { | ||
console.log(`onTaskRejected ${rejectedValue}`); | ||
}); | ||
ts.onEmpty(()=>{ | ||
ts.onEmpty(() => { | ||
console.log(`onEmpty`); | ||
@@ -28,7 +30,7 @@ resolve(); | ||
} | ||
async function main(){ | ||
let ts=new Callbacks({concurrentTaskLimit:2}); | ||
async function main() { | ||
let ts = new Callbacks({concurrentTaskLimit: 2}); | ||
await Promise.all([ | ||
consumer(ts),// consumer must initialize first | ||
producer(ts) | ||
consumer(ts), // consumer must initialize first | ||
producer(ts), | ||
]); | ||
@@ -38,4 +40,10 @@ } | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -42,0 +50,0 @@ //--ELSE |
@@ -9,30 +9,35 @@ //--IF{{RELEASE}} | ||
//--ELSE | ||
var {AsyncIter,NextSymbol}=require('../dist/index'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
var {AsyncIter, NextSymbol} = require('../dist/index'); | ||
//--ENDIF | ||
function snooze(ms){return new Promise(r=>setTimeout(r,ms));} | ||
function range(len){return [...Array(len).keys()];} | ||
function makepr(){ | ||
let pr={}; | ||
pr.promise=new Promise((r)=>{pr.resolve=r;}); | ||
function snooze(ms) { | ||
return new Promise((r) => setTimeout(r, ms)); | ||
} | ||
function range(len) { | ||
return [...Array(len).keys()]; | ||
} | ||
function makepr() { | ||
let pr = {}; | ||
pr.promise = new Promise((r) => { | ||
pr.resolve = r; | ||
}); | ||
return pr; | ||
} | ||
function logStatus(ts){ | ||
let wa=ts.getCountWaiting(); | ||
let wo=ts.getCountWorking(); | ||
let rest=ts.getCountResolvedTotal(); | ||
let rejt=ts.getCountRejectedTotal(); | ||
let fint=ts.getCountFinishedTotal(); | ||
console.log( | ||
`wa:${wa},wo:${wo},rest:${rest},rejt:${rejt},fint:${fint}`); | ||
if ((ts instanceof AsyncIter)||(ts instanceof NextSymbol)){ | ||
let resnr=ts.getCountResolvedNotRead(); | ||
let rejnr=ts.getCountRejectedNotRead(); | ||
let finnr=ts.getCountFinishedNotRead(); | ||
function logStatus(ts) { | ||
let wa = ts.getCountWaiting(); | ||
let wo = ts.getCountWorking(); | ||
let rest = ts.getCountResolvedTotal(); | ||
let rejt = ts.getCountRejectedTotal(); | ||
let fint = ts.getCountFinishedTotal(); | ||
console.log(`wa:${wa},wo:${wo},rest:${rest},rejt:${rejt},fint:${fint}`); | ||
if (ts instanceof AsyncIter || ts instanceof NextSymbol) { | ||
let resnr = ts.getCountResolvedNotRead(); | ||
let rejnr = ts.getCountRejectedNotRead(); | ||
let finnr = ts.getCountFinishedNotRead(); | ||
console.log(`resnr:${resnr},rejnr:${rejnr},finnr:${finnr}`); | ||
} | ||
} | ||
async function task(id,ms,err=false){ | ||
async function task(id, ms, err = false) { | ||
console.log(`-->enter ${id}`); | ||
if (err) | ||
throw new Error(`task failed id=${id}`); | ||
if (err) throw new Error(`task failed id=${id}`); | ||
await snooze(ms); | ||
@@ -42,35 +47,34 @@ console.log(`<--leave ${id}`); | ||
} | ||
async function producer(ts){ | ||
for (let i=0; i<6; i++){ | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
async function producer(ts) { | ||
for (let i = 0; i < 6; i++) { | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
logStatus(ts); | ||
//--ENDIF | ||
ts.addTask(task,i,2**(10-i),(i+1)%3==0); | ||
//--ENDIF | ||
ts.addTask(task, i, 2 ** (10 - i), (i + 1) % 3 == 0); | ||
await snooze(100); | ||
} | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
logStatus(ts); | ||
//--ENDIF | ||
//--ENDIF | ||
ts.addEnd(); | ||
console.log('producer finished'); | ||
} | ||
module.exports.snooze=snooze; | ||
module.exports.task=task; | ||
module.exports.range=range; | ||
module.exports.makepr=makepr; | ||
module.exports.producer=producer; | ||
module.exports.snooze = snooze; | ||
module.exports.task = task; | ||
module.exports.range = range; | ||
module.exports.makepr = makepr; | ||
module.exports.producer = producer; | ||
//--IF{{NODEJS}} | ||
function exitOnBeforeExit(exitCode){ | ||
process.on('beforeExit',async()=>{ | ||
if (typeof process.exitCode=='undefined'){ | ||
function exitOnBeforeExit(exitCode) { | ||
process.on('beforeExit', async () => { | ||
if (typeof process.exitCode == 'undefined') { | ||
console.error('unexpected "beforeExit" event'); | ||
process.exit(exitCode); | ||
} else | ||
process.exit(process.exitCode); | ||
} else process.exit(process.exitCode); | ||
}); | ||
} | ||
module.exports.exitOnBeforeExit=exitOnBeforeExit; | ||
module.exports.exitOnBeforeExit = exitOnBeforeExit; | ||
//--ENDIF |
@@ -5,48 +5,58 @@ 'use strict'; | ||
//--ELSE | ||
const {NextSymbol}=require('../dist/uif-next-symbol.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {NextSymbol} = require('../dist/uif-next-symbol.js'); | ||
//--ENDIF | ||
//--IF{{NODEJS}} | ||
const {exitOnBeforeExit,makepr,producer}=require('./demo-lib.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {exitOnBeforeExit, makepr, producer} = require('./demo-lib.js'); | ||
//--ELSE | ||
//--const {makepr,producer}=require('./demo-lib.js'); | ||
//--ENDIF | ||
var somethingElse=makepr(); | ||
var iv=setInterval(()=>{somethingElse.resolve("somethingElse");},300); | ||
async function consumer(ts){ | ||
let emptied=false; | ||
while(!emptied){ | ||
let next = await Promise.race([ | ||
somethingElse.promise, | ||
ts.nextSymbol(), | ||
]); | ||
switch(next){ | ||
case "somethingElse": | ||
console.log(next); | ||
somethingElse=makepr();// reset | ||
break; | ||
case ts.symbolTaskResolved():{ | ||
console.log(); | ||
let res=ts.getTaskResolvedValue(); | ||
console.log("symbolTaskResolved, result="+res); | ||
break;} | ||
case ts.symbolTaskRejected():{ | ||
let e=ts.getTaskRejectedValue(); | ||
console.log("symbolTaskRejected, message="+e.message); | ||
break;} | ||
case ts.symbolAllRead():{ | ||
console.log("symbolAllRead"); | ||
emptied=true; | ||
clearInterval(iv); | ||
break;} | ||
var somethingElse = makepr(); | ||
var iv = setInterval(() => { | ||
somethingElse.resolve('somethingElse'); | ||
}, 300); | ||
async function consumer(ts) { | ||
let emptied = false; | ||
while (!emptied) { | ||
let next = await Promise.race([somethingElse.promise, ts.nextSymbol()]); | ||
switch (next) { | ||
case 'somethingElse': | ||
console.log(next); | ||
somethingElse = makepr(); // reset | ||
break; | ||
case ts.symbolTaskResolved(): { | ||
console.log(); | ||
let res = ts.getTaskResolvedValue(); | ||
console.log('symbolTaskResolved, result=' + res); | ||
break; | ||
} | ||
case ts.symbolTaskRejected(): { | ||
let e = ts.getTaskRejectedValue(); | ||
console.log('symbolTaskRejected, message=' + e.message); | ||
break; | ||
} | ||
case ts.symbolAllRead(): { | ||
console.log('symbolAllRead'); | ||
emptied = true; | ||
clearInterval(iv); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
async function main(){ | ||
let ts=new NextSymbol({concurrentTaskLimit:2}); | ||
await Promise.all([consumer(ts),producer(ts)]); | ||
async function main() { | ||
let ts = new NextSymbol({concurrentTaskLimit: 2}); | ||
await Promise.all([consumer(ts), producer(ts)]); | ||
} | ||
//--IF{{NODEJS}} | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -53,0 +63,0 @@ //--ELSE |
@@ -5,40 +5,42 @@ 'use strict'; | ||
//--ELSE | ||
const {WaitAll}=require('../dist/uif-wait-all.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {WaitAll} = require('../dist/uif-wait-all.js'); | ||
//--ENDIF | ||
//--IF{{NODEJS}} | ||
const {exitOnBeforeExit,producer}=require('./demo-lib.js'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const {exitOnBeforeExit, producer} = require('./demo-lib.js'); | ||
//--ELSE | ||
//--const {producer}=require('./demo-lib.js'); | ||
//--ENDIF | ||
async function consumer_waitAll(ts){ | ||
try{ | ||
let r=await ts.waitAll(); | ||
async function consumer_waitAll(ts) { | ||
try { | ||
let r = await ts.waitAll(); | ||
console.log(`ts.waitAll() returned`); | ||
console.log(JSON.stringify(r,0,2)); | ||
}catch(e){ | ||
console.log(JSON.stringify(r, 0, 2)); | ||
} catch (e) { | ||
console.log(`ts.waitAll() caught ${e.message}`); | ||
} | ||
} | ||
async function consumer_waitAllSettled(ts){ | ||
let r=await ts.waitAllSettled(); | ||
async function consumer_waitAllSettled(ts) { | ||
let r = await ts.waitAllSettled(); | ||
console.log(`ts.waitAllSettled() returned`); | ||
console.log(JSON.stringify(r,0,2)); | ||
console.log(JSON.stringify(r, 0, 2)); | ||
console.log('consumer finished'); | ||
} | ||
async function main(){ | ||
let waitAll=new WaitAll({concurrentTaskLimit:2}); | ||
await Promise.all([ | ||
consumer_waitAll(waitAll), | ||
producer(waitAll), | ||
]); | ||
waitAll=new WaitAll({concurrentTaskLimit:2}); | ||
await Promise.all([ | ||
consumer_waitAllSettled(waitAll), | ||
producer(waitAll), | ||
]); | ||
async function main() { | ||
let waitAll = new WaitAll({concurrentTaskLimit: 2}); | ||
await Promise.all([consumer_waitAll(waitAll), producer(waitAll)]); | ||
waitAll = new WaitAll({concurrentTaskLimit: 2}); | ||
await Promise.all([consumer_waitAllSettled(waitAll), producer(waitAll)]); | ||
} | ||
//--IF{{NODEJS}} | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -45,0 +47,0 @@ //--ELSE |
@@ -1,2 +0,1 @@ | ||
//--IF{{RELEASE}} | ||
@@ -9,26 +8,33 @@ //--import {AsyncIter} from 'task-serializer' | ||
//--IF{{NODEJS}} | ||
import {exitOnBeforeExit,producer} from './demo-lib'; | ||
import {exitOnBeforeExit, producer} from './demo-lib'; | ||
//--ELSE | ||
//--import {producer} from './demo-lib'; | ||
//--ENDIF | ||
async function consumer(ai: AsyncIter){ | ||
do{ | ||
try{ | ||
for await(const res of ai){ | ||
console.log(' '+JSON.stringify(res)); | ||
async function consumer(ai: AsyncIter) { | ||
do { | ||
try { | ||
for await (const res of ai) { | ||
console.log(' ' + JSON.stringify(res)); | ||
} | ||
break; | ||
}catch(e){ | ||
console.log(' '+'error: '+e.message); | ||
} catch (e) { | ||
console.log(' ' + 'error: ' + e.message); | ||
} | ||
}while(true); | ||
// eslint-disable-next-line no-constant-condition | ||
} while (true); | ||
} | ||
async function main(){ | ||
let ai=new AsyncIter({concurrentTaskLimit:2}); | ||
await Promise.all([producer(ai),consumer(ai)]); | ||
async function main() { | ||
const ai = new AsyncIter({concurrentTaskLimit: 2}); | ||
await Promise.all([producer(ai), consumer(ai)]); | ||
} | ||
//--IF{{NODEJS}} | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -35,0 +41,0 @@ //--ELSE |
@@ -8,15 +8,15 @@ 'use strict'; | ||
//--IF{{NODEJS}} | ||
import {exitOnBeforeExit,producer} from './demo-lib.js'; | ||
import {exitOnBeforeExit, producer} from './demo-lib.js'; | ||
//--ELSE | ||
//--import {producer} from './demo-lib.js'; | ||
//--ENDIF | ||
async function consumer(ts: Callbacks){ | ||
await new Promise<void>((resolve)=>{ | ||
ts.onTaskResolved((resolvedValue:any)=>{ | ||
async function consumer(ts: Callbacks) { | ||
await new Promise<void>((resolve) => { | ||
ts.onTaskResolved((resolvedValue) => { | ||
console.log(`onTaskResolved ${resolvedValue}`); | ||
}); | ||
ts.onTaskRejected((rejectedValue)=>{ | ||
ts.onTaskRejected((rejectedValue) => { | ||
console.log(`onTaskRejected ${rejectedValue}`); | ||
}); | ||
ts.onEmpty(()=>{ | ||
ts.onEmpty(() => { | ||
console.log(`onEmpty`); | ||
@@ -28,7 +28,7 @@ resolve(); | ||
} | ||
async function main(){ | ||
let ts=new Callbacks({concurrentTaskLimit:2}); | ||
async function main() { | ||
const ts = new Callbacks({concurrentTaskLimit: 2}); | ||
await Promise.all([ | ||
consumer(ts),// consumer must initialize first | ||
producer(ts) | ||
consumer(ts), // consumer must initialize first | ||
producer(ts), | ||
]); | ||
@@ -38,4 +38,10 @@ } | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -42,0 +48,0 @@ //--ELSE |
//--IF{{RELEASE}} | ||
//--import {AsyncIter,Callbacks,NextSymbol, WaitAll} from 'task-serializer'; | ||
//--ELSE | ||
import {AsyncIter,Callbacks,NextSymbol,WaitAll} from '../dist/index'; | ||
import {AsyncIter, Callbacks, NextSymbol, WaitAll} from '../dist/index'; | ||
//--ENDIF | ||
function snooze(ms:number){return new Promise(r=>setTimeout(r,ms));} | ||
function range(len:number){return [...Array(len).keys()];} | ||
interface Promolve{promise:Promise<any>,resolve:(r:any)=>void} | ||
function makepr():Promolve{ | ||
let pr={}; | ||
// @ts-expect-error | ||
pr.promise=new Promise((r)=>{pr.resolve=r;}); | ||
// @ts-expect-error | ||
return pr; | ||
function snooze(ms: number): Promise<void> { | ||
return new Promise((r) => setTimeout(r, ms)); | ||
} | ||
function logStatus(ts:any){ | ||
let wa=ts.getCountWaiting(); | ||
let wo=ts.getCountWorking(); | ||
let rest=ts.getCountResolvedTotal(); | ||
let rejt=ts.getCountRejectedTotal(); | ||
let fint=ts.getCountFinishedTotal(); | ||
console.log( | ||
`wa:${wa},wo:${wo},rest:${rest},rejt:${rejt},fint:${fint}`); | ||
if ((ts instanceof AsyncIter)||(ts instanceof NextSymbol)){ | ||
let resnr=ts.getCountResolvedNotRead(); | ||
let rejnr=ts.getCountRejectedNotRead(); | ||
let finnr=ts.getCountFinishedNotRead(); | ||
function range(len: number): number[] { | ||
return [...Array(len).keys()]; | ||
} | ||
interface Promolve<T = void> { | ||
promise: Promise<T>; | ||
resolve: (value: T) => void; | ||
} | ||
function makepr<T = void>(): Promolve<T> { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const pr: any = {}; | ||
pr.promise = new Promise((r) => (pr.resolve = r)); | ||
return pr as Promolve<T>; | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
function logStatus(ts: AsyncIter | NextSymbol | Callbacks | WaitAll) { | ||
const wa = ts.getCountWaiting(); | ||
const wo = ts.getCountWorking(); | ||
const rest = ts.getCountResolvedTotal(); | ||
const rejt = ts.getCountRejectedTotal(); | ||
const fint = ts.getCountFinishedTotal(); | ||
console.log(`wa:${wa},wo:${wo},rest:${rest},rejt:${rejt},fint:${fint}`); | ||
if (ts instanceof AsyncIter || ts instanceof NextSymbol) { | ||
const resnr = ts.getCountResolvedNotRead(); | ||
const rejnr = ts.getCountRejectedNotRead(); | ||
const finnr = ts.getCountFinishedNotRead(); | ||
console.log(`resnr:${resnr},rejnr:${rejnr},finnr:${finnr}`); | ||
} | ||
} | ||
async function task(id: any,ms: number,err=false){ | ||
async function task(id: number, ms: number, err = false): Promise<string> { | ||
console.log(`-->enter ${id}`); | ||
if (err) | ||
throw new Error(`task failed id=${id}`); | ||
if (err) throw new Error(`task failed id=${id}`); | ||
await snooze(ms); | ||
@@ -39,15 +44,17 @@ console.log(`<--leave ${id}`); | ||
} | ||
async function producer(ts:AsyncIter|NextSymbol|Callbacks|WaitAll){ | ||
for (let i=0; i<6; i++){ | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
async function producer( | ||
ts: AsyncIter | NextSymbol | Callbacks | WaitAll | ||
): Promise<void> { | ||
for (let i = 0; i < 6; i++) { | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
logStatus(ts); | ||
//--ENDIF | ||
ts.addTask(task,i,2**(10-i),(i+1)%3==0); | ||
//--ENDIF | ||
ts.addTask(task, i, 2 ** (10 - i), (i + 1) % 3 == 0); | ||
await snooze(100); | ||
} | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
//--IF{{RELEASE}} | ||
//--ELSE | ||
logStatus(ts); | ||
//--ENDIF | ||
//--ENDIF | ||
ts.addEnd(); | ||
@@ -57,15 +64,14 @@ console.log('producer finished'); | ||
export {snooze,task,range,makepr,producer} | ||
export {snooze, task, range, makepr, producer}; | ||
//--IF{{NODEJS}} | ||
function exitOnBeforeExit(exitCode: number){ | ||
process.on('beforeExit',async()=>{ | ||
if (typeof process.exitCode=='undefined'){ | ||
function exitOnBeforeExit(exitCode: number): void { | ||
process.on('beforeExit', async () => { | ||
if (typeof process.exitCode == 'undefined') { | ||
console.error('unexpected "beforeExit" event'); | ||
process.exit(exitCode); | ||
} else | ||
process.exit(process.exitCode); | ||
} else process.exit(process.exitCode); | ||
}); | ||
} | ||
export {exitOnBeforeExit} | ||
export {exitOnBeforeExit}; | ||
//--ENDIF |
@@ -8,45 +8,54 @@ 'use strict'; | ||
//--IF{{NODEJS}} | ||
import {exitOnBeforeExit,makepr,producer} from './demo-lib.js'; | ||
import {exitOnBeforeExit, makepr, producer} from './demo-lib.js'; | ||
//--ELSE | ||
//--import {makepr,producer} from './demo-lib.js'; | ||
//--ENDIF | ||
var somethingElse=makepr(); | ||
var iv=setInterval(()=>{somethingElse.resolve("somethingElse");},300); | ||
async function consumer(ts: NextSymbol){ | ||
let emptied=false; | ||
while(!emptied){ | ||
let next = await Promise.race([ | ||
somethingElse.promise, | ||
ts.nextSymbol(), | ||
]); | ||
switch(next){ | ||
case "somethingElse": | ||
console.log(next); | ||
somethingElse=makepr();// reset | ||
break; | ||
case ts.symbolTaskResolved():{ | ||
console.log(); | ||
let res=ts.getTaskResolvedValue(); | ||
console.log("symbolTaskResolved, result="+res); | ||
break;} | ||
case ts.symbolTaskRejected():{ | ||
let e=ts.getTaskRejectedValue(); | ||
console.log("symbolTaskRejected, message="+e.message); | ||
break;} | ||
case ts.symbolAllRead():{ | ||
console.log("symbolAllRead"); | ||
emptied=true; | ||
clearInterval(iv); | ||
break;} | ||
let somethingElse = makepr<string>(); | ||
const iv = setInterval(() => { | ||
somethingElse.resolve('somethingElse'); | ||
}, 300); | ||
async function consumer(ts: NextSymbol) { | ||
let emptied = false; | ||
while (!emptied) { | ||
const next = await Promise.race([somethingElse.promise, ts.nextSymbol()]); | ||
switch (next) { | ||
case 'somethingElse': | ||
console.log(next); | ||
somethingElse = makepr(); // reset | ||
break; | ||
case ts.symbolTaskResolved(): { | ||
console.log(); | ||
const res = ts.getTaskResolvedValue(); | ||
console.log('symbolTaskResolved, result=' + res); | ||
break; | ||
} | ||
case ts.symbolTaskRejected(): { | ||
const e = ts.getTaskRejectedValue(); | ||
console.log('symbolTaskRejected, message=' + e.message); | ||
break; | ||
} | ||
case ts.symbolAllRead(): { | ||
console.log('symbolAllRead'); | ||
emptied = true; | ||
clearInterval(iv); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
async function main(){ | ||
let ts=new NextSymbol({concurrentTaskLimit:2}); | ||
await Promise.all([consumer(ts),producer(ts)]); | ||
async function main() { | ||
const ts = new NextSymbol({concurrentTaskLimit: 2}); | ||
await Promise.all([consumer(ts), producer(ts)]); | ||
} | ||
//--IF{{NODEJS}} | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -53,0 +62,0 @@ //--ELSE |
@@ -9,37 +9,37 @@ 'use strict'; | ||
//--IF{{NODEJS}} | ||
import {exitOnBeforeExit,producer} from './demo-lib.js'; | ||
import {exitOnBeforeExit, producer} from './demo-lib.js'; | ||
//--ELSE | ||
//--import {producer} from './demo-lib.js'; | ||
//--ENDIF | ||
async function consumer_waitAll(ts: WaitAll){ | ||
try{ | ||
let r=await ts.waitAll(); | ||
async function consumer_waitAll(ts: WaitAll) { | ||
try { | ||
const r = await ts.waitAll(); | ||
console.log(`ts.waitAll() returned`); | ||
console.log(JSON.stringify(r,null,2)); | ||
}catch(e){ | ||
console.log(JSON.stringify(r, null, 2)); | ||
} catch (e) { | ||
console.log(`ts.waitAll() caught ${e.message}`); | ||
} | ||
} | ||
async function consumer_waitAllSettled(ts: WaitAll){ | ||
let r=await ts.waitAllSettled(); | ||
async function consumer_waitAllSettled(ts: WaitAll) { | ||
const r = await ts.waitAllSettled(); | ||
console.log(`ts.waitAllSettled() returned`); | ||
console.log(JSON.stringify(r,null,2)); | ||
console.log(JSON.stringify(r, null, 2)); | ||
console.log('consumer finished'); | ||
} | ||
async function main(){ | ||
let waitAll=new WaitAll({concurrentTaskLimit:2}); | ||
await Promise.all([ | ||
consumer_waitAll(waitAll), | ||
producer(waitAll), | ||
]); | ||
waitAll=new WaitAll({concurrentTaskLimit:2}); | ||
await Promise.all([ | ||
consumer_waitAllSettled(waitAll), | ||
producer(waitAll), | ||
]); | ||
async function main() { | ||
let waitAll = new WaitAll({concurrentTaskLimit: 2}); | ||
await Promise.all([consumer_waitAll(waitAll), producer(waitAll)]); | ||
waitAll = new WaitAll({concurrentTaskLimit: 2}); | ||
await Promise.all([consumer_waitAllSettled(waitAll), producer(waitAll)]); | ||
} | ||
//--IF{{NODEJS}} | ||
main() | ||
.then(()=>{console.log('success');process.exitCode=0;}) | ||
.catch((e)=>{console.log('failure '+e.message);process.exitCode=1;}); | ||
.then(() => { | ||
console.log('success'); | ||
process.exitCode = 0; | ||
}) | ||
.catch((e) => { | ||
console.log('failure ' + e.message); | ||
process.exitCode = 1; | ||
}); | ||
exitOnBeforeExit(2); | ||
@@ -46,0 +46,0 @@ //--ELSE |
74051
1454
13