Coder Social home page Coder Social logo

action-queue's Introduction

Action Queue

Queues up actions for control over when they run.

Usage

  1. Create a queue object.

     var ActionQueue = require('action-queue');
     var queue = new ActionQueue();
    
  2. Add actions to it.

     queue.addAction(function(){
       // Action does some stuff...
       console.log('Doing stuff...', arguments);
     }, ['arg1', 'arg2']);
    
     queue.addAction(function(){
       // Another action does some other stuff...
       console.log('Doing more stuff...', arguments);
     });
    
  3. Bundle those actions up into a "step". A step is a series of actions.

     queue.endStep();
    
  4. Build more steps if you want. For each step you want to build, add more actions, then call endStep().

     queue.addAction(function(){
       // This action runs in step 2.
       console.log('Doing next step stuff...', arguments);
     });
     queue.endStep();
     queue.addAction(function(){
       // This action runs in step 2.
       console.log('Step 3 stuff...', arguments);
     });
     queue.endStep();
    
  5. Run your actions! runNextStep() runs the next step in the queue (which means each action in that step will execute in order).

     queue.runNextStep();
    
     // The following new content is displayed in the console at this point:
     //  Doing stuff... { '0': 'arg1', '1': 'arg2' }
     //  Doing more stuff... {}
    
  6. If you call wait(), any subsequent calls to runNextStep() do nothing.

     queue.wait();
    
  7. If you call onComplete() and pass in a callback, it will be called when complete() is called or when runNextStep() finishes running all the next step's actions.

     queue.onComplete(function(){
       console.log("Complete event triggered!");
     });
    
  8. Since we're waiting now, this call has no effect.

     queue.runNextStep();
    
  9. Stop waiting!

     queue.complete();
    
     // The following new content is displayed in the console at this point:
     //  Complete event triggered!
    
     queue.runNextStep();
    
     // The following new content is displayed in the console at this point:
     //  Doing next step stuff... {}
     //  Complete event triggered!
    
     queue.runNextStep();
    
     // The following new content is displayed in the console at this point:
     //  Step 3 stuff... {}
     //  Complete event triggered!
    

All our steps have been run and flushed out of the queue at this point.

action-queue's People

Contributors

apent2012 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.