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

operations-js

v0.1.9

Published

Inspried by [Operation](https://developer.apple.com/documentation/foundation/operation) and [OperationQueue](https://developer.apple.com/documentation/foundation/operationqueue) classes from the iOS Framework.

Downloads

5

Readme

operations-js

Inspried by Operation and OperationQueue classes from the iOS Framework.

Classes

  • Operation
  • BlockOperation
  • OperationQueue
  • GroupOperation

Operation

const operation = new BlockOperation(6, async () => {
    return new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve();
            }, 1000);
        })
});

operation.start()

OperationQueue

const operationQueue = new OperationQueue();

const operation1 = new BlockOperation(1, async () => {
    [...]
});

const operation2 = new BlockOperation(2, async () => {
    [...]
});

operationQueue.addOperations([operation1, operation2]);

Utilizing OperationQueue's maximum concurrent operations

operationQueue.maximumConcurentOperations = 2;

Console:

// operation1 started
// operation2 started
// operation1 done
// operation2 done

Add dependencies

operation1.dependencies = [operation2];
operation1.start()

Console:

// operation2 started
// operation2 done
// operation1 started
// operation1 done

Extend the Operation class for complex tasks

class ValidateTokenOperation extends Operation {
    
    /**
     * Method to implement to run custom task
     * @override
     */
    run() {
        [...]
    }
    
}

Create a list of complex operations that represents your application

const validateToken = new ValidateTokenOperation();
const getUsersApi = new GetUsersApi();

getUsersApi.dependencies = [validateToken];
getUsersApi.completionCallback = operation => {
    // operation.result;
};
getUsersApi.start()
    .then(result => { ... })
    .catch(e => { ... });

Or set the dependency directly in the subclass:

class GetUsersApi extends Operation {
    constructor() {
        this.dependencies = [new ValidateTokenOperation()];
    }
}

Inserting operations in a queue to control flow

const blockOperation6 = new BlockOperation(6, async () => {
    const response = await axios.get('https://samples.openweathermap.org/data/2.5/weather?lat=35&lon=139&appid=b6907d289e10d714a6e88b30761fae22');
    const data = response.data;
    return data;
});

const operation = new DownloadDataOperation(1);
const operation2 = new DownloadDataOperation(2);
const operation3 = new DownloadDataOperation(3);
const operation4 = new TimeOutOperation(4, 2000);
const operation5 = new TimeOutOperation(5, 1000);
const operation7 = new TimeOutOperation(7, 8000);
const operation8 = new TimeOutOperation(8, 1500);

operation.dependencies = [operation2, operation7];
operation2.dependencies = [operation4, operation8];
operation3.dependencies = [operation, operation5, operation2];
operation4.dependencies = [operation5, blockOperation6];
operation5.dependencies = [blockOperation6];
operation8.dependencies = [operation7];

const operationQueue = new OperationQueue();
operationQueue.maximumConcurentOperations = 10;

operationQueue.completionCallback = () => {
    console.log('queue done');
};

operationQueue.addOperations([operation3])
    .then(result => {
        console.log(result)
    })
    .catch(e => {
        console.log(e)
    });

GroupOperation:

Group multiple operations and return the result of each operation when they are all resolved.

const groupOperation = new GroupOperation();

groupOperation.addOperation(new GetUsersApi());
groupOperation.addOperation(new GetPostsApi());

const [users, posts] = await groupOperation.start();