1 | import { EVENT_ALL } from "../api.js";
|
2 | import { mixin } from "../mixin.js";
|
3 | const inotify_dispatch = (listeners, e) => {
|
4 | if (!listeners) return false;
|
5 | for (let i = 0, n = listeners.length, l; i < n; i++) {
|
6 | l = listeners[i];
|
7 | l[0].call(l[1], e);
|
8 | if (e.canceled) {
|
9 | return false;
|
10 | }
|
11 | }
|
12 | return true;
|
13 | };
|
14 | const INotifyMixin = mixin({
|
15 | addListener(id, fn, scope) {
|
16 | let l = (this._listeners = this._listeners || {})[id];
|
17 | !l && (l = this._listeners[id] = []);
|
18 | if (this.__listener(l, fn, scope) === -1) {
|
19 | l.push([fn, scope]);
|
20 | return true;
|
21 | }
|
22 | return false;
|
23 | },
|
24 | removeListener(id, fn, scope) {
|
25 | let listeners;
|
26 | if (!(listeners = this._listeners)) return false;
|
27 | const l = listeners[id];
|
28 | if (l) {
|
29 | const idx = this.__listener(l, fn, scope);
|
30 | if (idx !== -1) {
|
31 | l.splice(idx, 1);
|
32 | !l.length && delete listeners[id];
|
33 | return true;
|
34 | }
|
35 | }
|
36 | return false;
|
37 | },
|
38 | notify(e) {
|
39 | let listeners;
|
40 | if (!(listeners = this._listeners)) return false;
|
41 | e.target === void 0 && (e.target = this);
|
42 | const res = inotify_dispatch(listeners[e.id], e);
|
43 | return inotify_dispatch(listeners[EVENT_ALL], e) || res;
|
44 | },
|
45 | __listener(listeners, f, scope) {
|
46 | let i = listeners.length;
|
47 | while (i-- > 0) {
|
48 | const l = listeners[i];
|
49 | if (l[0] === f && l[1] === scope) {
|
50 | break;
|
51 | }
|
52 | }
|
53 | return i;
|
54 | }
|
55 | });
|
56 | export {
|
57 | INotifyMixin,
|
58 | inotify_dispatch
|
59 | };
|