2018-11-05 05:06:39 -05:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
max-len,
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2020-05-19 05:02:56 -04:00
|
|
|
// Simple event emitter implementation, but has a slightly unusual API for
|
|
|
|
// removing specific listeners. If a specific listener needs to be removed
|
|
|
|
// (instead of all listeners), then it needs to use a "namespace":
|
|
|
|
// Create a listener on the foo event with bar namespace: .on 'foo.bar'
|
|
|
|
// Trigger all events for the foo event (including namespaces): .trigger 'foo'
|
|
|
|
// Remove all listeners for the foo event (including namespaces): .off 'foo'
|
|
|
|
// Remove a listener for the foo event with the bar namespace: .off 'foo.bar'
|
|
|
|
|
2022-08-24 07:49:15 -04:00
|
|
|
export default class EventEmitter {
|
2020-05-19 05:02:56 -04:00
|
|
|
on(event, callback) {
|
|
|
|
let namespace
|
|
|
|
if (!this.events) {
|
|
|
|
this.events = {}
|
|
|
|
}
|
|
|
|
;[event, namespace] = Array.from(event.split('.'))
|
|
|
|
if (!this.events[event]) {
|
|
|
|
this.events[event] = []
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return this.events[event].push({
|
|
|
|
callback,
|
2021-04-27 03:52:58 -04:00
|
|
|
namespace,
|
2020-05-19 05:02:56 -04:00
|
|
|
})
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
off(event) {
|
|
|
|
if (!this.events) {
|
|
|
|
this.events = {}
|
|
|
|
}
|
|
|
|
if (event != null) {
|
|
|
|
let namespace
|
|
|
|
;[event, namespace] = Array.from(event.split('.'))
|
|
|
|
if (namespace == null) {
|
|
|
|
// Clear all listeners for event
|
|
|
|
return delete this.events[event]
|
|
|
|
} else {
|
|
|
|
// Clear only namespaced listeners
|
|
|
|
const remaining_events = []
|
2021-05-05 09:05:04 -04:00
|
|
|
for (const callback of Array.from(this.events[event] || [])) {
|
2020-05-19 05:02:56 -04:00
|
|
|
if (callback.namespace !== namespace) {
|
|
|
|
remaining_events.push(callback)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return (this.events[event] = remaining_events)
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
} else {
|
|
|
|
// Remove all listeners
|
|
|
|
return (this.events = {})
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
trigger(event, ...args) {
|
|
|
|
if (!this.events) {
|
|
|
|
this.events = {}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
return Array.from(this.events[event] || []).map(callback =>
|
|
|
|
callback.callback(...Array.from(args || []))
|
|
|
|
)
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
emit(...args) {
|
|
|
|
return this.trigger(...Array.from(args || []))
|
|
|
|
}
|
2020-12-15 05:23:54 -05:00
|
|
|
}
|