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
111
112
113
114
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
/* globals dump */
const Services = require("Services");
const KNOWN_EE_APIS = [
["on", "off"],
["addEventListener", "removeEventListener"],
["addListener", "removeListener"]
];
/**
* Listens for any event for a single time on a target, no matter what kind of
* event emitter it is, returning a promise resolved with the passed arguments
* once the event is fired.
*/
exports.once = function (target, eventName, options = {}) {
return exports.times(target, eventName, 1, options);
};
/**
* Waits for any event to be fired a specified amount of times on a target, no
* matter what kind of event emitter.
* Possible options: `useCapture`, `spreadArgs`, `expectedArgs`
*/
exports.times = function (target, eventName, receiveCount, options = {}) {
let msg = `Waiting for event: '${eventName}' on ${target} for ${receiveCount} time(s)`;
if ("expectedArgs" in options) {
dump(`${msg} with arguments: ${JSON.stringify(options.expectedArgs)}.\n`);
} else {
dump(`${msg}.\n`);
}
return new Promise((resolve, reject) => {
if (typeof eventName != "string") {
reject(new Error(`Unexpected event name: ${eventName}.`));
}
let API = KNOWN_EE_APIS.find(([a, r]) => (a in target) && (r in target));
if (!API) {
reject(new Error("Target is not a supported event listener."));
return;
}
let [add, remove] = API;
target[add](eventName, function onEvent(...args) {
if ("expectedArgs" in options) {
for (let index of Object.keys(options.expectedArgs)) {
if (
// Expected argument matches this regexp.
(options.expectedArgs[index] instanceof RegExp &&
!options.expectedArgs[index].exec(args[index])) ||
// Expected argument is not a regexp and equal to the received arg.
(!(options.expectedArgs[index] instanceof RegExp) &&
options.expectedArgs[index] != args[index])
) {
dump(`Ignoring event '${eventName}' with unexpected argument at index ` +
`${index}: ${args[index]}\n`);
return;
}
}
}
if (--receiveCount > 0) {
dump(`Event: '${eventName}' on ${target} needs to be fired ${receiveCount} ` +
`more time(s).\n`);
} else if (!receiveCount) {
dump(`Event: '${eventName}' on ${target} received.\n`);
target[remove](eventName, onEvent, options.useCapture);
resolve(options.spreadArgs ? args : args[0]);
}
}, options.useCapture);
});
};
/**
* Like `once`, but for observer notifications.
*/
exports.observeOnce = function (notificationName, options = {}) {
return exports.observeTimes(notificationName, 1, options);
};
/**
* Like `times`, but for observer notifications.
* Possible options: `expectedSubject`
*/
exports.observeTimes = function (notificationName, receiveCount, options = {}) {
dump(`Waiting for notification: '${notificationName}' for ${receiveCount} time(s).\n`);
return new Promise((resolve, reject) => {
if (typeof notificationName != "string") {
reject(new Error(`Unexpected notification name: ${notificationName}.`));
}
Services.obs.addObserver(function onObserve(subject, topic, data) {
if ("expectedSubject" in options && options.expectedSubject != subject) {
dump(`Ignoring notification '${notificationName}' with unexpected subject: ` +
`${subject}\n`);
return;
}
if (--receiveCount > 0) {
dump(`Notification: '${notificationName}' needs to be fired ${receiveCount} ` +
`more time(s).\n`);
} else if (!receiveCount) {
dump(`Notification: '${notificationName}' received.\n`);
Services.obs.removeObserver(onObserve, topic);
resolve(data);
}
}, notificationName, false);
});
};
|