0.1.0 • Published 9 years ago

voltrevo-event-loop v0.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
9 years ago

voltrevo-event-loop NPM version Build Status Dependency Status Coverage percentage

An event loop abstraction.

Install

$ npm install --save voltrevo-event-loop

Usage

At the moment, this package basically implements setTimeout without using the global event loop. Control is given to you in the form of .run() and .runNext().

var EventLoop = require('voltrevo-event-loop');

var el = EventLoop();

el.post(function() {
  console.log('a');
}, 100);

el.post(function() {
  console.log('b');
});

el.post(function() {
  console.log('c');
});

// No output yet

el.runNext(); // b
el.runNext(); // c
el.runNext(); // a

// Or use el.run() to keep running until no events are left.

.run() will run all tasks that it can possibly see, which includes tasks that get added during .run(). It doesn't just run the tasks that have been posted before the call.

el.post(function() {
  el.post(function() {
    console.log('a');
  });
});

el.post(function() {
  console.log('b');
});

el.post(function() {
  console.log('c');
});

// No output yet

el.run(); // b, c, a

License

MIT © Andrew Morris

0.1.0

9 years ago