-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCallback.js
60 lines (56 loc) · 1.69 KB
/
Callback.js
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
import { isYaffEvents } from './utils';
import { getInterop } from './interop';
import { handler } from './EventsHandler';
let gccounter = 0;
export class Callback {
constructor(eventName, callback, context, once, listener, eHandler, lHandler) {
this.id = ++gccounter;
this.eventName = eventName
this.callback = callback
this.context = context
this.once = once;
this.listener = listener;
//this.eHandler = eHandler;
this.emitter = eHandler.obj;
this.defaultContext = listener || this.emitter;
if (this.listener) {
this.lHandler = lHandler || handler(this.listener);
this._listen = this.lHandler.listenTos.push(eHandler);
if (!isYaffEvents(this.emitter)) {
let interop = getInterop(this.emitter);
let interopCallback = (...args) => this.invoke(args);
interop.on(this.emitter, this.listener, this.eventName, interopCallback);
this.removeInterop = () => {
delete this.removeInterop;
interop.off(this.emitter, this.listener, this.eventName, interopCallback);
}
}
}
}
invoke(args) {
if (this.calledOnce) return this;
if (this.once) {
this.calledOnce = true;
this.destroy();
}
this.callback && this.callback.apply(this.context || this.defaultContext, args);
return this;
}
destroy() {
this.removeInterop && this.removeInterop();
this.removeListenTo();
this.removeEvent();
}
removeListenTo() {
if (this._listen) {
this._listen.count--;
if (!this._listen.count) {
this.lHandler.listenTos.delete(this._listen);
this._listen = null;
}
}
}
removeEvent() {
this._event[this._eventIndex] = null;
}
}