EventEmitter event ordering
May. 1st, 2016 12:24 pmhttp://stackoverflow.com/q/36956220/1935631
It looks like the people don't even understand the meaning of the question.
Is there a safe pattern / good practice / guarantee by design on the arrival and observation of events?
It looks like the people don't even understand the meaning of the question.
Is there a safe pattern / good practice / guarantee by design on the arrival and observation of events?
$ cat a.js
'use strict';
const EventEmitter = require('events');
const e = new EventEmitter();
e.on('a', x => {
console.log('x = ' + x);
if (x === 0) {
e.emit('a', 2);
e.removeAllListeners('a');
e.emit('a', 3);
e.on('a', y => console.log('y = ' + y));
e.emit('a', 4);
}
});
e.on('a', z => console.log('z = ' + z));
console.log('a');
e.emit('a', 0);
console.log('b');
e.emit('a', 1);
console.log('c');
$ node a.js
a
x = 0
x = 2
z = 2
y = 4
z = 0
b
y = 1
c
- emit is atomic with respect to remove* and add* listeners
- emits are not atomic with respect to each other
- As a result, consumers can't agree on the observed order of events: x=0,x=2, but z=2,z=0 - but what's the way to make them agree? The only guarantee I can see is program order of emit start with respect to emit end (ie make all emits occur in a single place)