export class EventBus {

    constructor() {

        this.events = {};
    }

    on(event, callback) {

        if (!this.events[event]) {

            this.events[event] = [];
        }

        this.events[event].push(callback);
    }

    emit(event, payload = {}) {

        // normal listeners
        if (this.events[event]) {

            this.events[event]
                .forEach(cb => cb(payload));
        }

        // wildcard listeners
        if (this.events['*']) {

            this.events['*']
                .forEach(cb =>
                    cb(event, payload)
                );
        }
    }
}
