Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement CallbacksInvoker using a linked list to avoid disorderly calling order caused by array.fastRemoveAt. #17705

Open
wants to merge 3 commits into
base: v3.8.5
Choose a base branch
from

Conversation

JoliChen
Copy link

Re: #

Changelog


Continuous Integration

This pull request:

  • needs automatic test cases check.

    Manual trigger with @cocos-robot run test cases afterward.

  • does not change any runtime related code or build configuration

    If any reviewer thinks the CI checks are needed, please uncheck this option, then close and reopen the issue.


Compatibility Check

This pull request:

  • changes public API, and have ensured backward compatibility with deprecated features.
  • affects platform compatibility, e.g. system version, browser version, platform sdk version, platform toolchain, language version, hardware compatibility etc.
  • affects file structure of the build package or build configuration which requires user project upgrade.
  • introduces breaking changes, please list all changes, affected features and the scope of violation.

Copy link

github-actions bot commented Oct 13, 2024

Interface Check Report

! WARNING this pull request has changed these public interfaces:

@@ -23209,8 +23209,77 @@
     export function setPropertyEnumType(objectOrConstructor: object, propertyName: string, enumType: __private._cocos_core_value_types_enum__EnumType): void;
     export function setPropertyEnumTypeOnAttrs(attrs: Record<string, any>, propertyName: string, enumType: __private._cocos_core_value_types_enum__EnumType): void;
     export function isCCObject(object: any): object is CCObject;
     export function isValid(value: any, strictMode?: boolean): boolean;
