| 4 | import { EventListener } from "./event_listener" |
| 5 | |
| 6 | export class Dispatcher implements BindingObserverDelegate { |
| 7 | readonly application: Application |
| 8 | private eventListenerMaps: Map<EventTarget, Map<string, EventListener>> |
| 9 | private started: boolean |
| 10 | |
| 11 | constructor(application: Application) { |
| 12 | this.application = application |
| 13 | this.eventListenerMaps = new Map() |
| 14 | this.started = false |
| 15 | } |
| 16 | |
| 17 | start() { |
| 18 | if (!this.started) { |
| 19 | this.started = true |
| 20 | this.eventListeners.forEach((eventListener) => eventListener.connect()) |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | stop() { |
| 25 | if (this.started) { |
| 26 | this.started = false |
| 27 | this.eventListeners.forEach((eventListener) => eventListener.disconnect()) |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | get eventListeners(): EventListener[] { |
| 32 | return Array.from(this.eventListenerMaps.values()).reduce( |
| 33 | (listeners, map) => listeners.concat(Array.from(map.values())), |
| 34 | [] as EventListener[] |
| 35 | ) |
| 36 | } |
| 37 | |
| 38 | // Binding observer delegate |
| 39 | |
| 40 | bindingConnected(binding: Binding) { |
| 41 | this.fetchEventListenerForBinding(binding).bindingConnected(binding) |
| 42 | } |
| 43 | |
| 44 | bindingDisconnected(binding: Binding, clearEventListeners = false) { |
| 45 | this.fetchEventListenerForBinding(binding).bindingDisconnected(binding) |
| 46 | if (clearEventListeners) this.clearEventListenersForBinding(binding) |
| 47 | } |
| 48 | |
| 49 | // Error handling |
| 50 | |
| 51 | handleError(error: Error, message: string, detail: object = {}) { |
| 52 | this.application.handleError(error, `Error ${message}`, detail) |
| 53 | } |
| 54 | |
| 55 | private clearEventListenersForBinding(binding: Binding) { |
| 56 | const eventListener = this.fetchEventListenerForBinding(binding) |
| 57 | if (!eventListener.hasBindings()) { |
| 58 | eventListener.disconnect() |
| 59 | this.removeMappedEventListenerFor(binding) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private removeMappedEventListenerFor(binding: Binding) { |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…