export default class EventBus {
    constructor () {
        this.events = []
    }

    on (event, fn, prio = 10) {
        if (!this.events[event]) {
            this.events[event] = []
        }

        this.events[event].push({
            fn,
            prio
        })

        this.events[event].sort((a, b) => +(a.prio > b.prio) || +(a.prio === b.prio) - 1)
    }

    emit (event, data) {
        if (this.events[event]) {
            this.events.forEach(ev => ev.fn(data))
        }
    }
}
