index.js 7.2 KB
"use strict";
/*
用法1:
import { Emitter, CompositeDisposable } from "event-kit";

class Somthing {
    disposables = new CompositeDisposable();

    constructor(user: User) {
        this.disposables.add(user.onDidChangeName(()=>{}));
        this.disposables.add(user.onDidChangePhone(()=>{}));
    }

    destroy() {
        this.disposables.dispose();
    }
}

class User {
    emitter = new Emitter();
    private name: string;
    private phone: string;

    onDidChangeName(callback: (name: string) => void) {
        return this.emitter.on("did-change-name", callback);
    }

    onDidChangePhone(callback: (phone: string) => void) {
        return this.emitter.on("did-change-phone", callback);
    }

    setName(name: string) {
        if (name != this.name) {
            this.name = name;
            this.emitter.emit("did-change-name", name);
        }
    }

    setPhone(phone: string) {
        if (phone != this.phone) {
            this.phone = phone;
            this.emitter.emit("did-change-phone", phone);
        }
    }
}
*/
/*
用法2:
class User {
    private name: string;
    private emitter = new Emitter();
    onNameChangeEvent = this.emitter.createEvent<(name: string) => void>("did-change-name");

    setName(name: string) {
        if (name != this.name) {
            this.onNameChangeEvent.emit(this.name);
        }
    }
}

class Something {
    disposables = new CompositeDisposable();
    constructor(user: User) {
        this.disposables.add(user.onNameChangeEvent.on(() => {}));
    }
    destroy() {
        this.disposables.dispose();
    }
}
*/
Object.defineProperty(exports, "__esModule", { value: true });
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 = [];
        for (let k of this._handlersByEventName.keys()) {
            if (typeof (k) === 'string') {
                ret.push(k);
            }
        }
        return ret;
    }
    getEventSymbols() {
        let ret = [];
        for (let k of this._handlersByEventName.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;
        for (let eventName of this._handlersByEventName.keys()) {
            result += this._handlersByEventName.get(eventName).length;
        }
        return result;
    }
}
exports.Emitter = Emitter;