forked from Floorp-Projects/Floorp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDomain.jsm
51 lines (41 loc) · 1.16 KB
/
Domain.jsm
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
var EXPORTED_SYMBOLS = ["Domain"];
class Domain {
constructor(session) {
this.session = session;
this.name = this.constructor.name;
this.eventListeners_ = new Set();
}
destructor() {}
emit(eventName, params = {}) {
for (const listener of this.eventListeners_) {
try {
if (isEventHandler(listener)) {
listener.onEvent(eventName, params);
} else {
listener.call(this, eventName, params);
}
} catch (e) {
Cu.reportError(e);
}
}
}
addEventListener(listener) {
if (typeof listener != "function" && !isEventHandler(listener)) {
throw new TypeError();
}
this.eventListeners_.add(listener);
}
// static
static implements(command) {
return command && typeof this.prototype[command] == "function";
}
}
function isEventHandler(listener) {
return listener &&
"onEvent" in listener &&
typeof listener.onEvent == "function";
}