-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
eventify.ts
161 lines (146 loc) · 7.29 KB
/
eventify.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
/**
* @category event
*/
import { CallbacksInvoker } from './callbacks-invoker';
import { createMap } from '../utils/js';
type Constructor<T = {}> = new (...args: any[]) => T;
type EventType = string;
/**
* @zh
* 实现该接口的对象具有处理事件的能力。
* @en
* Objects those implement this interface have essentially the capability to process events.
*/
export interface IEventified {
/**
* @zh 检查指定事件是否已注册回调。
* @en Checks whether there is correspond event listener registered on the given event.
* @param type - Event type.
* @param callback - Callback function when event triggered.
* @param target - Callback callee.
*/
hasEventListener (type: string, callback?: Function, target?: object): boolean;
/**
* @en
* Register an callback of a specific event type on the EventTarget.
* This type of event should be triggered via `emit`.
* @zh
* 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。
*
* @param type - A string representing the event type to listen for.
* @param callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param thisArg - The target (this object) to invoke the callback, can be null
* @return - Just returns the incoming callback so you can save the anonymous function easier.
* @example
* import { log } from 'cc';
* eventTarget.on('fire', function () {
* log("fire in the hole");
* }, node);
*/
on<TFunction extends Function> (type: EventType, callback: TFunction, thisArg?: any, once?: boolean): typeof callback;
/**
* @en
* Register an callback of a specific event type on the EventTarget,
* the callback will remove itself after the first time it is triggered.
* @zh
* 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。
*
* @param type - A string representing the event type to listen for.
* @param callback - The callback that will be invoked when the event is dispatched.
* The callback is ignored if it is a duplicate (the callbacks are unique).
* @param target - The target (this object) to invoke the callback, can be null
* @example
* import { log } from 'cc';
* eventTarget.once('fire', function () {
* log("this is the callback and will be invoked only once");
* }, node);
*/
once<TFunction extends Function> (type: EventType, callback: TFunction, thisArg?: any): typeof callback;
/**
* @en
* Removes the listeners previously registered with the same type, callback, target and or useCapture,
* if only type is passed as parameter, all listeners registered with that type will be removed.
* @zh
* 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
*
* @param type - A string representing the event type being removed.
* @param callback - The callback to remove.
* @param target - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @example
* import { log } from 'cc';
* // register fire eventListener
* var callback = eventTarget.on('fire', function () {
* log("fire in the hole");
* }, target);
* // remove fire event listener
* eventTarget.off('fire', callback, target);
* // remove all fire event listeners
* eventTarget.off('fire');
*/
off<TFunction extends Function> (type: EventType, callback?: TFunction, thisArg?: any): void;
/**
* @en Removes all callbacks previously registered with the same target (passed as parameter).
* This is not for removing all listeners in the current event target,
* and this is not for removing all listeners the target parameter have registered.
* It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter.
* @zh 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。
* 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。
* 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。
* @param typeOrTarget - The target to be searched for all related listeners
*/
targetOff (typeOrTarget: string | object): void;
/**
* @zh 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
* @en Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
* @param typeOrTarget - The event type or target with which the listeners will be removed
*/
removeAll (typeOrTarget: string | object): void;
/**
* @zh 派发一个指定事件,并传递需要的参数
* @en Trigger an event directly with the event name and necessary arguments.
* @param type - event type
* @param args - Arguments when the event triggered
*/
emit (type: EventType, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void;
}
/**
* @en Generate a new class from the given base class, after polyfill all functionalities in [[IEventified]] as if it's extended from [[EventTarget]]
* @zh 生成一个类,该类继承自指定的基类,并以和 [[EventTarget]] 等同的方式实现了 [[IEventified]] 的所有接口。
* @param base The base class
* @example
* ```ts
* class Base { say() { console.log('Hello!'); } }
* class MyClass extends Eventify(Base) { }
* function (o: MyClass) {
* o.say(); // Ok: Extend from `Base`
* o.emit('sing', 'The ghost'); // Ok: `MyClass` implements IEventified
* }
* ```
*/
export function Eventify<TBase> (base: Constructor<TBase>): Constructor<TBase & IEventified> {
class Eventified extends (base as unknown as any) {
private _callbackTable = createMap(true);
public once<Callback extends Function> (type: EventType, callback: Callback, target?: object) {
return this.on(type, callback, target, true);
}
public targetOff (typeOrTarget: string | object) {
this.removeAll(typeOrTarget);
}
};
// Mixin with `CallbacksInvokers`'s prototype
const callbacksInvokerPrototype = CallbacksInvoker.prototype;
const propertyKeys: (string | symbol)[] =
(Object.getOwnPropertyNames(callbacksInvokerPrototype) as (string | symbol)[]).concat(
Object.getOwnPropertySymbols(callbacksInvokerPrototype));
for (let iPropertyKey = 0; iPropertyKey < propertyKeys.length; ++iPropertyKey) {
const propertyKey = propertyKeys[iPropertyKey];
if (!(propertyKey in Eventified.prototype)) {
const propertyDescriptor = Object.getOwnPropertyDescriptor(callbacksInvokerPrototype, propertyKey);
if (propertyDescriptor) {
Object.defineProperty(Eventified.prototype, propertyKey, propertyDescriptor);
}
}
}
return Eventified as unknown as any;
}