| 1 | /** |
| 2 | * @abstract |
| 3 | * @implements {EventTarget} |
| 4 | */ |
| 5 | export class AbstractEventTarget { |
| 6 | constructor() { |
| 7 | this.listeners = {}; |
| 8 | } |
| 9 | |
| 10 | addEventListener(type, callback, _options) { |
| 11 | if (!this.listeners[type]) { |
| 12 | this.listeners[type] = []; |
| 13 | } |
| 14 | this.listeners[type].push(callback); |
| 15 | } |
| 16 | |
| 17 | dispatchEvent(event) { |
| 18 | if (!this.listeners[event.type] || this.listeners[event.type].length === 0) { |
| 19 | return true; |
| 20 | } |
| 21 | this.listeners[event.type].forEach(listener => { |
| 22 | listener(event); |
| 23 | }); |
| 24 | return true; |
| 25 | } |
| 26 | |
| 27 | removeEventListener(type, callback, _options) { |
| 28 | if (!this.listeners[type]) { |
| 29 | return; |
| 30 | } |
| 31 | const index = this.listeners[type].indexOf(callback); |
| 32 | if (index !== -1) { |
| 33 | this.listeners[type].splice(index, 1); |
| 34 | } |
| 35 | } |
| 36 | } |