1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
"use strict";
const { EventEmitter } = ChromeUtils.import(
"resource://normandy/lib/EventEmitter.jsm"
);
const evidence = {
a: 0,
b: 0,
c: 0,
log: "",
};
function listenerA(x) {
evidence.a += x;
evidence.log += "a";
}
function listenerB(x) {
evidence.b += x;
evidence.log += "b";
}
function listenerC(x) {
evidence.c += x;
evidence.log += "c";
}
decorate_task(async function() {
const eventEmitter = new EventEmitter();
// Fire an unrelated event, to make sure nothing goes wrong
eventEmitter.on("nothing");
// bind listeners
eventEmitter.on("event", listenerA);
eventEmitter.on("event", listenerB);
eventEmitter.once("event", listenerC);
// one event for all listeners
eventEmitter.emit("event", 1);
// another event for a and b, since c should have turned off already
eventEmitter.emit("event", 10);
// make sure events haven't actually fired yet, just queued
Assert.deepEqual(
evidence,
{
a: 0,
b: 0,
c: 0,
log: "",
},
"events are fired async"
);
// Spin the event loop to run events, so we can safely "off"
await Promise.resolve();
// Check intermediate event results
Assert.deepEqual(
evidence,
{
a: 11,
b: 11,
c: 1,
log: "abcab",
},
"intermediate events are fired"
);
// one more event for a
eventEmitter.off("event", listenerB);
eventEmitter.emit("event", 100);
// And another unrelated event
eventEmitter.on("nothing");
// Spin the event loop to run events
await Promise.resolve();
Assert.deepEqual(
evidence,
{
a: 111,
b: 11,
c: 1,
log: "abcaba", // events are in order
},
"events fired as expected"
);
// Test that mutating the data passed to the event doesn't actually
// mutate it for other events.
let handlerRunCount = 0;
const mutationHandler = data => {
handlerRunCount++;
data.count++;
is(data.count, 1, "Event data is not mutated between handlers.");
};
eventEmitter.on("mutationTest", mutationHandler);
eventEmitter.on("mutationTest", mutationHandler);
const data = { count: 0 };
eventEmitter.emit("mutationTest", data);
await Promise.resolve();
is(handlerRunCount, 2, "Mutation handler was executed twice.");
is(data.count, 0, "Event data cannot be mutated by handlers.");
});
|