Skip to content

Commit

Permalink
Merge branch 'release/2.0.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
Dirk Lüth authored and Dirk Lüth committed Jan 26, 2018
2 parents 3abd0d5 + e1fe2ef commit 492b7d9
Show file tree
Hide file tree
Showing 3 changed files with 363 additions and 119 deletions.
45 changes: 29 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,28 @@ function mapListener(listener) {
}

/**
* Apply an event + optional details to a listener
* Apply an event + optional details to all listener
*
* @param {Function} callback
* @param {Listener[]} listener
* @param {Event} event
* @param {Object[]=} details
*
* @ignore
*/
function applyEvent(callback, event, details) {
callback.call(this, event, ...details);
async function applyEvent(listener, event, details) {
let i = 0, item;

for(; (item = listener[i]); i++) {
if(item.remaining && !(item.remaining -= 1)) {
this.off(item.identifier, item.callback);
}

await Promise.resolve(item.callback.call(this, event, ...details));

if(event.isCanceled) {
break;
}
}
}

/**
Expand Down Expand Up @@ -203,17 +215,7 @@ class Emitter {
let listener = retrieveListener.call(this, name);

if(listener.length) {
let event = new Event(name, this);

listener.some((listener) => {
applyEvent.call(this, listener.callback, event, details);

if(listener.remaining && !(listener.remaining -= 1)) {
this.off(listener.identifier, listener.callback);
}

return event.isCanceled;
});
applyEvent.call(this, listener, new Event(name, this), details);
}

return this;
Expand Down Expand Up @@ -394,4 +396,15 @@ class Emitter {
}
}

module.exports = initialize(Emitter);
module.exports = initialize(Emitter);

/*
(new Emitter())
.on([ 'test1', 'test1' ], function(event) {
console.log(event.name);
return new Promise((resolve) => { resolve(); });
})
.emit('test1')
.emit('test1');
*/
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "flexee",
"version": "2.0.3",
"version": "2.0.4",
"description": "Flexible event emitter for Node.js supporting RegExp-based event subscription and global broadcast listeners",
"main": "index.js",
"author": {
Expand Down Expand Up @@ -37,5 +37,6 @@
"mocha": "^3.4.1",
"nyc": "^10.3.2",
"sinon": "^2.2.0"
}
},
"engines" : { "node" : ">=7.10.1" }
}
Loading

0 comments on commit 492b7d9

Please sign in to comment.