-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
181 lines (142 loc) · 5.09 KB
/
index.ts
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module Howl {
interface IListen<TParam> {
listen(handler: Callback<TParam>): IListen<TParam>;
once(handler: Callback<TParam>): IListen<TParam>;
}
class EventEngine<TParam> implements IListen<TParam> {
listen(callback: Callback<TParam>): EventEngine<TParam> {
this.handlers.push({
callback: callback,
once: false
});
return this;
}
once(callback: Callback<TParam>): EventEngine<TParam> {
this.handlers.push({
callback: callback,
once: true
});
return this;
}
off(callback?: Callback<TParam>): EventEngine<TParam> {
this.handlers = this.handlers.filter((handler: Handler<TParam>) => {
return !isSameOrFalsy(handler.callback, callback);
});
return this;
}
protected executeHandlers(param?: TParam) {
this.handlers.forEach((handler: Handler<TParam>) => {
handler.callback(param);
if (handler.once) {
this.off(handler.callback)
}
});
}
protected static executeHandlersOn<TParam>(target: EventEngine<TParam>, param?: TParam) {
target.executeHandlers(param);
}
private handlers: Array<Handler<TParam>> = [];
}
export class Event<TParam> extends EventEngine<TParam> implements IListen<TParam> {
add<TChildParam>(child_param?: TParam): Event<TChildParam> {
var child: Event<TChildParam> = new Event<TChildParam>();
this.children.push(child);
child.parent = this;
child.parent_param = child_param;
return child;
}
trigger(param?: TParam): Event<TParam> {
this.executeHandlers(param);
if (this.parent) {
this.parent.trigger(this.parent_param);
}
this.listenings.forEach((listening: Listening<TParam>) => {
if (listening.source === this) {
EventEngine.executeHandlersOn(listening.proxy, param);
}
});
return this;
}
off(handler?: Callback<TParam>): Event<TParam> {
super.off(handler);
this.children.forEach((child: Event<TParam>) => {
child.off(handler);
});
return this;
}
toward(listener: Event<any>): IListen<TParam> {
var listening: Listening<TParam>;
listening = find<TParam>(this.listenings, (listening: Listening<TParam>) => {
return listening.listener == listener;
});
if (!listening) {
listening = {
listener: listener,
source: this,
proxy: new EventEngine<TParam>()
};
this.listenings.push(listening);
listener.listenings.push(listening);
}
return listening.proxy;
}
stopListening(source?: Event<any>, callback?: Callback<any>): Event<TParam> {
var sources: Event<any>[] = [];
this.listenings = this.listenings.filter((listening) => {
if (isSameOrFalsy(listening.source, source)) {
listening.proxy.off(callback);
if (!callback) {
sources.push(listening.source);
return false;
}
}
return true;
});
sources.forEach((source) => {
source.listenings = source.listenings.filter((listening) => {
return listening.listener !== this;
})
});
if (source) {
source.children.forEach((child) => {
this.stopListening(child, callback);
});
}
this.children.forEach((child) => {
child.stopListening(source, callback);
});
return this;
}
private parent: Event<any>;
private parent_param: any;
private children: Event<any>[] = [];
private listenings: Listening<TParam>[] = [];
}
interface Listening<TParam> {
listener: Event<any>;
source: Event<TParam>;
proxy: EventEngine<TParam>;
}
interface Handler<TParam> {
callback: Callback<TParam>;
once: boolean;
}
interface Callback<TParam> {
(param: TParam): void;
}
function isSameOrFalsy(staff, same_or_falsy) {
return !same_or_falsy || staff == same_or_falsy;
}
function find<TParam>(listenings: Listening<TParam>[], predicate: (item: Listening<TParam>) => {}) {
var i: number,
f: Listening<TParam>,
c: Listening<TParam>;
for (i = 0; !f && i < listenings.length; i++) {
c = listenings[i];
if (predicate(c)) {
return f = c;
}
}
return f;
}
}