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