|
| 1 | +/* |
| 2 | + Copyright (c) 2020-2023 Xiamen Yaji Software Co., Ltd. |
| 3 | +
|
| 4 | + https://www.cocos.com/ |
| 5 | +
|
| 6 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + of this software and associated documentation files (the "Software"), to deal |
| 8 | + in the Software without restriction, including without limitation the rights to |
| 9 | + use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 10 | + of the Software, and to permit persons to whom the Software is furnished to do so, |
| 11 | + subject to the following conditions: |
| 12 | +
|
| 13 | + The above copyright notice and this permission notice shall be included in |
| 14 | + all copies or substantial portions of the Software. |
| 15 | +
|
| 16 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + THE SOFTWARE. |
| 23 | +*/ |
| 24 | + |
| 25 | +import { CallbacksInvoker } from './CallbacksInvoker'; |
| 26 | +import { createMap } from './utils'; |
| 27 | + |
| 28 | +type Constructor<T> = new (...args: any[]) => T; |
| 29 | + |
| 30 | +type EventType = string | number; |
| 31 | + |
| 32 | +/** |
| 33 | + * @zh |
| 34 | + * 实现该接口的对象具有处理事件的能力。 |
| 35 | + * @en |
| 36 | + * Objects those implement this interface have essentially the capability to process events. |
| 37 | + */ |
| 38 | +export interface IEventified { |
| 39 | + /** |
| 40 | + * @zh 检查指定事件是否已注册回调。 |
| 41 | + * @en Checks whether there is correspond event listener registered on the given event. |
| 42 | + * @param type - Event type. |
| 43 | + * @param callback - Callback function when event triggered. |
| 44 | + * @param target - Callback callee. |
| 45 | + */ |
| 46 | + hasEventListener(type: string, callback?: (...args: any[]) => void, target?: any): boolean; |
| 47 | + |
| 48 | + /** |
| 49 | + * @en |
| 50 | + * Register an callback of a specific event type on the EventTarget. |
| 51 | + * This type of event should be triggered via `emit`. |
| 52 | + * @zh |
| 53 | + * 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。 |
| 54 | + * |
| 55 | + * @param type - A string representing the event type to listen for. |
| 56 | + * @param callback - The callback that will be invoked when the event is dispatched. |
| 57 | + * The callback is ignored if it is a duplicate (the callbacks are unique). |
| 58 | + * @param thisArg - The target (this object) to invoke the callback, can be null |
| 59 | + * @return - Just returns the incoming callback so you can save the anonymous function easier. |
| 60 | + * @example |
| 61 | + * import { log } from 'cc'; |
| 62 | + * eventTarget.on('fire', function () { |
| 63 | + * log("fire in the hole"); |
| 64 | + * }, node); |
| 65 | + */ |
| 66 | + on<TFunction extends (...args: any[]) => void>(type: EventType, callback: TFunction, thisArg?: any, once?: boolean): typeof callback; |
| 67 | + |
| 68 | + /** |
| 69 | + * @en |
| 70 | + * Register an callback of a specific event type on the EventTarget, |
| 71 | + * the callback will remove itself after the first time it is triggered. |
| 72 | + * @zh |
| 73 | + * 注册事件目标的特定事件类型回调,回调会在第一时间被触发后删除自身。 |
| 74 | + * |
| 75 | + * @param type - A string representing the event type to listen for. |
| 76 | + * @param callback - The callback that will be invoked when the event is dispatched. |
| 77 | + * The callback is ignored if it is a duplicate (the callbacks are unique). |
| 78 | + * @param target - The target (this object) to invoke the callback, can be null |
| 79 | + * @example |
| 80 | + * import { log } from 'cc'; |
| 81 | + * eventTarget.once('fire', function () { |
| 82 | + * log("this is the callback and will be invoked only once"); |
| 83 | + * }, node); |
| 84 | + */ |
| 85 | + once<TFunction extends (...args: any[]) => void>(type: EventType, callback: TFunction, thisArg?: any): typeof callback; |
| 86 | + |
| 87 | + /** |
| 88 | + * @en |
| 89 | + * Removes the listeners previously registered with the same type, callback, target and or useCapture, |
| 90 | + * if only type is passed as parameter, all listeners registered with that type will be removed. |
| 91 | + * @zh |
| 92 | + * 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。 |
| 93 | + * |
| 94 | + * @param type - A string representing the event type being removed. |
| 95 | + * @param callback - The callback to remove. |
| 96 | + * @param target - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed |
| 97 | + * @example |
| 98 | + * import { log } from 'cc'; |
| 99 | + * // register fire eventListener |
| 100 | + * var callback = eventTarget.on('fire', function () { |
| 101 | + * log("fire in the hole"); |
| 102 | + * }, target); |
| 103 | + * // remove fire event listener |
| 104 | + * eventTarget.off('fire', callback, target); |
| 105 | + * // remove all fire event listeners |
| 106 | + * eventTarget.off('fire'); |
| 107 | + */ |
| 108 | + off<TFunction extends (...args: any[]) => void>(type: EventType, callback?: TFunction, thisArg?: any): void; |
| 109 | + |
| 110 | + /** |
| 111 | + * @en Removes all callbacks previously registered with the same target (passed as parameter). |
| 112 | + * This is not for removing all listeners in the current event target, |
| 113 | + * and this is not for removing all listeners the target parameter have registered. |
| 114 | + * It's only for removing all listeners (callback and target couple) registered on the current event target by the target parameter. |
| 115 | + * @zh 在当前 EventTarget 上删除指定目标(target 参数)注册的所有事件监听器。 |
| 116 | + * 这个函数无法删除当前 EventTarget 的所有事件监听器,也无法删除 target 参数所注册的所有事件监听器。 |
| 117 | + * 这个函数只能删除 target 参数在当前 EventTarget 上注册的所有事件监听器。 |
| 118 | + * @param typeOrTarget - The target to be searched for all related listeners |
| 119 | + */ |
| 120 | + targetOff(typeOrTarget: any): void; |
| 121 | + |
| 122 | + /** |
| 123 | + * @zh 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。 |
| 124 | + * @en Removes all callbacks registered in a certain event type or all callbacks registered with a certain target |
| 125 | + * @param typeOrTarget - The event type or target with which the listeners will be removed |
| 126 | + */ |
| 127 | + removeAll(typeOrTarget: any): void; |
| 128 | + |
| 129 | + /** |
| 130 | + * @zh 派发一个指定事件,并传递需要的参数 |
| 131 | + * @en Trigger an event directly with the event name and necessary arguments. |
| 132 | + * @param type - event type |
| 133 | + * @param args - Arguments when the event triggered |
| 134 | + */ |
| 135 | + emit(type: EventType, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any, arg9?: any, |
| 136 | + arg10?: any, arg11?: any, arg12?: any, arg13?: any, arg14?: any, arg15?: any, arg16?: any, arg17?: any, arg18?: any): any; |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * @en Generate a new class from the given base class, after polyfill all functionalities in [[IEventified]] as if it's extended from [[EventTarget]] |
| 141 | + * @zh 生成一个类,该类继承自指定的基类,并以和 [[EventTarget]] 等同的方式实现了 [[IEventified]] 的所有接口。 |
| 142 | + * @param base The base class |
| 143 | + * @example |
| 144 | + * ```ts |
| 145 | + * class Base { say() { console.log('Hello!'); } } |
| 146 | + * class MyClass extends Eventify(Base) { } |
| 147 | + * function (o: MyClass) { |
| 148 | + * o.say(); // Ok: Extend from `Base` |
| 149 | + * o.emit('sing', 'The ghost'); // Ok: `MyClass` implements IEventified |
| 150 | + * } |
| 151 | + * ``` |
| 152 | + */ |
| 153 | +export function Eventify<TBase>(base: Constructor<TBase>): Constructor<TBase & IEventified> { |
| 154 | + class Eventified extends (base as unknown as any) { |
| 155 | + /** |
| 156 | + * @dontmangle |
| 157 | + * NOTE: Eventified mixins all properties from CallbacksInvoker.prototype in the following code. |
| 158 | + * After invoking `Eventify` for a class, CallbacksInvoker's constructor will not be called, |
| 159 | + * but its functions may invoke `this._callbackTable` which is declared as `public` in CallbacksInvoker. |
| 160 | + * Marking it as dontmangle is a workaround to avoid the issue that `this._callbackTable` is not defined. |
| 161 | + */ |
| 162 | + protected _callbackTable = createMap(true); |
| 163 | + |
| 164 | + public once<Callback extends (...any) => void>(type: EventType, callback: Callback, target?: any): Callback { |
| 165 | + return this.on(type, callback, target, true) as Callback; |
| 166 | + } |
| 167 | + |
| 168 | + public targetOff(typeOrTarget: any): void { |
| 169 | + this.removeAll(typeOrTarget); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + // Mixin with `CallbacksInvokers`'s prototype |
| 174 | + const callbacksInvokerPrototype = CallbacksInvoker.prototype; |
| 175 | + const propertyKeys: (string | symbol)[] = (Object.getOwnPropertyNames(callbacksInvokerPrototype) as (string | symbol)[]).concat( |
| 176 | + Object.getOwnPropertySymbols(callbacksInvokerPrototype), |
| 177 | + ); |
| 178 | + for (let iPropertyKey = 0; iPropertyKey < propertyKeys.length; ++iPropertyKey) { |
| 179 | + const propertyKey = propertyKeys[iPropertyKey]; |
| 180 | + if (!(propertyKey in Eventified.prototype)) { |
| 181 | + const propertyDescriptor = Object.getOwnPropertyDescriptor(callbacksInvokerPrototype, propertyKey); |
| 182 | + if (propertyDescriptor) { |
| 183 | + Object.defineProperty(Eventified.prototype, propertyKey, propertyDescriptor); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return Eventified as unknown as Constructor<TBase & IEventified>; |
| 189 | +} |
0 commit comments