fix event related ts files

This commit is contained in:
Leslie
2019-01-08 17:18:56 +08:00
parent 59723a976f
commit 857b901d52
3 changed files with 220 additions and 294 deletions
+51 -71
View File
@@ -28,16 +28,17 @@ import * as js from '../utils/js';
const fastRemoveAt = js.array.fastRemoveAt;
class CallbackList {
constructor () {
this.callbacks = [];
this.targets = []; // same length with callbacks, nullable
this.isInvoking = false;
this.containCanceled = false;
}
public callbacks: Array<Function | null> = [];
public targets: Array<Object | null> = []; // same length with callbacks, nullable
public isInvoking = false;
public containCanceled = false;
public removeBy (array, value) {
var callbacks = this.callbacks;
var targets = this.targets;
const callbacks = this.callbacks;
const targets = this.targets;
for (let i = 0; i < array.length; ++i) {
if (array[i] === value) {
fastRemoveAt(callbacks, i);
@@ -47,7 +48,7 @@ class CallbackList {
}
}
public cancel (index) {
public cancel (index: number) {
this.callbacks[index] = this.targets[index] = null;
this.containCanceled = true;
}
@@ -69,7 +70,7 @@ class CallbackList {
}
const MAX_SIZE = 16;
const callbackListPool = new js.Pool(function (list) {
const callbackListPool = new js.Pool((list: CallbackList) => {
list.callbacks.length = 0;
list.targets.length = 0;
list.isInvoking = false;
@@ -79,6 +80,10 @@ callbackListPool.get = function () {
return this._get() || new CallbackList();
};
interface ICallbackTable {
[x: string]: CallbackList | undefined;
}
/**
* The CallbacksHandler is an abstract class that can register and unregister callbacks by key.
* Subclasses should implement their own methods about how to invoke the callbacks.
@@ -86,36 +91,31 @@ callbackListPool.get = function () {
*
* @private
*/
function CallbacksHandler () {
this._callbackTable = js.createMap(true);
}
CallbacksHandler.prototype = {
constructor: CallbacksHandler,
export class CallbacksHandler {
protected _callbackTable: ICallbackTable = js.createMap(true);
/**
* @method add
* @param {String} key
* @param {Function} callback
* @param {Object} [target] - can be null
* @param key
* @param callback
* @param [target] - can be null
*/
add (key, callback, target) {
public add (key: string, callback: Function, target: object | null = null) {
let list = this._callbackTable[key];
if (!list) {
list = this._callbackTable[key] = callbackListPool.get();
list = this._callbackTable[key] = callbackListPool.get() as CallbackList;
}
list.callbacks.push(callback);
list.targets.push(target || null);
},
}
/**
* Check if the specified key has any registered callback. If a callback is also specified,
* it will only return true if the callback is registered.
* @method hasEventListener
* @param {String} key
* @param {Function} [callback]
* @param {Object} [target]
* @return {Boolean}
* @param key
* @param [callback]
* @param [target]
*/
hasEventListener (key, callback, target) {
public hasEventListener (key: string, callback?: Function, target: Object | null = null) {
const list = this._callbackTable[key];
if (!list) {
return false;
@@ -126,8 +126,8 @@ CallbacksHandler.prototype = {
if (!callback) {
// Make sure no cancelled callbacks
if (list.isInvoking) {
for (let i = 0; i < callbacks.length; i++) {
if (callbacks[i]) {
for (const cb of callbacks) {
if (cb) {
return true;
}
}
@@ -145,14 +145,13 @@ CallbacksHandler.prototype = {
}
}
return false;
},
}
/**
* Removes all callbacks registered in a certain event type or all callbacks registered with a certain target
* @method removeAll
* @param {String|Object} keyOrTarget - The event key to be removed or the target to be removed
* @param keyOrTarget - The event key to be removed or the target to be removed
*/
removeAll (keyOrTarget) {
public removeAll (keyOrTarget?: string | object) {
if (typeof keyOrTarget === 'string') {
// remove by key
const list = this._callbackTable[keyOrTarget];
@@ -167,20 +166,22 @@ CallbacksHandler.prototype = {
} else if (keyOrTarget) {
// remove by target
for (const key in this._callbackTable) {
const list = this._callbackTable[key];
if (list.isInvoking) {
const targets = list.targets;
for (let i = 0; i < targets.length; ++i) {
if (targets[i] === keyOrTarget) {
list.cancel(i);
if (this._callbackTable.hasOwnProperty(key)) {
const list = this._callbackTable[key]!;
if (list.isInvoking) {
const targets = list.targets;
for (let i = 0; i < targets.length; ++i) {
if (targets[i] === keyOrTarget) {
list.cancel(i);
}
}
} else {
list.removeBy(list.targets, keyOrTarget);
}
} else {
list.removeBy(list.targets, keyOrTarget);
}
}
}
},
}
/**
* @method remove
@@ -188,7 +189,7 @@ CallbacksHandler.prototype = {
* @param {Function} callback
* @param {Object} [target]
*/
remove (key, callback, target) {
public remove (key, callback, target) {
const list = this._callbackTable[key];
if (list) {
target = target || null;
@@ -206,31 +207,15 @@ CallbacksHandler.prototype = {
}
}
}
},
};
}
}
/**
* !#en The callbacks invoker to handle and invoke callbacks by key.
* !#zh CallbacksInvoker 用来根据 Key 管理并调用回调方法。
* @class CallbacksInvoker
*
* @extends _CallbacksHandler
*/
function CallbacksInvoker () {
CallbacksHandler.call(this);
}
js.extend(CallbacksInvoker, CallbacksHandler);
js.mixin(CallbacksInvoker.prototype, {
/**
* @method emit
* @param {String} key
* @param {any} [p1]
* @param {any} [p2]
* @param {any} [p3]
* @param {any} [p4]
* @param {any} [p5]
*/
emit (key, p1, p2, p3, p4, p5) {
export class CallbacksInvoker extends CallbacksHandler {
public emit (key: string, p1?: any, p2?: any, p3?: any, p4?: any, p5?: any) {
const list = this._callbackTable[key];
if (list) {
const rootInvoker = !list.isInvoking;
@@ -257,14 +242,9 @@ js.mixin(CallbacksInvoker.prototype, {
}
}
}
},
});
}
}
if (CC_TEST) {
cc._Test.CallbacksInvoker = CallbacksInvoker;
}
export {
CallbacksHandler,
CallbacksInvoker,
};
+50 -57
View File
@@ -25,7 +25,8 @@
****************************************************************************/
import * as js from '../utils/js';
import {CallbacksInvoker} from './callbacks-invoker';
import { CallbacksInvoker } from './callbacks-invoker';
import Event from './event';
const fastRemove = js.array.fastRemove;
@@ -53,21 +54,17 @@ const fastRemove = js.array.fastRemove;
* 事件目标是事件触发时,分派的事件对象,Node 是最常见的事件目标,
* 但是其他对象也可以是事件目标。<br/>
*
* @class EventTarget
* @extends CallbacksInvoker
*/
function EventTarget () {
CallbacksInvoker.call(this);
}
js.extend(EventTarget, CallbacksInvoker);
js.mixin(EventTarget.prototype, {
export default class EventTarget extends CallbacksInvoker {
/**
* !#en Checks whether the EventTarget object has any callback registered for a specific type of event.
* !#zh 检查事件目标对象是否有为特定类型的事件注册的回调。
* @method hasEventListener
* @param {String} type - The type of event.
* @return {Boolean} True if a callback of the specified type is registered; false otherwise.
* @param type - The type of event.
* @return True if a callback of the specified type is registered; false otherwise.
*/
public hasEventListener (key: string) {
return super.hasEventListener(key);
}
/**
* !#en
@@ -76,39 +73,28 @@ js.mixin(EventTarget.prototype, {
* !#zh
* 注册事件目标的特定事件类型回调。这种类型的事件应该被 `emit` 触发。
*
* @method on
* @param {String} type - A string representing the event type to listen for.
* @param {Function} 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 {any} [callback.arg1] arg1
* @param {any} [callback.arg2] arg2
* @param {any} [callback.arg3] arg3
* @param {any} [callback.arg4] arg4
* @param {any} [callback.arg5] arg5
* @param {Object} [target] - The target (this object) to invoke the callback, can be null
* @return {Function} - Just returns the incoming callback so you can save the anonymous function easier.
* @typescript
* on<T extends Function>(type: string, callback: T, target?: any, useCapture?: boolean): T
* @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
* @return Just returns the incoming callback so you can save the anonymous function easier.
* @example
* eventTarget.on('fire', function () {
* cc.log("fire in the hole");
* }, node);
*/
on (type, callback, target) {
if (!callback) {
cc.errorID(6800);
return;
}
if ( !this.hasEventListener(type, callback, target) ) {
public on<T extends Function> (type: string, callback: T, target: Object | null = null) {
if (!super.hasEventListener(type, callback, target)) {
this.add(type, callback, target);
// @ts-ignore
if (target && target.__eventTargets) {
// @ts-ignore
target.__eventTargets.push(this);
}
}
return callback;
},
}
/**
* !#en
@@ -118,9 +104,10 @@ js.mixin(EventTarget.prototype, {
* 删除之前用同类型,回调,目标或 useCapture 注册的事件监听器,如果只传递 type,将会删除 type 类型的所有事件监听器。
*
* @method off
* @param {String} type - A string representing the event type being removed.
* @param {Function} [callback] - The callback to remove.
* @param {Object} [target] - The target (this object) to invoke the callback, if it's not given, only callback without target will be removed
* @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
* // register fire eventListener
* var callback = eventTarget.on('fire', function () {
@@ -131,32 +118,35 @@ js.mixin(EventTarget.prototype, {
* // remove all fire event listeners
* eventTarget.off('fire');
*/
off (type, callback, target) {
public off (type: string, callback?: Function, target: Object | null = null) {
if (!callback) {
this.removeAll(type);
} else {
this.remove(type, callback, target);
// @ts-ignore
if (target && target.__eventTargets) {
// @ts-ignore
fastRemove(target.__eventTargets, this);
}
}
},
}
/**
* !#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.
* 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 上注册的所有事件监听器。
* @method targetOff
* @param {Object} target - The target to be searched for all related listeners
*/
targetOff () {
public targetOff () {
this.removeAll();
},
}
/**
* !#en
@@ -180,20 +170,21 @@ js.mixin(EventTarget.prototype, {
* cc.log("this is the callback and will be invoked only once");
* }, node);
*/
once (type, callback, target) {
public once (type, callback, target) {
const eventType_hasOnceListener = '__ONCE_FLAG:' + type;
const hasOnceListener = this.hasEventListener(eventType_hasOnceListener, callback, target);
const hasOnceListener = super.hasEventListener(eventType_hasOnceListener, callback, target);
if (!hasOnceListener) {
const self = this;
const onceWrapper = function (arg1, arg2, arg3, arg4, arg5) {
self.off(type, onceWrapper, target);
self.remove(eventType_hasOnceListener, callback, target);
// @ts-ignore
callback.call(this, arg1, arg2, arg3, arg4, arg5);
};
this.on(type, onceWrapper, target);
this.add(eventType_hasOnceListener, callback, target);
}
},
}
/**
* !#en
@@ -201,18 +192,20 @@ js.mixin(EventTarget.prototype, {
* !#zh
* 通过事件名发送自定义事件
*
* @method emit
* @param {String} type - event type
* @param {*} [arg1] - First argument
* @param {*} [arg2] - Second argument
* @param {*} [arg3] - Third argument
* @param {*} [arg4] - Fourth argument
* @param {*} [arg5] - Fifth argument
* @param type - event type
* @param arg1 - First argument
* @param arg2 - Second argument
* @param arg3 - Third argument
* @param arg4 - Fourth argument
* @param arg5 - Fifth argument
* @example
*
* eventTarget.emit('fire', event);
* eventTarget.emit('fire', message, emitter);
*/
public emit (key: string, p1?: any, p2?: any, p3?: any, p4?: any, p5?: any) {
return super.emit(key, p1, p2, p3, p4, p5);
}
/**
* !#en
@@ -220,13 +213,13 @@ js.mixin(EventTarget.prototype, {
* !#zh
* 通过事件对象派发事件
*
* @method dispatchEvent
* @param {Event} event
* @param event
*/
dispatchEvent (event) {
public dispatchEvent (event: Event) {
// @ts-ignore
this.invoke(event.type, event);
},
});
}
}
cc.EventTarget = EventTarget;
export default EventTarget;
/* tslint:disable:no-string-literal */
cc['EventTarget'] = EventTarget;
+119 -166
View File
@@ -29,89 +29,134 @@
* !#zh 包含事件相关信息的对象。
* @class Event
*/
class Event {
export default class Event {
// Event types
/**
* @method constructor
* @param {String} type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit"
* @param {Boolean} bubbles - A boolean indicating whether the event bubbles up through the tree or not
* !#en Code for event without type.
* !#zh 没有类型的事件
*/
constructor (type, bubbles) {
/**
* !#en The name of the event (case-sensitive), e.g. "click", "fire", or "submit".
* !#zh 事件类型。
* @property type
* @type {String}
*/
public static NO_TYPE = 'no_type';
/**
* !#en The type code of Touch event.
* !#zh 触摸事件类型
*/
public static TOUCH = 'touch';
/**
* !#en The type code of Mouse event.
* !#zh 鼠标事件类型
*/
public static MOUSE = 'mouse';
/**
* !#en The type code of Keyboard event.
* !#zh 键盘事件类型
*/
public static KEYBOARD = 'keyboard';
/**
* !#en The type code of Acceleration event.
* !#zh 加速器事件类型
*/
public static ACCELERATION = 'acceleration';
// Event phases
/**
* !#en Events not currently dispatched are in this phase
* !#zh 尚未派发事件阶段
*/
public static NONE = 0;
/**
* !#en
* The capturing phase comprises the journey from the root to the last node before the event target's node
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 捕获阶段,包括事件目标节点之前从根节点到最后一个节点的过程。
*/
public static CAPTURING_PHASE = 1;
/**
* !#en
* The target phase comprises only the event target node
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 目标阶段仅包括事件目标节点。
*/
public static AT_TARGET = 2;
/**
* !#en
* The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the hierarchy
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 冒泡阶段, 包括回程遇到到层次根节点的任何后续节点。
*/
public static BUBBLING_PHASE = 3;
/**
* !#en The name of the event (case-sensitive), e.g. "click", "fire", or "submit".
* !#zh 事件类型。
*/
public type: string;
/**
* !#en Indicate whether the event bubbles up through the tree or not.
* !#zh 表示该事件是否进行冒泡。
*/
public bubbles: boolean;
/**
* !#en A reference to the target to which the event was originally dispatched.
* !#zh 最初事件触发的目标
*/
public target: Object | null = null;
/**
* !#en A reference to the currently registered target for the event.
* !#zh 当前目标
*/
public currentTarget: Object | null = null;
/**
* !#en
* Indicates which phase of the event flow is currently being evaluated.
* Returns an integer value represented by 4 constants:
* - Event.NONE = 0
* - Event.CAPTURING_PHASE = 1
* - Event.AT_TARGET = 2
* - Event.BUBBLING_PHASE = 3
* The phases are explained in the [section 3.1, Event dispatch and DOM event flow]
* (http://www.w3.org/TR/DOM-Level-3-Events/#event-flow), of the DOM Level 3 Events specification.
* !#zh 事件阶段
*/
public eventPhase = 0;
/*
* Indicates whether or not event.stopPropagation() has been called on the event.
*/
private _propagationStopped = false;
/*
* Indicates whether or not event.stopPropagationImmediate() has been called on the event.
*/
private _propagationImmediateStopped = false;
/**
* @param type - The name of the event (case-sensitive), e.g. "click", "fire", or "submit"
* @param bubbles - A boolean indicating whether the event bubbles up through the tree or not
*/
constructor (type: string, bubbles?: boolean) {
this.type = type;
/**
* !#en Indicate whether the event bubbles up through the tree or not.
* !#zh 表示该事件是否进行冒泡。
* @property bubbles
* @type {Boolean}
*/
this.bubbles = !!bubbles;
/**
* !#en A reference to the target to which the event was originally dispatched.
* !#zh 最初事件触发的目标
* @property target
* @type {Object}
*/
this.target = null;
/**
* !#en A reference to the currently registered target for the event.
* !#zh 当前目标
* @property currentTarget
* @type {Object}
*/
this.currentTarget = null;
/**
* !#en
* Indicates which phase of the event flow is currently being evaluated.
* Returns an integer value represented by 4 constants:
* - Event.NONE = 0
* - Event.CAPTURING_PHASE = 1
* - Event.AT_TARGET = 2
* - Event.BUBBLING_PHASE = 3
* The phases are explained in the [section 3.1, Event dispatch and DOM event flow]
* (http://www.w3.org/TR/DOM-Level-3-Events/#event-flow), of the DOM Level 3 Events specification.
* !#zh 事件阶段
* @property eventPhase
* @type {Number}
*/
this.eventPhase = 0;
/*
* Indicates whether or not event.stopPropagation() has been called on the event.
* @property _propagationStopped
* @type {Boolean}
* @private
*/
this._propagationStopped = false;
/*
* Indicates whether or not event.stopPropagationImmediate() has been called on the event.
* @property _propagationImmediateStopped
* @type {Boolean}
* @private
*/
this._propagationImmediateStopped = false;
}
/**
* !#en Reset the event for being stored in the object pool.
* !#zh 重置对象池中存储的事件。
* @method unuse
* @returns {String}
*/
public unuse () {
this.type = cc.Event.NO_TYPE;
this.type = Event.NO_TYPE;
this.target = null;
this.currentTarget = null;
this.eventPhase = cc.Event.NONE;
this.eventPhase = Event.NONE;
this._propagationStopped = false;
this._propagationImmediateStopped = false;
}
@@ -119,10 +164,8 @@ class Event {
/**
* !#en Reuse the event for being used again by the object pool.
* !#zh 用于对象池再次使用的事件。
* @method reuse
* @returns {String}
*/
public reuse (type, bubbles) {
public reuse (type: string, bubbles?: boolean) {
this.type = type;
this.bubbles = bubbles || false;
}
@@ -130,7 +173,6 @@ class Event {
/**
* !#en Stops propagation for current event.
* !#zh 停止传递当前事件。
* @method stopPropagation
*/
public stopPropagation () {
this._propagationStopped = true;
@@ -140,7 +182,6 @@ class Event {
* !#en Stops propagation for current event immediately,
* the event won't even be dispatched to the listeners attached in the current target.
* !#zh 立即停止当前事件的传递,事件甚至不会被分派到所连接的当前目标。
* @method stopPropagationImmediate
*/
public stopPropagationImmediate () {
this._propagationImmediateStopped = true;
@@ -149,8 +190,6 @@ class Event {
/**
* !#en Checks whether the event has been stopped.
* !#zh 检查该事件是否已经停止传递.
* @method isStopped
* @returns {Boolean}
*/
public isStopped () {
return this._propagationStopped || this._propagationImmediateStopped;
@@ -164,8 +203,7 @@ class Event {
* It returns 0 when the listener is associated with fixed priority.
* </p>
* !#zh 获取当前目标节点
* @method getCurrentTarget
* @returns {Node} The target with which the event associates.
* @returns The target with which the event associates.
*/
public getCurrentTarget () {
return this.currentTarget;
@@ -174,96 +212,11 @@ class Event {
/**
* !#en Gets the event type.
* !#zh 获取事件类型
* @method getType
* @returns {String}
*/
public getType () {
return this.type;
}
}
// event type
/**
* !#en Code for event without type.
* !#zh 没有类型的事件
* @property NO_TYPE
* @static
* @type {string}
*/
Event.NO_TYPE = 'no_type';
/**
* !#en The type code of Touch event.
* !#zh 触摸事件类型
* @property TOUCH
* @static
* @type {String}
*/
Event.TOUCH = 'touch';
/**
* !#en The type code of Mouse event.
* !#zh 鼠标事件类型
* @property MOUSE
* @static
* @type {String}
*/
Event.MOUSE = 'mouse';
/**
* !#en The type code of Keyboard event.
* !#zh 键盘事件类型
* @property KEYBOARD
* @static
* @type {String}
*/
Event.KEYBOARD = 'keyboard';
/**
* !#en The type code of Acceleration event.
* !#zh 加速器事件类型
* @property ACCELERATION
* @static
* @type {String}
*/
Event.ACCELERATION = 'acceleration';
// event phase
/**
* !#en Events not currently dispatched are in this phase
* !#zh 尚未派发事件阶段
* @property NONE
* @type {Number}
* @static
*/
Event.NONE = 0;
/**
* !#en
* The capturing phase comprises the journey from the root to the last node before the event target's node
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 捕获阶段,包括事件目标节点之前从根节点到最后一个节点的过程。
* @property CAPTURING_PHASE
* @type {Number}
* @static
*/
Event.CAPTURING_PHASE = 1;
/**
* !#en
* The target phase comprises only the event target node
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 目标阶段仅包括事件目标节点。
* @property AT_TARGET
* @type {Number}
* @static
*/
Event.AT_TARGET = 2;
/**
* !#en
* The bubbling phase comprises any subsequent nodes encountered on the return trip to the root of the hierarchy
* see http://www.w3.org/TR/DOM-Level-3-Events/#event-flow
* !#zh 冒泡阶段, 包括回程遇到到层次根节点的任何后续节点。
* @property BUBBLING_PHASE
* @type {Number}
* @static
*/
Event.BUBBLING_PHASE = 3;
cc.Event = Event;
export default Event;
/* tslint:disable:no-string-literal */
cc['Event'] = Event;