GithubHelp home page GithubHelp logo

complexflow's Introduction

complexflow

Complexflow helps you run complex async functions with multiple dependencies by building a dependency tree and running the async functions in the minimum time required.

Dependency Status Build Status

Installation

$ npm install --save complexflow

Basic Usage

import ComplexFlow from 'complexflow';

const cf = new ComplexFlow<any>(sharedObject, options);
cf.add({
    fn: async (a: any) => return a,
});
await cf.run();
console.log(sharedObject);

Example Usage

import ComplexFlow from 'complexflow';

interface SharedObject {
    one: number;
}

const calc1 = async (object: SharedObject) => {
    await sleep(50);
    object.one *= 2;
    return object;
};

const calc2 = async (object: SharedObject) => {
    await sleep(100);
    object.one += 10;
    return object;
};

// Just run in Parallel - Works just like await Promises.all(...)
const object = { one: 10 };
const cf = new ComplexFlow<SharedObject>(object);
cf.add({
    fn: calc1,
});
cf.add({
    fn: calc2,
});
await cf.run();

// object now is { one: 30 } like 10 * 2 + 10

// Run with Dependencies
const object = { one: 10 };
const cf = new ComplexFlow<SharedObject>(object);
cf.add({
    fn: calc1,
    depends: [calc2],
});
cf.add({
    fn: calc2,
});
await cf.run();

// object now is { one: 40 } like (10 + 10) * 2

Hints

As the complexflow-scheduler needs to be able to invoke the supplied functions on demand, it needs the functions as (not already invoked) parameters. All functions will receive the shared object as parameter. There are two modes for the scheduler to work in:

Non-restricted common access

In this mode all function work on the same object which is passed by reference to the functions. In this mode there may be concurrent access to data in the object. You need to take care of the concurrency by yourself. You create an instance for this mode with:

const cf = new ComplexFlow<any>(args, { detectConficts: false }); // false is default

Restricted common access

In this mode all function work a copies of the supplied object which is copied when the function is called by the scheduler. After the execution of an async function the result is merged back into the object. In this mode complexflow throws an exception when it detects concurrent access to an attribute of two different functions. Be aware, that the results of an function are only merged back into the object AFTER execution of the single function. You create a complexflow scheduler with concurrency detection with this:

const cf = new ComplexFlow<any>(args, { detectConficts: true });

License

MIT

Changelog

  • 0.2.4 - Extended example
  • 0.2.3 - Removed a bug in constructor for non-concurrent access
  • 0.2.2 - Updated documentation
  • 0.2.1 - Updated documentation
  • 0.2.0 - Added documentation and published to npm
  • 0.1.0 - Initial commit

complexflow's People

Contributors

speckm avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.