routes-utility
Advanced tools
Comparing version 2.1.4 to 2.1.5
@@ -39,11 +39,2 @@ export interface IDoneFn<FINAL_RES> { | ||
} | ||
export declare class Queue<T> { | ||
private _offset; | ||
private _queue; | ||
readonly length: number; | ||
dequeue(): T; | ||
enqueue(item: T, isUrgent: boolean): this; | ||
isEmpty(): boolean; | ||
peek(): T; | ||
} | ||
export declare class Route<CONTEXT, INIT_REQ, FINAL_RES = INIT_REQ> { | ||
@@ -67,3 +58,14 @@ private _maxParallel; | ||
match(req: INIT_REQ, context: CONTEXT, onProgress?: (progression: IProgression<Controller<CONTEXT, INIT_REQ, any, any, FINAL_RES> | Route<CONTEXT, INIT_REQ, FINAL_RES>>) => void, isUrgent?: boolean, stepTimeoutMS?: number, stepTimeoutHandler?: (p: IProgression<any>) => void): Promise<FINAL_RES>; | ||
/** | ||
* Set max parallel requests | ||
* @param {number} n (null or < 0 = unlimited) | ||
* @returns {this} | ||
*/ | ||
setMaxParallel(n: number): this; | ||
/** | ||
* Set timeout handler to process each step, by default handler throws a timeout error | ||
* @param {number} ms (null or <= 0 = unlimited) | ||
* @param {() => void} handler | ||
* @returns {this} | ||
*/ | ||
setStepTimeout(ms: number, handler?: () => void): this; | ||
@@ -70,0 +72,0 @@ private _match; |
@@ -13,2 +13,4 @@ "use strict"; | ||
const sw_logger_1 = require("sw-logger"); | ||
const Queue_1 = require("./Queue"); | ||
// Controller | ||
class Controller { | ||
@@ -30,36 +32,3 @@ constructor(fn, name) { | ||
exports.Controller = Controller; | ||
class Queue { | ||
constructor() { | ||
this._offset = 0; | ||
this._queue = []; | ||
} | ||
get length() { | ||
return this._queue.length - this._offset; | ||
} | ||
dequeue() { | ||
if (this._queue.length === 0) { | ||
return undefined; | ||
} | ||
const item = this._queue[this._offset]; | ||
if (++this._offset * 2 >= this._queue.length) { | ||
this._queue = this._queue.slice(this._offset); | ||
this._offset = 0; | ||
} | ||
return item; | ||
} | ||
enqueue(item, isUrgent) { | ||
const lastId = this._queue.push(item) - 1; | ||
if (isUrgent) { | ||
[this._queue[lastId], this._queue[this._offset]] = [this._queue[this._offset], this._queue[lastId]]; | ||
} | ||
return this; | ||
} | ||
isEmpty() { | ||
return this._queue.length === 0; | ||
} | ||
peek() { | ||
return (this._queue.length > 0 ? this._queue[this._offset] : undefined); | ||
} | ||
} | ||
exports.Queue = Queue; | ||
// Route | ||
class Route { | ||
@@ -86,3 +55,3 @@ constructor(name, ...controllers) { | ||
_.each(controllers || [], (c) => this.add(c)); | ||
this._onHoldQueue = new Queue(); | ||
this._onHoldQueue = new Queue_1.Queue(); | ||
this._stepTimeoutHandler = (p) => { | ||
@@ -154,8 +123,10 @@ throw new sw_logger_1.CustomError('timeout', 'step timed out', 408, 'fatal', { progression: p }); | ||
return new Promise((resolve, reject) => { | ||
this._onHoldQueue.enqueue({ | ||
// Put in queue | ||
this._onHoldQueue.add({ | ||
runner: () => __awaiter(this, void 0, void 0, function* () { | ||
this._parallel++; | ||
let stepTimer = undefined; | ||
try { | ||
let onProgressFn = onProgress; | ||
if (Number.isFinite(stepTimeoutMS) && stepTimeoutMS > 0) { | ||
if (Number.isFinite(stepTimeoutMS) && stepTimeoutMS > 0) { // wrap onProgress | ||
let lastProgression = null; | ||
@@ -170,5 +141,6 @@ const handler = () => { | ||
}; | ||
let t = setTimeout(handler, stepTimeoutMS); | ||
stepTimer = setTimeout(handler, stepTimeoutMS); | ||
onProgressFn = (progression) => { | ||
clearTimeout(t); | ||
clearTimeout(stepTimer); | ||
stepTimer = undefined; | ||
lastProgression = progression; | ||
@@ -178,3 +150,3 @@ if (onProgress != null) { | ||
} | ||
t = setTimeout(handler, stepTimeoutMS); | ||
stepTimer = setTimeout(handler, stepTimeoutMS); | ||
}; | ||
@@ -189,2 +161,4 @@ } | ||
finally { | ||
clearTimeout(stepTimer); | ||
stepTimer = undefined; | ||
this._parallel--; | ||
@@ -199,2 +173,7 @@ this._run(); | ||
} | ||
/** | ||
* Set max parallel requests | ||
* @param {number} n (null or < 0 = unlimited) | ||
* @returns {this} | ||
*/ | ||
setMaxParallel(n) { | ||
@@ -205,2 +184,8 @@ this._maxParallel = n; | ||
} | ||
/** | ||
* Set timeout handler to process each step, by default handler throws a timeout error | ||
* @param {number} ms (null or <= 0 = unlimited) | ||
* @param {() => void} handler | ||
* @returns {this} | ||
*/ | ||
setStepTimeout(ms, handler) { | ||
@@ -243,7 +228,8 @@ this._stepTimeout = ms; | ||
} | ||
else if (index === this._steps.length - 1) { | ||
else if (index === this._steps.length - 1) { // finish | ||
finish(res); | ||
} | ||
else { | ||
if (step != null) { | ||
// Define next step | ||
if (step != null) { // Jump to a specific step | ||
while (step !== currentStep && step !== currentStep.name && index < this._steps.length - 1) { | ||
@@ -289,3 +275,3 @@ index++; | ||
catch (e) { | ||
next(e); | ||
next(e); // is similar to finish(err) | ||
} | ||
@@ -317,3 +303,3 @@ }))(); | ||
while (this._parallel < maxParallel && this._onHoldQueue.length > 0) { | ||
this._onHoldQueue.dequeue().runner(); | ||
this._onHoldQueue.next().runner(); | ||
} | ||
@@ -320,0 +306,0 @@ } |
@@ -7,3 +7,3 @@ { | ||
}, | ||
"version": "2.1.4", | ||
"version": "2.1.5", | ||
"scripts": { | ||
@@ -29,2 +29,3 @@ "test": "./node_modules/.bin/mocha --opts tests/mocha.opts tests/**/*.ts", | ||
"@types/mocha": "^5.2.4", | ||
"@types/node": "^10.11.0", | ||
"chai": "^4.1.2", | ||
@@ -31,0 +32,0 @@ "mocha": "^5.2.0", |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
29950
8
468
0
11