all files / common/global-event/ global-event-emmitter.ts

86.79% Statements 46/53
57.69% Branches 15/26
83.33% Functions 10/12
86% Lines 43/50
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                                                                                             
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
import { IGlobalEventEmmitter } from './global-event-emmiter.interface';
 
export class GlobalEventEmmitter implements IGlobalEventEmmitter {
    private static _instance: GlobalEventEmmitter;
 
    public static get instance() {
        if (!this._instance) {
            this._instance = new GlobalEventEmmitter();
        }
        return this._instance;
    }
 
    private _callbacks = {} as any;
 
    public off = (event: string, fn: () => void) => {
        return this.removeEventListener(event, fn);
    }
 
    public removeListener = (event: string, fn: () => void) => {
        return this.removeEventListener(event, fn);
    }
 
    public removeAllListeners = () => {
        return this.removeEventListener();
    }
 
    public removeEventListener = (event?: string, fn?: () => void) => {
        this._callbacks = this._callbacks || {};
 
        // all
        Iif (!event) {
            this._callbacks = {};
            return this;
        }
 
        // specific event
        const callbacks = this._callbacks[`$${event}`];
        Iif (!callbacks) {
            return this;
        }
 
        // remove all handlers
        Iif (!fn) {
            delete this._callbacks[`$${event}`];
            return this;
        }
 
        // remove specific handler
        let cb;
        for (let i = 0; i < callbacks.length; i++) {
            cb = callbacks[i];
            Eif (cb === fn || cb.fn === fn) {
                callbacks.splice(i, 1);
                break;
            }
        }
        return this;
    }
 
    public emit = (event: string, ...params: any[]) => {
        this._callbacks = this._callbacks || {};
        let callbacks = this._callbacks[`$${event}`];
 
        Eif (callbacks) {
            callbacks = callbacks.slice(0);
            // tslint:disable-next-line:prefer-for-of
            for (let i = 0; i < callbacks.length; ++i) {
                callbacks[i].apply(this, params);
            }
        }
 
        return this;
    }
 
    public listeners = (event: string) => {
        this._callbacks = this._callbacks || {};
        return this._callbacks[`$${event}`] || [];
    }
 
    public hasListeners = (event: string) => {
        return !!this.listeners(event).length;
    }
 
    public on = (event: string, fn: (params: any[]) => void) => {
        return this.addEventListener(event, fn);
    }
 
    public addEventListener = (event: string, fn: (params: any[]) => void) => {
        this._callbacks = this._callbacks || {};
        (this._callbacks[`$${event}`] = this._callbacks[`$${event}`] || []).push(fn);
        return this;
    }
}
 
// tslint:disable-next-line
(<any>window)['GlobalEventEmmitter'] = GlobalEventEmmitter;