Asynchronous events' library with a guarantee of the preservation of the events' order
$ npm install --save aventvar eventified = {
doThing: function () {
this._eventEmitter.trigger('thing:done');
};
};
Avent.eventify(eventified);
// OR
var Eventified = function () {
this._initEventEmitter();
};
Eventified.prototype.doThing = function () {
this._eventEmitter.trigger('thing:done');
};
var eventified = new Eventified();
// ...
eventified.on('thing:done', function () {
console.log('The thing was done!');
});
eventified.doThing();
console.log('Waiting for the thing is done...');
// in console:
// > Waiting for the thing is done...
// > The thing is done!- Asynchronous event triggering. Callback is called after the event loop turn in which it was triggered.
- Order of events is preserved. First triggered event will be handled first.
- Events can be added to object or to class
- By default, an event emitter is a hidden object inside an eventified object. Eventified object provides abilities for external user to subscribe on events but not to trigger them.
- It is possible to eventify objects by external event emitter.
- There is an event logger which may be customized.
- It is possible to customize the event system (to rename methods) with minimal reworks on your side.
When I started to work on a project, I had the following requirements for event library:
- Events must be triggered asynchronously
- Order of events must be kept unchanged (first triggered, first handled)
- Zero dependencies
The first was (and is) absolutely mandatory thing for me since I thought (and still think) that mixing synchronous and asynchronous code is unsafe. Btw, for the same reason we have asynchronous promises in ECMA standard.
And (surprise!) I was not able to find such library, so in the project I took Backbone Events and modified them a bit to introduce asynchronous handling:
Events._originalTrigger = Events.trigger;
Events.trigger = function () {
var args = arguments;
var that = this;
setTimeout(function () {
Events._originalTrigger.apply(that, args);
}, 0);
};For the project it was a "zero dependency" because Backbone was in dependencies already so I did not introduce anything new.
It served my needs for a while but at some point I was reported about a defect which was caused by changing order of
events during handling. Most of time setTimeout worked fine for me, but in some conditions the first triggered
event was handled after the second triggered one and the order change moved application to wrong state.
There was no clue about why it happened... but I had to fix the defect, so I wrote own event library which guarantees preservation of events' order.
And it was the right decision since we started the new project quite soon and the new project did not need Backbone but required event system.
This is revisited version of that library with extra features written in hope that I'm not only person who likes asynchronous events.
TODO
TODO
TODO
TODO
TODO
MIT - see LICENSE