EventKit.js 5.95 KB
"use strict";
cc._RF.push(module, '9cfc1m4netFHInZPxXRhk45', 'EventKit');
// script/event/EventKit.ts

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Emitter = exports.Event = exports.CompositeDisposable = exports.Disposable = void 0;
class Disposable {
    constructor(disposalAction) {
        this._disposed = false;
        this._disposalAction = disposalAction;
    }
    get disposed() {
        return this._disposed;
    }
    dispose() {
        if (!this.disposed) {
            this._disposed = true;
            if (this._disposalAction) {
                this._disposalAction();
                this._disposalAction = null;
            }
        }
    }
}
exports.Disposable = Disposable;
class CompositeDisposable {
    constructor(...disposables) {
        this._disposed = false;
        this._disposables = new Set();
        for (let disposable of disposables) {
            this.add(disposable);
        }
    }
    add(disposable) {
        if (!this._disposed) {
            if (disposable) {
                this._disposables.add(disposable);
            }
        }
    }
    remove(disposable) {
        if (!this._disposed) {
            this._disposables.delete(disposable);
        }
    }
    clear() {
        if (!this._disposed) {
            this._disposables.clear();
        }
    }
    dispose() {
        if (!this._disposed) {
            this._disposed = true;
            this._disposables.forEach((disposable) => {
                disposable.dispose();
            });
            this._disposables = null;
        }
    }
}
exports.CompositeDisposable = CompositeDisposable;
class Event {
    constructor(emitter, name) {
        this.emit = ((...argsparam) => {
            argsparam.unshift(this.name);
            return this.emitter.emit.apply(this.emitter, argsparam);
        });
        this.emitter = emitter;
        this.name = name;
    }
    getName() {
        return this.name;
    }
    toString() {
        return `[Event ${this.name.toString()}]`;
    }
    on(handler, unshift = false) {
        return this.emitter.on(this.name, handler, unshift);
    }
    once(handler, unshift = false) {
        return this.emitter.once(this.name, handler, unshift);
    }
}
exports.Event = Event;
class Emitter {
    constructor() {
        this._disposed = false;
        this._handlersByEventName = new Map();
        this.off = (eventName, handlerToRemove) => {
            if (this._disposed)
                return;
            let oldHandlers = this._handlersByEventName.get(eventName);
            if (oldHandlers) {
                let newHandlers = [];
                for (let handler of oldHandlers) {
                    if (handler != handlerToRemove) {
                        newHandlers.push(handler);
                    }
                }
                if (newHandlers.length) {
                    this._handlersByEventName.set(eventName, newHandlers);
                }
                else {
                    this._handlersByEventName.delete(eventName);
                }
            }
        };
    }
    get disposed() {
        return this._disposed;
    }
    clear() {
        this._handlersByEventName.clear();
    }
    dispose() {
        this._handlersByEventName = null;
        this._disposed = true;
    }
    createEvent(eventName) {
        if (!eventName) {
            return new Event(this, Symbol("Event"));
        }
        return new Event(this, eventName);
    }
    on(eventName, handler, unshift = false) {
        if (this._disposed) {
            throw new Error("Emitter has been diposed");
        }
        let currentHandler = this._handlersByEventName.get(eventName);
        if (currentHandler) {
            if (unshift) {
                currentHandler.unshift(handler);
            }
            else {
                currentHandler.push(handler);
            }
        }
        else {
            this._handlersByEventName.set(eventName, [handler]);
        }
        return new Disposable(this.off.bind(this, eventName, handler));
    }
    once(eventName, handler, unshift = false) {
        let wrapped = (...args) => {
            disposable.dispose();
            return handler(...args);
        };
        let disposable = this.on(eventName, wrapped, unshift);
        return disposable;
    }
    emit(eventName, ...args) {
        let handlers = this._handlersByEventName.get(eventName);
        let result = [];
        if (handlers) {
            for (let handler of handlers) {
                try {
                    let ret = handler(...args);
                    if (ret === undefined) {
                        result.push(Promise.resolve());
                    }
                    else if (ret instanceof Promise) {
                        result.push(ret);
                    }
                    else {
                        result.push(Promise.resolve(ret));
                    }
                }
                catch (e) {
                    console.error(e);
                }
            }
        }
        return result;
    }
    getEventNames() {
        let ret = [];
        let keys = Object.keys(this._handlersByEventName);
        for (let k of keys) {
            if (typeof (k) === 'string') {
                ret.push(k);
            }
        }
        return ret;
    }
    getEventSymbols() {
        let ret = [];
        let keys = Object.keys(this._handlersByEventName);
        for (let k of keys) {
            if (typeof (k) === 'symbol') {
                ret.push(k);
            }
        }
        return ret;
    }
    listenerCountForEventName(eventName) {
        let handlers = this._handlersByEventName.get(eventName);
        return handlers ? handlers.length : 0;
    }
    getTotalListenerCount() {
        let result = 0;
        let keys = Object.keys(this._handlersByEventName);
        for (let eventName of keys) {
            result += this._handlersByEventName.get(eventName).length;
        }
        return result;
    }
}
exports.Emitter = Emitter;

cc._RF.pop();