@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
103 lines (102 loc) • 3.15 kB
TypeScript
/**
* Callback used by {@link EventHandler} functions. Note the callback is limited to 8 arguments.
*/
export type HandleEventCallback = (arg1?: any, arg2?: any, arg3?: any, arg4?: any, arg5?: any, arg6?: any, arg7?: any, arg8?: any) => any;
/**
* @import { EventHandler } from './event-handler.js'
*/
/**
* Callback used by {@link EventHandler} functions. Note the callback is limited to 8 arguments.
*
* @callback HandleEventCallback
* @param {*} [arg1] - First argument that is passed from caller.
* @param {*} [arg2] - Second argument that is passed from caller.
* @param {*} [arg3] - Third argument that is passed from caller.
* @param {*} [arg4] - Fourth argument that is passed from caller.
* @param {*} [arg5] - Fifth argument that is passed from caller.
* @param {*} [arg6] - Sixth argument that is passed from caller.
* @param {*} [arg7] - Seventh argument that is passed from caller.
* @param {*} [arg8] - Eighth argument that is passed from caller.
*/
/**
* Event Handle that is created by {@link EventHandler} and can be used for easier event removal and management.
* @example
* const evt = obj.on('test', (a, b) => {
* console.log(a + b);
* });
* obj.fire('test');
*
* evt.off(); // easy way to remove this event
* obj.fire('test'); // this will not trigger an event
* @example
* // store an array of event handles
* let events = [ ];
*
* events.push(objA.on('testA', () => { }));
* events.push(objB.on('testB', () => { }));
*
* // when needed, remove all events
* events.forEach((evt) => {
* evt.off();
* });
* events = [ ];
*/
export class EventHandle {
/**
* @param {EventHandler} handler - source object of the event.
* @param {string} name - Name of the event.
* @param {HandleEventCallback} callback - Function that is called when event is fired.
* @param {object} scope - Object that is used as `this` when event is fired.
* @param {boolean} [once] - If this is a single event and will be removed after event is fired.
*/
constructor(handler: EventHandler, name: string, callback: HandleEventCallback, scope: object, once?: boolean);
/**
* @type {EventHandler}
* @private
*/
private handler;
/**
* @type {string}
* @ignore
*/
name: string;
/**
* @type {HandleEventCallback}
* @ignore
*/
callback: HandleEventCallback;
/**
* @type {object}
* @ignore
*/
scope: object;
/**
* @type {boolean}
* @ignore
*/
_once: boolean;
/**
* True if event has been removed.
* @type {boolean}
* @private
*/
private _removed;
/**
* Remove this event from its handler.
*/
off(): void;
on(name: any, callback: any, scope?: this): EventHandle;
once(name: any, callback: any, scope?: this): EventHandle;
/**
* Mark if event has been removed.
* @type {boolean}
* @ignore
*/
set removed(value: boolean);
/**
* True if event has been removed.
* @type {boolean}
*/
get removed(): boolean;
}
import type { EventHandler } from './event-handler.js';