-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
callbacks-invoker.ts
345 lines (316 loc) · 10.5 KB
/
callbacks-invoker.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* @hidden
*/
import Pool from '../memop/pool';
import { array, createMap } from '../utils/js';
const fastRemoveAt = array.fastRemoveAt;
function empty (){}
class CallbackInfo {
public callback: Function = empty;
public target: Object | undefined = undefined;
public once = false;
public set (callback: Function, target?: Object, once?: boolean) {
this.callback = callback;
this.target = target;
this.once = !!once;
}
}
const callbackInfoPool = new Pool(() => {
return new CallbackInfo();
}, 32);
export class CallbackList {
public callbackInfos: Array<CallbackInfo | null> = [];
public isInvoking = false;
public containCanceled = false;
/**
* @zh
* 从列表中移除与指定目标相同回调函数的事件。
*
* @param cb - 指定回调函数
*/
public removeByCallback (cb: Function) {
for (let i = 0; i < this.callbackInfos.length; ++i) {
const info = this.callbackInfos[i];
if (info && info.callback === cb) {
callbackInfoPool.free(info);
fastRemoveAt(this.callbackInfos, i);
--i;
}
}
}
/**
* @zh
* 从列表中移除与指定目标相同调用者的事件。
*
* @param target - 指定调用者
*/
public removeByTarget (target: Object) {
for (let i = 0; i < this.callbackInfos.length; ++i) {
const info = this.callbackInfos[i];
if (info && info.target === target) {
callbackInfoPool.free(info);
fastRemoveAt(this.callbackInfos, i);
--i;
}
}
}
/**
* @zh
* 移除指定编号事件。
*
* @param index - 指定编号。
*/
public cancel (index: number) {
const info = this.callbackInfos[index];
if (info) {
callbackInfoPool.free(info);
this.callbackInfos[index] = null;
}
this.containCanceled = true;
}
/**
* @zh
* 注销所有事件。
*/
public cancelAll () {
for (let i = 0; i < this.callbackInfos.length; i++) {
const info = this.callbackInfos[i];
if (info) {
callbackInfoPool.free(info);
this.callbackInfos[i] = null;
}
}
this.containCanceled = true;
}
/**
* @zh
* 取消所有回调。(在移除过程中会更加紧凑的排列数组)
*/
public purgeCanceled () {
for (let i = this.callbackInfos.length - 1; i >= 0; --i) {
const info = this.callbackInfos[i];
if (!info) {
fastRemoveAt(this.callbackInfos, i);
}
}
this.containCanceled = false;
}
/**
* @zh
* 清除并重置所有数据。
*/
public clear () {
this.cancelAll();
this.callbackInfos.length = 0;
this.isInvoking = false;
this.containCanceled = false;
}
}
const MAX_SIZE = 16;
const callbackListPool = new Pool<CallbackList>(() => {
return new CallbackList();
}, MAX_SIZE);
export interface ICallbackTable {
[x: string]: CallbackList | undefined;
}
/**
* @zh
* CallbacksInvoker 用来根据 Key 管理事件监听器列表并调用回调方法。
* @class CallbacksInvoker
*/
export class CallbacksInvoker {
public _callbackTable: ICallbackTable = createMap(true);
/**
* @zh
* 事件添加管理
*
* @param key - 一个监听事件类型的字符串。
* @param callback - 事件分派时将被调用的回调函数。
* @param arget - 调用回调的目标。可以为空。
* @param once - 是否只调用一次。
*/
public on (key: string, callback: Function, target?: Object, once?: boolean) {
let list = this._callbackTable[key];
if (!list) {
list = this._callbackTable[key] = callbackListPool.alloc();
}
const info = callbackInfoPool.alloc();
info.set(callback, target, once);
list.callbackInfos.push(info);
}
/**
* @zh
* 检查指定事件是否已注册回调。
*
* @param key - 一个监听事件类型的字符串。
* @param callback - 事件分派时将被调用的回调函数。
* @param target - 调用回调的目标。
* @return - 指定事件是否已注册回调。
*/
public hasEventListener (key: string, callback?: Function, target: Object | null = null) {
const list = this._callbackTable[key];
if (!list) {
return false;
}
// check any valid callback
const infos = list.callbackInfos;
if (!callback) {
// Make sure no cancelled callbacks
if (list.isInvoking) {
for (const info of infos) {
if (info) {
return true;
}
}
return false;
}
else {
return infos.length > 0;
}
}
for (const info of infos) {
if (info && info.callback === callback && info.target === target) {
return true;
}
}
return false;
}
/**
* @zh
* 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
*
* @param keyOrTarget - 要删除的事件键或要删除的目标。
*/
public removeAll (keyOrTarget?: string | Object) {
if (typeof keyOrTarget === 'string') {
// remove by key
const list = this._callbackTable[keyOrTarget];
if (list) {
if (list.isInvoking) {
list.cancelAll();
}
else {
list.clear();
callbackListPool.free(list);
delete this._callbackTable[keyOrTarget];
}
}
}
else if (keyOrTarget) {
// remove by target
for (const key in this._callbackTable) {
const list = this._callbackTable[key]!;
if (list.isInvoking) {
const infos = list.callbackInfos;
for (let i = 0; i < infos.length; ++i) {
const info = infos[i];
if (info && info.target === keyOrTarget) {
list.cancel(i);
}
}
}
else {
list.removeByTarget(keyOrTarget);
}
}
}
}
/**
* @zh
* 删除之前与同类型,回调,目标注册的回调。
*
* @param key - 一个监听事件类型的字符串。
* @param callback - 移除指定注册回调。如果没有给,则删除全部同事件类型的监听。
* @param target - 调用回调的目标。
*/
public off (key: string, callback?: Function, target?: Object) {
const list = this._callbackTable[key];
if (list) {
const infos = list.callbackInfos;
if (callback) {
for (let i = 0; i < infos.length; ++i) {
const info = infos[i];
if (info && info.callback === callback && info.target === target) {
if (list.isInvoking) {
list.cancel(i);
}
else {
fastRemoveAt(infos, i);
callbackInfoPool.free(info);
}
break;
}
}
}
else {
this.removeAll(key);
}
}
}
/**
* @zh
* 事件派发
*
* @param key - 一个监听事件类型的字符串
* @param p1 - 派发的第一个参数。
* @param p2 - 派发的第二个参数。
* @param p3 - 派发的第三个参数。
* @param p4 - 派发的第四个参数。
* @param p5 - 派发的第五个参数。
*/
public emit (key: string, ...args: any[]) {
const list: CallbackList = this._callbackTable[key]!;
if (list) {
const rootInvoker = !list.isInvoking;
list.isInvoking = true;
const infos = list.callbackInfos;
for (let i = 0, len = infos.length; i < len; ++i) {
const info = infos[i];
if (info) {
const callback = info.callback;
const target = info.target;
// Pre off once callbacks to avoid influence on logic in callback
if (info.once) {
this.off(key, callback, target);
}
if (target) {
callback.call(target, ...args);
}
else {
callback(...args);
}
}
}
if (rootInvoker) {
list.isInvoking = false;
if (list.containCanceled) {
list.purgeCanceled();
}
}
}
}
}
if (CC_TEST) {
cc._Test.CallbacksInvoker = CallbacksInvoker;
}