Create event emitters, intervals, and timeouts that can be paused and resumed.
Pauseable allows you to pause event emitters, timeouts, and intervals. It can easily group multiple of these pauseable objects and pause entire groups.
var pauseable = require'pauseable'EventEmitter = require'events'EventEmitter;var ee = ;eeon'foo' ;// ...laterpauseablepauseee;// this event will not be immediately fired// instead, it will be bufferedeeemit'foo' 'hellow';// ...much later// the 'foo' event will be fired at this pointpauseableresumeee;
var timeout = pauseablesetTimeout// this will take 8 seconds total to execute// not 55000;// pause timeout after 2 secssetTimeouttimeoutpause;timeoutisPaused; // true// resume after 3setTimeouttimeoutresume;3000;2000;
ms arguments are interchangeable. Use whichever way you prefer!
var interval = pauseablesetInterval5000// this is called after 5 seconds// then paused for 2 secondsintervalpause2000;;
// create a groupvar g = pauseablecreateGroup;// make and add emitters to groupvar ee1 = gadd;var ee2 = gadd;ee1on'forth'// pause entire group (that means ee1 and ee2) for 500 ms// timeout is out of the group by the time this executesgpause500;console.log'forth';ee2emit'back';;ee2on'back'console.log'back';ee1emit'forth';;var timeout = gsetTimeoutee2emit'back' 'poop';1000;
That's where this module comes in. Pauseable helps manage pausing and resuming your application or part of it. It works with EventEmitter and with setInterval and setTimeout.
Pauses an instance of EventEmitter. All events get buffered and emitted once the emitter is resumed. Currently only works with EventEmitters. Optional
ms will pause only for that long.
Resumes the emitter. Events can be emitted again. Optional
ms will resume only for that long.
Creates a pauseable timeout or interval.
ms are interchangeabale. Returns an instance of timer.
Pauses timer. Optional
ms will pause the timer only for that amount.
Resumes timer. Optional
ms will resume the timer only for that amount.
Returns the number of ms left until the
fn function in the constructor gets called again.
Clears timeout. Can no longer be resumed.
true if timer is currently paused.
true if timer was a timeout and
fn was called, or
timer.clear() has been called.
If timer is a timeout, this can be used to execute the
callback when the
fn in the constructor is called.
group where emitters and timers can be easily managed.
Add an emitter or a timer to the group. Returns added emitter/timer.
Shortcut to create an instance of a timer and add it to the group.
Pauses all emitters and timers in the group.
Resumes all emitters and timers in the group.
Clears timers in the group.
true is group is paused.
true if all timers currently in the group are timeouts and their original function has been called or all timers have been cleared.
Contains both emitters and timers. Useful if you want to micro manage more.
npm install pauseable
Tests are written with mocha