This module exports a single function that when passed an event(s) and an EventEmitter compatible object will register a couple of handlers that will provide the following functionality:
Cache event data for each event of the given type that gets emitted.
Call new handlers of the specified event with each of the prior event data sets in order of event occurrence.
.clearGuaranteedQueue(<event>)method to the emitter to facilitate event cache cleaning.
$ npm install guarantee-events
A synthetic example illustrating the basic functionality:
var emitter = new EventEmittervar guaranteeEvents =// emit some events...emitteremitter// Here the handler will be called for each event it missed...emitteremitter
A real-life use-case using the excellent glob utility:
var glob =var guaranteeEvents =// build a glob object with cached 'match' and 'end' events...var results =// Do stuff for some time...// This will not miss a single result, regardless of how long it// took to do stuff...results
Cache cleaning and use for long running emitters
One of the dangers in using this in long running event emitters is cache buildup -- the data for each event emitted will get stored and this might get quite large, this, if not managed, is a potential memory leak.
To deal with this issue a
.clearGuaranteedQueue(<event>) method is
added to the emitter, this will clear the cache for a specific event.
This and has a shorthand form
.clearGuaranteedQueue('*') that will
clear the cache for all wrapped events.
So for the above example:
// This this will drop all the prior match data, so newly registred handlers// will not see them...// NOTE: this will not affect the underlaying glob object in any way.results