window.EventBusListeners = {}

export default class EventBus {
	on(event, listener) {
		const listeners = window.EventBusListeners
		if (listeners[event] === undefined) {
			listeners[event] = []
		}
		listeners[event].push(listener)
	}

	emit(event) {
		const listeners = window.EventBusListeners[event]
		if (listeners instanceof Array && listeners.length > 0) {
			for (const listener of listeners) {
				listener.call(this)
			}
		}
	}

	remove(event, listener) {
		const listeners = window.EventBusListeners
		if (listeners[event] != null) {
			const newListeners = []
			listeners[event].forEach(_listener => {
				if (listener !== _listener) {
					newListeners.push(_listener)
				}
			})
			listeners[event] = newListeners
		}
	}
}