Files
CoreID/app/assets/app/service/EventBus.service.js
garrettmills f06ff83dce
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing
Move all front-end public field definitions into constructors for iOS support
2020-10-28 19:53:07 -05:00

40 lines
785 B
JavaScript

class Event {
constructor(name) {
this.name = name
this.firings = []
this.subscriptions = []
}
subscribe(handler) {
if ( typeof handler !== 'function' ) {
throw new TypeError('Event subscription handlers must be functions.')
}
this.subscriptions.push(handler)
}
async fire(...args) {
this.firings.push({ args })
return Promise.all(this.subscriptions.map(x => x(...args)))
}
}
class EventBusService {
constructor() {
this._events = {}
}
event(name) {
if ( !this._events[name] ) {
this._events[name] = new Event(name)
}
return this._events[name]
}
}
const event_bus = new EventBusService()
export { event_bus, Event }