Giter Club home page Giter Club logo

sequencer.js's Introduction

sequencer.js

A simple but powerful and extensible JavaScript task sequencer.

Installation

Using NPM

npm install --save sequencer.js

Usage

// Create a handle and release it after some time has passed.
// The sequence will block at `doWaitForHandle(blockUntilLaterHandle)` until the handle is released.
var blockUntilLaterHandle = new Handle();
setTimeout(blockUntilLaterHandle.release, 10000);

var sequencer = new Sequencer();

// Enqueue a simple synchronous action
sequencer.do(() => log("1st instantly"));

// Waits for one second then performs an action after the delay has elapsed.
// This also demonstrates "do" task chaining.
sequencer.doWait(1000).do(() => log("2nd after 1 second"));

// Performs an action and waits until release() is called
sequencer.doWaitForRelease(release => setTimeout(release, 3000));

sequencer.do(() => log("3rd after waiting for a release() call"));

// Block until the handle is released
sequencer.doWaitForHandle(blockUntilLaterHandle);

sequencer.do(() => log("4th after waiting for a manually-created handle to be released"));

// Performs an action and waits until release() is called a certain number of times.
// The sequencer proceeds after 5 seconds (when two releases have been performed).
sequencer.doWaitForReleases(2, release => {
  setTimeout(release, 5000);
  setTimeout(release, 3000);
});

sequencer.do(() => log("5th after waiting for two release() calls"));

// Wait for a promise to be fulfilled.
// You can optionally obtain the promise's value and/or rejection reason.
var url = "https://cors-test.appspot.com/test";
sequencer.doWaitForPromise(fetch(url), response => log(`(Promise Resolved : HTTP ${response.status} from ${url})`));

sequencer.do(() => log("6th after waiting for a promise to be resolved"));

sequencer.doSequence(s => {
  s.doWait(1000);
  s.do(() => log(`> Subsequence : First`));
  s.doWait(1000);
  s.do(() => log(`> Subsequence : Second`));
  s.doWait(1000);
  s.do(() => log(`> Subsequence : Third`));
  s.doWait(1000);
});

sequencer.do(() => log("7th after waiting for a subsequence to complete"));

Creating an Extension

An external sequencer.js extension looks like this:

var DoSomethingTask = function (someValue) {
    this.perform = function (handle) {
        // Do something, synchronously or asynchronously
        setTimeout(() => {
          console.log("Task performed after 2 seconds with value " + someValue);
          handle.release(); // Then release the handle to indicate that the task is complete
        }, 2000);
    };
};

Sequencer.prototype.doSomething = function (someValue) {
    this.push(new DoSomethingTask(someValue));
    return this;
}

Take a look at core extensions in the sequencer.js source for concrete examples. The way the extensions are installed is slightly different from external extensions, but they still serve as appropriate examples.

sequencer.js's People

Contributors

fortinmike avatar

Stargazers

 avatar François Beaulieu avatar changfuguo avatar timelyportfolio avatar

Watchers

 avatar timelyportfolio avatar James Cloos avatar François Beaulieu 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.