🚀 Big News:Socket Has Acquired Secure Annex.Learn More
Socket
Book a DemoSign in
Socket

SimpleQueue

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

SimpleQueue - npm Package Compare versions

Comparing version
0.0.1
to
1.0.0
+48
lib/index.d.ts
/**
* A simple FIFO queue, delivering items in order.
*
* Paremeters:
* `T`: Type that is pushed onto the stack.
* `R`: Type that the passed `callback` maps to.
*/
export default class SimpleQueue<T, R = void> {
private readonly worker;
private readonly callback;
private readonly done?;
private readonly concurrent;
private queue;
/** Stores elements that are finished. */
private stack;
private working;
private lastStarted;
private finished;
paused: boolean;
/**
* Creates a new FIFO queue.
*
* @param worker Method to call for each child. Args:
* @param callback Method to call when an element was processed.
* @param done Method to call once the stack is cleared.
* @param concurrent Number of elements to process in parallel. Defaults to 20.
*/
constructor(worker: (element: T, callback: (error: Error | null, result: R) => void) => void, callback: (error: Error | null, result: R, element: T) => void, done?: (() => void) | undefined, concurrent?: number);
/** Adds an element to the queue. */
push(props: T): void;
/**
* Clears the queue (can't stop running processes).
*/
abort(): void;
/**
* Pause the queue execution.
* Will not stop already in-flight items.
*/
pause(): void;
/**
* Resume the queue execution,
* and catch up with remaining items.
*/
resume(): void;
private checkStack;
private scan;
}
//# sourceMappingURL=index.d.ts.map
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,OAAO,WAAW,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI;IAoBpC,OAAO,CAAC,QAAQ,CAAC,MAAM;IAIvB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAKzB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAC;IACtB,OAAO,CAAC,QAAQ,CAAC,UAAU;IA7B/B,OAAO,CAAC,KAAK,CAAW;IACxB,yCAAyC;IACzC,OAAO,CAAC,KAAK,CAEN;IACP,OAAO,CAAC,OAAO,CAAK;IACpB,OAAO,CAAC,WAAW,CAAK;IACxB,OAAO,CAAC,QAAQ,CAAK;IACd,MAAM,UAAS;IAEtB;;;;;;;OAOG;gBAEkB,MAAM,EAAE,CACrB,OAAO,EAAE,CAAC,EACV,QAAQ,EAAE,CAAC,KAAK,EAAE,KAAK,GAAG,IAAI,EAAE,MAAM,EAAE,CAAC,KAAK,IAAI,KACjD,IAAI,EACQ,QAAQ,EAAE,CACvB,KAAK,EAAE,KAAK,GAAG,IAAI,EACnB,MAAM,EAAE,CAAC,EACT,OAAO,EAAE,CAAC,KACT,IAAI,EACQ,IAAI,CAAC,SAAQ,IAAI,aAAA,EACjB,UAAU,SAAK;IAGpC,oCAAoC;IAC7B,IAAI,CAAC,KAAK,EAAE,CAAC,GAAG,IAAI;IAI3B;;OAEG;IACI,KAAK,IAAI,IAAI;IAKpB;;;OAGG;IACI,KAAK,IAAI,IAAI;IAGpB;;;OAGG;IACI,MAAM,IAAI,IAAI;IAMrB,OAAO,CAAC,UAAU;IAYlB,OAAO,CAAC,IAAI;CA2Bf"}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* A simple FIFO queue, delivering items in order.
*
* Paremeters:
* `T`: Type that is pushed onto the stack.
* `R`: Type that the passed `callback` maps to.
*/
var SimpleQueue = /** @class */ (function () {
/**
* Creates a new FIFO queue.
*
* @param worker Method to call for each child. Args:
* @param callback Method to call when an element was processed.
* @param done Method to call once the stack is cleared.
* @param concurrent Number of elements to process in parallel. Defaults to 20.
*/
function SimpleQueue(worker, callback, done, concurrent) {
if (concurrent === void 0) { concurrent = 20; }
this.worker = worker;
this.callback = callback;
this.done = done;
this.concurrent = concurrent;
this.queue = [];
/** Stores elements that are finished. */
this.stack = {};
this.working = 0;
this.lastStarted = 0;
this.finished = 0;
this.paused = false;
}
/** Adds an element to the queue. */
SimpleQueue.prototype.push = function (props) {
this.queue.push(props);
this.scan();
};
/**
* Clears the queue (can't stop running processes).
*/
SimpleQueue.prototype.abort = function () {
this.queue.length = 0;
this.paused = true; // `cb` won't be called any more
};
/**
* Pause the queue execution.
* Will not stop already in-flight items.
*/
SimpleQueue.prototype.pause = function () {
this.paused = true;
};
/**
* Resume the queue execution,
* and catch up with remaining items.
*/
SimpleQueue.prototype.resume = function () {
this.paused = false;
this.scan();
this.checkStack();
};
SimpleQueue.prototype.checkStack = function () {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (this.stack[this.finished]) {
this.callback.apply(this, this.stack[this.finished]);
delete this.stack[this.finished];
this.finished += 1;
}
if (this.working === 0 && this.queue.length === 0 && this.done) {
this.done();
}
};
SimpleQueue.prototype.scan = function () {
var _this = this;
if (this.working === this.concurrent ||
this.queue.length === 0 ||
this.paused) {
return;
}
var element = this.queue.shift();
var index = this.lastStarted++;
this.working++;
this.worker(element, function (err, result) {
_this.working--;
if (!_this.paused && index === _this.finished) {
_this.callback(err, result, element);
_this.finished += 1;
_this.checkStack();
}
else {
_this.stack[index] = [err, result, element];
}
_this.scan();
});
};
return SimpleQueue;
}());
exports.default = SimpleQueue;
MIT License
Copyright (c) 2018 Felix Boehm
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# SimpleQueue
A simple FIFO queue
npm install SimpleQueue
## What is this?
There are plenty queues for node, but even those branded as FIFO (first in first out) usually destroy the order.
Eg. when mapping over an RSS feeds & doing something with all of the pages,
you need to know what element had what position - so I created this little helper.
## API
### Class: SimpleQueue\<T, R>
A simple FIFO queue, delivering items in order.
#### Type parameters
| Name | Default | Description |
| ---- | ------- | ---------------------------------------- |
| `T` | - | Type that is pushed onto the stack. |
| `R` | void | Type that the passed `callback` maps to. |
### Constructors
#### constructor
\+ **new SimpleQueue**(`worker`: (element: T, callback: (error: Error \| null, result: R) => void) => void, `callback`: (error: Error \| null, result: R, element: T) => void, `done?`: undefined \| () => void, `concurrent?`: number): `SimpleQueue`
_Defined in [index.ts:16](https://github.com/fb55/SimpleQueue/blob/master/src/index.ts#L16)_
Creates a new FIFO queue.
##### Parameters:
| Name | Type | Default value | Description |
| ------------ | ------------------------------------------------------------------------- | ------------- | ---------------------------------------------------------- |
| `worker` | (element: T, callback: (error: Error \| null, result: R) => void) => void | - | Method to call for each child. Args: |
| `callback` | (error: Error \| null, result: R, element: T) => void | - | Method to call when an element was processed. |
| `done?` | undefined \| () => void | - | Method to call once the stack is cleared. |
| `concurrent` | number | 20 | Number of elements to process in parallel. Defaults to 20. |
**Returns:** `SimpleQueue`
### Properties
#### paused
• **paused**: boolean = false
_Defined in [index.ts:16](https://github.com/fb55/SimpleQueue/blob/master/src/index.ts#L16)_
### Methods
#### abort
▸ **abort**(): void
_Defined in [index.ts:48](https://github.com/fb55/SimpleQueue/blob/master/src/index.ts#L48)_
Clears the queue (can't stop running processes).
**Returns:** void
---
#### pause
▸ **pause**(): void
_Defined in [index.ts:57](https://github.com/fb55/SimpleQueue/blob/master/src/index.ts#L57)_
Pause the queue execution.
Will not stop already in-flight items.
**Returns:** void
---
#### push
▸ **push**(`props`: T): void
_Defined in [index.ts:41](https://github.com/fb55/SimpleQueue/blob/master/src/index.ts#L41)_
Adds an element to the queue.
##### Parameters:
| Name | Type |
| ------- | ---- |
| `props` | T |
**Returns:** void
---
#### resume
▸ **resume**(): void
_Defined in [index.ts:64](https://github.com/fb55/SimpleQueue/blob/master/src/index.ts#L64)_
Resume the queue execution,
and catch up with remaining items.
**Returns:** void
## Example
```js
import SimpleQueue from "SimpleQueue";
const queue = new SimpleQueue(
(element, callback) => {
// Set
setTimeout(() => callback(null, element / 1000), element);
},
(err, result, element) => {
console.log(result);
},
() => {
console.log("done");
},
4
);
queue.push(1000);
queue.push(5000);
queue.push(3000);
queue.push(4000);
queue.push(8000);
queue.push(2000);
queue.push(0);
```
Output:
1, 5, 3, 4, 8, 2, 0, "done"
This takes 9 seconds to run.
+48
-7
{
"name": "SimpleQueue",
"version": "0.0.1",
"license": "MIT",
"version": "1.0.0",
"description": "a simple fifo queue",
"author": "Felix Boehm <me@feedic.com>",
"main": "./SimpleQueue.js",
"funding": "https://github.com/sponsors/fb55",
"sideEffects": false,
"main": "lib/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib/"
},
"files": [
"lib/**/*"
],
"repository": {
"type": "git"
, "url": "git://github.com/fb55/simplequeue.git"
},
"type": "git",
"url": "git://github.com/fb55/simplequeue.git"
},
"scripts": {
"test": "node ./tests/00-basic.js"
"test": "jest --coverage && npm run lint",
"coverage": "cat coverage/lcov.info | coveralls",
"lint": "npm run lint:es && npm run lint:prettier",
"lint:es": "eslint .",
"lint:prettier": "npm run prettier -- --check",
"format": "npm run format:es && npm run format:prettier",
"format:es": "npm run lint:es -- --fix",
"format:prettier": "npm run prettier -- --write",
"prettier": "prettier '**/*.{js,ts,md,json,yml}'",
"build": "tsc",
"prepare": "npm run build"
},
"jest": {
"preset": "ts-jest",
"testEnvironment": "node"
},
"prettier": {
"tabWidth": 4
},
"devDependencies": {
"@types/jest": "^26.0.0",
"@types/node": "^14.11.8",
"@typescript-eslint/eslint-plugin": "^4.4.1",
"@typescript-eslint/parser": "^4.4.1",
"coveralls": "*",
"eslint": "^7.11.0",
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-node": "^11.1.0",
"jest": "^26.5.3",
"prettier": "^2.0.5",
"ts-jest": "^26.1.0",
"typescript": "^4.0.2"
}
}
}
-48
#SimpleQueue
A simple FIFO queue
##What is this?
There are plenty queues for node, but even those branded as FIFO (first in first out) usually destroy the order. When parsing data like RSS feeds & fetching the pages behind the links, you need to know what element had what position - so I created this little helper (mainly to process feeds with my script [readabilitySAX](https://github.com/fb55/readabilitysax)).
##How to use
Constructor:
new SimpleQueue(<func> worker, <func> callback[, <func> done[, <num> concurrent]])
Methods:
queue.push(<any> element) //adds an element to the list
Methods to include:
* `worker`: The method to call for each child. Args: `element`, `callback(err, result)`
* `callback`: The method to call when an element was processed. Args: `err` and `result` (whatever the worker returned), `element` (the input)
* `done`: The method to call once the stack is cleared. Args: none
##Example
var queue = new SimpleQueue(function(element, callback){
setTimeout(function(){
callback(null, element/1e3);
}, element);
}, function(err, result, element){
console.log(result);
}, function(){
console.log("done");
}, 4);
queue.push(1e3);
queue.push(5e3);
queue.push(3e3);
queue.push(4e3);
queue.push(8e3);
queue.push(2e3);
queue.push(0);
Output:
1, 5, 3, 4, 8, 2, 0, "done"
This takes 9 seconds to run.
var SimpleQueue = function(worker, callback, done, concurrent){
this._concurrent = concurrent || 20;
this._worker = worker;
this._callback = callback;
this._done = done;
this._queue = [];
this._stack = {}; //stores elements that are finished
this._working = 0;
this._lastStarted = 0;
this._finished = 0;
};
SimpleQueue.prototype.push = function(props){
this._queue.push(props);
this._scan();
};
SimpleQueue.prototype._scan = function(){
if( this._working === this._concurrent ||
this._queue.length === 0) return;
var element = this._queue.shift(),
index = this._lastStarted++,
that = this;
this._working++;
this._worker(element, function(err, result){
that._working--;
if(index === that._finished){
that._callback(err, result, element);
while(that._stack[++that._finished]){
that._callback.apply(that, that._stack[that._finished]);
that._stack[that._finished] = null;
}
if(that._working === 0 && that._queue.length === 0 && that._done){
that._done();
}
}
else that._stack[index] = [err, result, element];
that._scan();
});
};
module.exports = SimpleQueue;
var SimpleQueue = require("..");
var results = [],
started = Date.now(),
expectedResults = [
{ err: null, result: 1, element: 1000 },
{ err: null, result: 5, element: 5000 },
{ err: null, result: 3, element: 3000 },
{ err: null, result: 4, element: 4000 },
{ err: null, result: 8, element: 8000 },
{ err: null, result: 2, element: 2000 },
{ err: null, result: 0, element: 0 }
];
var queue = new SimpleQueue(function(element, callback){
setTimeout(function(){ callback(null,element/1e3); }, element);
}, function(err, result, element){
results.push({err:err, result:result, element:element});
}, function(){
if(JSON.stringify(expectedResults) === JSON.stringify(results)){
console.log("passed, took", Date.now()-started);
}
else {
throw Error("undexpected output, got: " + JSON.stringify(results));
}
}, 4);
expectedResults.forEach(function(elem){
queue.push(elem.element);
});
/*queue.push(1e3);
queue.push(5e3);
queue.push(3e3);
queue.push(4e3);
queue.push(8e3);
queue.push(2e3);
queue.push(0);*/