voltrevo-event-loop

0.1.0 • Public • Published

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

Readme

Keywords

Package Sidebar

Install

npm i voltrevo-event-loop

Weekly Downloads

1

Version

0.1.0

License

MIT

Last publish

Collaborators

  • voltrevo