Socket
Book a DemoInstallSign in
Socket

op-queue

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

op-queue

A simple module for operations in queue

1.1.1
latest
Source
npmnpm
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

op-queue

Build Status Coverage Status Dependency Status

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 call start() 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 returned Promise

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 by OpQueue.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

Keywords

operation

FAQs

Package last updated on 31 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.