op-queue
v1.1.1
Published
A simple module for operations in queue
Downloads
8
Maintainers
Readme
op-queue
A simple module for operations in queue.
Push operations to queue, they will execute one by one synchronous.
Support async
functions or functions returned Promise
.
Intall
npm install op-queue --save
Usage
const OpQueue = require('op-queue').default;
let myQ = new OpQueue();
function promiseOperation(...args) {
return new Promise((resolve: any, reject: any) => {
// ...
});
}
async function asyncOperation(...args) {
// ...
}
let myPromiseOp = OpQueue.buildOperation(promiseOperation);
let myAsyncOp = OpQueue.buildOperation(asyncOperation);
myQ.push(myPromiseOp(...args), (err. data) => {
// ... handle returns of function "promiseOperation"
});
myQ.push(myAsyncOp(...args), (err, data) => {
// ... handle returns of function "asyncOperation"
});
Events
done
myQ.on('done', (data) => {
console.log('event.done:', data);
});
op_error
myQ.on('op_error', (error) => {
console.log('event.error:', error);
});
API
const OpQueue = require('op-queue').default;
let myQ = new OpQueue(opt);
opt:
manualStart
: boolean, default false. If set true, operations pushed in queue will not execute until you callstart()
method. Use this when you want to push all operations in queue then start from first one and execute one by one.
OpQueue.buildOperation(fn)
fn
:async
function or function returnedPromise
length
let l = myQ.length
Get length of operations pending in queue (exclude the operation in processing)
push(op, callback)
Push operation to queue
op
value returned byOpQueue.buildOperation(fn)
callback
returns of op, optional, (error, data) => {}
start()
Start execute operations in queue. (only work when option manualStart
is true)
const OpQueue = require('op-queue').default;
let myQ = new OpQueue({ manualStart: true });
myQ.push(...);
myQ.push(...);
myQ.push(...);
// operations will start execute after call start() method
myQ.start();
Example
typescript example:
import OpQueue from 'op-queue';
let myQ = new OpQueue();
myQ.on('done', (p: any) => {
console.log('event.done:', p);
});
myQ.on('op_error', (e: any) => {
console.log('event.error:', e);
});
async function sleep(ms: number) {
return new Promise((resolve: any, reject: any) => {
setTimeout(resolve, ms);
});
}
function promiseOperation(a: any) {
return new Promise((resolve: any, reject: any) => {
resolve({a: a});
});
}
let myPromiseOp = OpQueue.buildOperation(promiseOperation);
async function asyncOperation(ms: number) {
console.log('sleep start');
await sleep(ms);
console.log('sleep done');
return ms;
}
let myAsyncOp = OpQueue.buildOperation(asyncOperation);
myQ.push(myAsyncOp(2000));
myQ.push(myPromiseOp(222));
myQ.push(myAsyncOp(3000));
myQ.push(myPromiseOp(333), (error: any, p: any) => {
if (error) {
console.log('cb.error', error);
} else {
console.log('cb.done', p);
}
});
// output should be:
// sleep start
// sleep done
// event.done: 2000
// event.done: { a: 222 }
// sleep start
// sleep done
// event.done: 3000
// event.done: { a: 333 }
// cb.done { a: 333 }
Test
npm test