+    /**
+     * @zh CallbacksInvoker 用来根据事件名(Key)管理事件监听器列表并调用回调方法。
+     * @en CallbacksInvoker is used to manager and invoke event listeners with different event keys, each key is mapped to a CallbackList.
+     */
+    export class CallbacksInvoker<EventTypeClass extends __private._cocos_core_event_callbacks_invoker__EventType = __private._cocos_core_event_callbacks_invoker__EventType> {
+        /**
+         * @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
+         */
+        _callbackTable: __private._cocos_core_event_callbacks_invoker__ICallbackTable;
+        /**
+         * @zh 向一个事件名注册一个新的事件监听器,包含回调函数和调用者
+         * @en Register an event listener to a given event key with callback and target.
+         *
+         * @param key - Event type
+         * @param callback - Callback function when event triggered
+         * @param target - Callback callee
+         * @param once - Whether invoke the callback only once (and remove it)
+         *
+         * @returns callback
+         */
+        on(key: EventTypeClass, callback: __private.__types_globals__AnyFunction, target?: unknown, once?: boolean): typeof callback;
+        /**
+         * @zh 向一个事件名注册一个新的事件监听器,包含回调函数和调用者,该监听器只执行一次。
+         * @en Register an event listener to a given event key with callback and target.
+         *
+         * @param key - Event type
+         * @param callback - Callback function when event triggered
+         * @param target - Callback callee
+         *
+         * @returns callback
+         */
+        once(key: EventTypeClass, callback: __private.__types_globals__AnyFunction, target?: unknown): Function;
+        /**
+         * @zh 删除以指定事件,回调函数,目标注册的回调。
+         * @en Remove event listeners registered with the given event key, callback and target
+         * @param key - Event type
+         * @param callback - The callback function of the event listener, if absent all event listeners for the given type will be removed
+         * @param target - The callback callee of the event listener
+         */
+        off(key: EventTypeClass, callback: __private.__types_globals__AnyFunction, target?: unknown): void;
+        /**
+         * @zh 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
+         * @en Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
+         * @param keyOrTarget - The event type or target with which the listeners will be removed
+         */
+        removeAll(keyOrTarget: unknown): void;
+        hasTarget(target: unknown): boolean;
+        /**
+         * @zh 检查指定事件是否已注册回调。
+         * @en Checks whether there is correspond event listener registered on the given event
+         *
+         * @param key - Event type
+         * @param callback - Callback function when event triggered
+         * @param target - Callback callee
+         */
+        hasEventListener(key: EventTypeClass, callback?: __private.__types_globals__AnyFunction, target?: unknown): boolean;
+        /**
+         * @zh 派发一个指定事件,并传递需要的参数
+         * @en Trigger an event directly with the event name and necessary arguments.
+         * @param key - Event type
+         * @param arg0 - The first argument to be passed to the callback
+         * @param arg1 - The second argument to be passed to the callback
+         * @param arg2 - The third argument to be passed to the callback
+         * @param arg3 - The fourth argument to be passed to the callback
+         * @param arg4 - The fifth argument to be passed to the callback
+         */
+        emit(key: EventTypeClass, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void;
+        clear(): void;
+    }
     export namespace pipeline {
         export enum SetIndex {
             GLOBAL = 0,
             MATERIAL = 1,
@@ -63608,8 +63677,43 @@
             PLUGINS = "plugins",
             XR = "xr"
         }
         export type _cocos_core_value_types_enum__EnumType = Record<string, string | number>;
+        /// <reference types="./@types/globals" />
+        export class _cocos_core_event_callbacks_invoker__CallbackInfo {
+            callback: __types_globals__AnyFunction;
+            target: unknown;
+            once: boolean;
+            valid: boolean;
+            next: _cocos_core_event_callbacks_invoker__CallbackInfo | null;
+            set(callback: __types_globals__AnyFunction, target?: unknown, once?: boolean): this;
+            invalidate(): void;
+            clear(): this;
+            check(): boolean;
+        }
+        /**
+         * @zh 事件监听器列表的简单封装。
+         * @en A simple list of event callbacks
+         */
+        export class _cocos_core_event_callbacks_invoker__CallbackList {
+            isInvoking: boolean;
+            containCanceled: boolean;
+            head: _cocos_core_event_callbacks_invoker__CallbackInfo | null;
+            find(callback: __types_globals__AnyFunction, target?: unknown): _cocos_core_event_callbacks_invoker__CallbackInfo | null;
+            add(callback: __types_globals__AnyFunction, target?: unknown, once?: boolean): Function;
+            remove(callback: __types_globals__AnyFunction, target?: unknown): void;
+            removeByTarget(target: unknown): void;
+            cancel(info: _cocos_core_event_callbacks_invoker__CallbackInfo): void;
+            cancelByCallback(callback: __types_globals__AnyFunction, target?: unknown): void;
+            cancelByTarget(target: unknown): void;
+            cancelAll(): void;
+            purgeCanceled(): void;
+            clear(): this;
+        }
+        export interface _cocos_core_event_callbacks_invoker__ICallbackTable {
+            [x: string]: _cocos_core_event_callbacks_invoker__CallbackList | undefined;
+        }
+        export type _cocos_core_event_callbacks_invoker__EventType = string | number;
         export class _cocos_rendering_shadow_csm_layers__ShadowLayerVolume {
             protected _shadowObjects: pipeline.IRenderObject[];
             protected _shadowCameraFar: number;
             protected _level: number;
@@ -63776,123 +63880,8 @@
             ADD_POINTER_EVENT_PROCESSOR = 0,
             REMOVE_POINTER_EVENT_PROCESSOR = 1,
             MARK_LIST_DIRTY = 2
         }
-        /// <reference types="./@types/globals" />
-        export class _cocos_core_event_callbacks_invoker__CallbackInfo {
-            callback: __types_globals__AnyFunction;
-            target: unknown;
-            once: boolean;
-            set(callback: __types_globals__AnyFunction, target?: unknown, once?: boolean): void;
-            reset(): void;
-            check(): boolean;
-        }
-        /**
-         * @zh 事件监听器列表的简单封装。
-         * @en A simple list of event callbacks
-         */
-        export class _cocos_core_event_callbacks_invoker__CallbackList {
-            callbackInfos: Array<_cocos_core_event_callbacks_invoker__CallbackInfo | null>;
-            isInvoking: boolean;
-            containCanceled: boolean;
-            /**
-             * @zh 从列表中移除与指定目标相同回调函数的事件。
-             * @en Remove the event listeners with the given callback from the list
-             *
-             * @param cb - The callback to be removed
-             */
-            removeByCallback(cb: __types_globals__AnyFunction): void;
-            /**
-             * @zh 从列表中移除与指定目标相同调用者的事件。
-             * @en Remove the event listeners with the given target from the list
-             * @param target
-             */
-            removeByTarget(target: unknown): void;
-            /**
-             * @zh 移除指定编号事件。
-             * @en Remove the event listener at the given index
-             * @param index
-             */
-            cancel(index: number): void;
-            /**
-             * @zh 注销所有事件。
-             * @en Cancel all event listeners
-             */
-            cancelAll(): void;
-            /**
-             * @zh 立即删除所有取消的回调。(在移除过程中会更加紧凑的排列数组)
-             * @en Delete all canceled callbacks and compact array
-             */
-            purgeCanceled(): void;
-            /**
-             * @zh 清除并重置所有数据。
-             * @en Clear all data
-             */
-            clear(): void;
-        }
-        export interface _cocos_core_event_callbacks_invoker__ICallbackTable {
-            [x: string]: _cocos_core_event_callbacks_invoker__CallbackList | undefined;
-        }
-        export type _cocos_core_event_callbacks_invoker__EventType = string | number;
-        /**
-         * @zh CallbacksInvoker 用来根据事件名(Key)管理事件监听器列表并调用回调方法。
-         * @en CallbacksInvoker is used to manager and invoke event listeners with different event keys,
-         * each key is mapped to a CallbackList.
-         * @engineInternal
-         */
-        export class _cocos_core_event_callbacks_invoker__CallbacksInvoker<EventTypeClass extends _cocos_core_event_callbacks_invoker__EventType = _cocos_core_event_callbacks_invoker__EventType> {
-            /**
-             * @deprecated since v3.5.0, this is an engine private interface that will be removed in the future.
-             */
-            _callbackTable: _cocos_core_event_callbacks_invoker__ICallbackTable;
-            /**
-             * @zh 向一个事件名注册一个新的事件监听器,包含回调函数和调用者
-             * @en Register an event listener to a given event key with callback and target.
-             *
-             * @param key - Event type
-             * @param callback - Callback function when event triggered
-             * @param target - Callback callee
-             * @param once - Whether invoke the callback only once (and remove it)
-             */
-            on(key: EventTypeClass, callback: __types_globals__AnyFunction, target?: unknown, once?: boolean): __types_globals__AnyFunction;
-            /**
-             * @zh 检查指定事件是否已注册回调。
-             * @en Checks whether there is correspond event listener registered on the given event
-             * @param key - Event type
-             * @param callback - Callback function when event triggered
-             * @param target - Callback callee
-             */
-            hasEventListener(key: EventTypeClass, callback?: __types_globals__AnyFunction, target?: unknown): boolean;
-            /**
-             * @zh 移除在特定事件类型中注册的所有回调或在某个目标中注册的所有回调。
-             * @en Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
-             * @param keyOrTarget - The event type or target with which the listeners will be removed
-             */
-            removeAll(keyOrTarget: unknown): void;
-            /**
-             * @zh 删除以指定事件,回调函数,目标注册的回调。
-             * @en Remove event listeners registered with the given event key, callback and target
-             * @param key - Event type
-             * @param callback - The callback function of the event listener, if absent all event listeners for the given type will be removed
-             * @param target - The callback callee of the event listener
-             */
-            off(key: EventTypeClass, callback?: __types_globals__AnyFunction, target?: unknown): void;
-            /**
-             * @zh 派发一个指定事件,并传递需要的参数
-             * @en Trigger an event directly with the event name and necessary arguments.
-             * @param key - event type
-             * @param arg0 - The first argument to be passed to the callback
-             * @param arg1 - The second argument to be passed to the callback
-             * @param arg2 - The third argument to be passed to the callback
-             * @param arg3 - The fourth argument to be passed to the callback
-             * @param arg4 - The fifth argument to be passed to the callback
-             */
-            emit(key: EventTypeClass, arg0?: any, arg1?: any, arg2?: any, arg3?: any, arg4?: any): void;
-            /**
-             * 移除所有回调。
-             */
-            clear(): void;
-        }
         export interface _cocos_scene_graph_node_event_processor__IMask {
             index: number;
             comp: Component;
         }
@@ -64042,9 +64031,9 @@
             static _maskComp: __types_globals__Constructor<Component> | null;
             /**
              * @internal
              */
-            static callbacksInvoker: _cocos_core_event_callbacks_invoker__CallbacksInvoker<_cocos_scene_graph_node_event_processor__DispatcherEventType>;
+            static callbacksInvoker: CallbacksInvoker<_cocos_scene_graph_node_event_processor__DispatcherEventType>;
             /**
              * Whether the node event is enabled
              */
             get isEnabled(): boolean;
@@ -64070,13 +64059,13 @@
             get node(): Node;
             /**
              * Target in bubbling phase.
              */
-            bubblingTarget: _cocos_core_event_callbacks_invoker__CallbacksInvoker<_cocos_input_types_event_enum__SystemEventTypeUnion> | null;
+            bubblingTarget: CallbacksInvoker<_cocos_input_types_event_enum__SystemEventTypeUnion> | null;
             /**
              * Target in capturing phase.
              */
-            capturingTarget: _cocos_core_event_callbacks_invoker__CallbacksInvoker<_cocos_input_types_event_enum__SystemEventTypeUnion> | null;
+            capturingTarget: CallbacksInvoker<_cocos_input_types_event_enum__SystemEventTypeUnion> | null;
             /**
              * Whether the node has registered the mouse event callback
              */
             shouldHandleEventMouse: boolean;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant