mirror of
https://github.com/flynx/guaranteeEvents.git
synced 2025-10-29 19:10:12 +00:00
fixed a small typo and added some basic docs...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
e133df7701
commit
d16ded7452
74
README.md
74
README.md
@ -1,2 +1,76 @@
|
|||||||
guaranteeEvents
|
guaranteeEvents
|
||||||
===============
|
===============
|
||||||
|
|
||||||
|
This module exports a single function that when passed an event(s) and an
|
||||||
|
[EventEmitter](http://nodejs.org/api/events.html#events_class_events_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.
|
||||||
|
|
||||||
|
This is useful for modules like [glob](https://github.com/isaacs/node-glob)
|
||||||
|
that use the [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)
|
||||||
|
model to pass data to the user (see examples below).
|
||||||
|
|
||||||
|
This is similar to how state change handlers work in
|
||||||
|
[jQuery.Deferred](http://api.jquery.com/category/deferred-object/) or in
|
||||||
|
[Promise](https://promisesaplus.com/) objects.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Install
|
||||||
|
-------
|
||||||
|
|
||||||
|
```
|
||||||
|
$ npm install guarantee-events
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Basic Examples
|
||||||
|
-------------
|
||||||
|
|
||||||
|
A synthetic example illustrating the basic functionality:
|
||||||
|
```javascript
|
||||||
|
|
||||||
|
var emitter = new (require('events').EventEmitter)
|
||||||
|
var guaranteeEvents = require('guarantee-events')
|
||||||
|
|
||||||
|
guaranteeEvents('event', emitter)
|
||||||
|
|
||||||
|
// emit some events...
|
||||||
|
emitter.emit('event', 'some data')
|
||||||
|
|
||||||
|
emitter.emit('event', 'some', 'more', 'data')
|
||||||
|
|
||||||
|
|
||||||
|
// Here the handler will be called for each event it missed...
|
||||||
|
emitter.on('event', function(){ console.log([].slice.apply(argumnts).join(' ')) })
|
||||||
|
|
||||||
|
|
||||||
|
emitter.emit('event', 'some data')
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
A real-life use-case:
|
||||||
|
```javascript
|
||||||
|
var glob = require('glob')
|
||||||
|
var guaranteeEvents = require('guarantee-events')
|
||||||
|
|
||||||
|
|
||||||
|
// build a glob object with cached 'match' and 'end' events...
|
||||||
|
var results = guaranteeEvents('match end', glob('**/*js'))
|
||||||
|
|
||||||
|
|
||||||
|
// Do stuff for some time...
|
||||||
|
|
||||||
|
|
||||||
|
// This will not miss a single result, regardless of how long it
|
||||||
|
// took to do stuff...
|
||||||
|
results.on('match', function(path){ console.log('found: '+path) })
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|||||||
14
index.js
14
index.js
@ -12,17 +12,17 @@
|
|||||||
// -> emitter
|
// -> emitter
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// This will add a .clearGuarantedQueue(..) method to the emitter that
|
// This will add a .clearGuaranteedQueue(..) method to the emitter that
|
||||||
// will clear the event queue for a specific event.
|
// will clear the event queue for a specific event.
|
||||||
//
|
//
|
||||||
// Clear event(s) queue(s)...
|
// Clear event(s) queue(s)...
|
||||||
// emitter.clearGuarantedQueue('event')
|
// emitter.clearGuaranteedQueue('event')
|
||||||
// emitter.clearGuarantedQueue('eventA eventB ...')
|
// emitter.clearGuaranteedQueue('eventA eventB ...')
|
||||||
// emitter.clearGuarantedQueue(['eventA', 'eventB', ...])
|
// emitter.clearGuaranteedQueue(['eventA', 'eventB', ...])
|
||||||
// -> emitter
|
// -> emitter
|
||||||
//
|
//
|
||||||
// Clear all queues...
|
// Clear all queues...
|
||||||
// emitter.clearGuarantedQueue('*')
|
// emitter.clearGuaranteedQueue('*')
|
||||||
// -> emitter
|
// -> emitter
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
@ -35,8 +35,8 @@ function(names, emitter){
|
|||||||
names = typeof(names) == typeof('str') ? names.split(/\s+/g) : names
|
names = typeof(names) == typeof('str') ? names.split(/\s+/g) : names
|
||||||
|
|
||||||
// add ability to clear the queue...
|
// add ability to clear the queue...
|
||||||
if(emitter.clearGuarantedQueue == null){
|
if(emitter.clearGuaranteedQueue == null){
|
||||||
emitter.clearGuarantedQueue = function(names){
|
emitter.clearGuaranteedQueue = function(names){
|
||||||
names = names == '*' ? Object.keys(this._guaranteed_queue)
|
names = names == '*' ? Object.keys(this._guaranteed_queue)
|
||||||
: typeof(names) == typeof('str') ? names.split(/\s+/g)
|
: typeof(names) == typeof('str') ? names.split(/\s+/g)
|
||||||
: names
|
: names
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user