npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

op-queue

v1.1.1

Published

A simple module for operations in queue

Downloads

8

Readme

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