Comments (5)
I implemented this myself, maybe you can improve it?
eventemitter2.chains.js
if (!EventEmitter2) throw 'EventEmitter2 unknown.';
//unordered event chain (emitted once)
EventEmitter2.prototype.oncechain = function() {
var emitter = this;
//get arguments array
var args = Array.prototype.slice.call(arguments);
//get callback
var cb = args.pop();
//keep track of events
var events = {};
//checks if we are ready to go
var checkPending = function() {
for (var event in events) {
if (events.hasOwnProperty(event) && events[event].pending) {
return;
}
}
//all events fired
var evargs = [];
for (var event in events) {
if (events.hasOwnProperty(event)) {
evargs = evargs.concat(event.args);
}
}
//done
cb.apply(this, evargs);
};
//listen to all events once
args.forEach(function(event) {
events[event] = {
pending: true,
args: null
};
emitter.once(event, function() {
events[event].pending = false;
events[event].args = Array.prototype.slice.call(arguments);
checkPending();
});
});
};
Usage: emitter.oncechain('ui.body.hidden', 'document.ready', function() { });
from eventemitter2.
Improved by @Raynos with help of after
module:
function onceChain() {
var args = [].slice.call(arguments);
var ee2 = args.unshift();
var callback = args.pop();
after.map(args, function (eventName, name, callback) {
ee2.once(eventName, function (item) {
callback(null, item);
});
}, function (err, data) {
callback(err, data);
});
}
onceChain(ee2, 'server.configured', 'socketio.configured', function (err, data) {
data[0].listen();
});
from eventemitter2.
Note that an implementation of after.map
should be in-lined if it ever went into EventEmitter2 core.
Personally I don't think this piece of "flow control" utility should go into eventemitter2 itself.
Also after.map isn't documented but it's implemented here
from eventemitter2.
Maybe a monkey patching module is an idea? All you'd need to do at process startup:
require('eventemitter2');
require('eventemitter2.flow');
from eventemitter2.
This is cool. I'd love to see a library on top of ee2 that did some fancy stuff like this. eventslib
or something.
from eventemitter2.
Related Issues (20)
- Missing GitHub releases HOT 2
- is there a reasonable concept of using WeakRef for listeners? HOT 2
- Waiting for event to finish HOT 4
- proposal: rewrite source in typescript HOT 5
- Wildcard listener not invoked when also matching exact listener HOT 5
- a typo in `toObject` HOT 1
- Make it more ESM module friendly HOT 4
- addListener types definition missing options parameter HOT 1
- Why not Promise.allSettled instead of Promise.all?
- Graceful Shutdown HOT 1
- Feature Request: await emitter.onAny() HOT 1
- Export interface WaitForFilter HOT 1
- An index signature parameter type must be either 'string', 'symbol' or 'number' HOT 10
- [QUESTION]How to on many eventNames when wildcard is true
- logPossibleMemoryLeak throws an error when event name is a Symbol
- EventEmitter2 is not handling throw exception and closing app HOT 4
- [request] Add method to add an EventEmitter to class that already extends another class
- How to obtain eventemitter2.min.js
- EventEmitter in JavaScript can cause out of memory
- Not compatible with strict CSP due to `new Function(..)`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from eventemitter2.