Compare commits

...

15 Commits

Author SHA1 Message Date
ae560cd00a updated year
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2023-09-09 16:39:03 +03:00
541f4bfe82 added .editorconfig
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2023-06-12 12:47:28 +03:00
9be9caefc9 updated license...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2019-11-03 19:28:07 +03:00
3572df122e 1.0.6 2014-12-31 19:23:50 +03:00
bf9674d063 added a link to the used in an example glob utility...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2014-12-31 19:23:36 +03:00
7b16dc493f minor licence update in package.json...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2014-12-30 00:07:22 +03:00
32a8826587 1.0.5 2014-12-29 22:59:39 +03:00
01ca45aec3 minor doc rewrite...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2014-12-29 22:59:27 +03:00
b068e0b49a fixed a typo...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2014-12-29 22:55:02 +03:00
367f85371f 1.0.4 2014-12-29 22:50:04 +03:00
f1373a167b some refactoring...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2014-12-29 22:47:05 +03:00
c764dcf6ad 1.0.3 2014-12-29 22:24:15 +03:00
93a5d86492 minor documentation changes...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2014-12-29 22:24:08 +03:00
e68fa30335 1.0.2 2014-12-29 22:20:43 +03:00
e02f6f3742 documented the .clearGuaranteedQueue(..) method...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2014-12-29 22:19:49 +03:00
5 changed files with 65 additions and 23 deletions

7
.editorconfig Executable file
View File

@ -0,0 +1,7 @@
root = true
[**]
indent_style = tab
tab_width = 4
charset = utf-8
end_of_line = lf

2
LICENSE Normal file → Executable file
View File

@ -1,4 +1,4 @@
Copyright (c) 2014, Alex A. Naanou Copyright (c) 2014-2023, Alex A. Naanou
All rights reserved. All rights reserved.
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without

28
README.md Normal file → Executable file
View File

@ -11,6 +11,9 @@ will provide the following functionality:
* Call new handlers of the specified event with each of the prior event * Call new handlers of the specified event with each of the prior event
data sets in order of event occurrence. data sets in order of event occurrence.
* Add a `.clearGuaranteedQueue(<event>)` method to the emitter to facilitate
event cache cleaning.
This is useful for modules like [glob](https://github.com/isaacs/node-glob) 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) that use the [EventEmitter](http://nodejs.org/api/events.html#events_class_events_eventemitter)
model to pass data to the user (see examples below). model to pass data to the user (see examples below).
@ -55,7 +58,8 @@ emitter.emit('event', 'some data')
``` ```
A real-life use-case: A real-life use-case using the excellent [glob](https://github.com/isaacs/node-glob)
utility:
```javascript ```javascript
var glob = require('glob') var glob = require('glob')
var guaranteeEvents = require('guarantee-events') var guaranteeEvents = require('guarantee-events')
@ -74,3 +78,25 @@ results.on('match', function(path){ console.log('found: '+path) })
``` ```
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:
```javascript
// 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.clearGuaranteedQueue('match')
```

47
index.js Normal file → Executable file
View File

@ -4,6 +4,28 @@
* *
**********************************************************************/ **********************************************************************/
// Clear event cache...
//
// This is added as a method to the emitter passed to guaranteeEvents(..)...
//
// NOTE: his has the same event names semantics as guaranteeEvents(..)
// NOTE: for more info see docs for guaranteeEvents(..)
function clearGuaranteedQueue(names){
names = names == '*' ? Object.keys(this._guaranteed_queue)
: typeof(names) == typeof('str') ? names.split(/\s+/g)
: names
var that = this
names.forEach(function(name){
if(name in that._guaranteed_queue){
that._guaranteed_queue[name] = []
}
})
return this
}
// Guarantee that every event handler gets every event... // Guarantee that every event handler gets every event...
// //
// guaranteeEvents('event', emitter) // guaranteeEvents('event', emitter)
@ -28,29 +50,12 @@
// //
// NOTE: the seen stack might get quite big, this is not recommended for // NOTE: the seen stack might get quite big, this is not recommended for
// long running emitters... // long running emitters...
// function guaranteeEvents(names, emitter){
var guaranteeEvents =
module.exports =
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.clearGuaranteedQueue == null){ if(emitter.clearGuaranteedQueue == null){
emitter.clearGuaranteedQueue = function(names){ emitter.clearGuaranteedQueue = clearGuaranteedQueue
names = names == '*' ? Object.keys(this._guaranteed_queue)
: typeof(names) == typeof('str') ? names.split(/\s+/g)
: names
var that = this
names.forEach(function(name){
if(name in that._guaranteed_queue){
that._guaranteed_queue[name] = []
}
})
return this
}
emitter._guaranteed_queue = {} emitter._guaranteed_queue = {}
} }
@ -81,6 +86,10 @@ function(names, emitter){
} }
// this is the only thing we are exporting...
module.exports = guaranteeEvents
/********************************************************************** /**********************************************************************
* vim:set ts=4 sw=4 : */ * vim:set ts=4 sw=4 : */

View File

@ -1,6 +1,6 @@
{ {
"name": "guarantee-events", "name": "guarantee-events",
"version": "1.0.1", "version": "1.0.6",
"description": "Guarantee that every event handler gets every event...", "description": "Guarantee that every event handler gets every event...",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
@ -18,7 +18,7 @@
"event" "event"
], ],
"author": "Alex A. Naanou <alex.nanou@gmail.com> (https://github.com/flynx)", "author": "Alex A. Naanou <alex.nanou@gmail.com> (https://github.com/flynx)",
"license": "New BSD", "license": "BSD-3-Clause",
"bugs": { "bugs": {
"url": "https://github.com/flynx/guaranteeEvents/issues" "url": "https://github.com/flynx/guaranteeEvents/issues"
}, },