From 2bd1dc85a9794e88f9c1736ff93a74bf0fe4ec43 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Thu, 1 Dec 2022 15:21:00 +0100 Subject: [PATCH] Remove `EventEmitter --- packages/dataverse/src/Box.ts | 11 ++-- packages/dataverse/src/utils/EventEmitter.ts | 56 -------------------- 2 files changed, 6 insertions(+), 61 deletions(-) delete mode 100644 packages/dataverse/src/utils/EventEmitter.ts diff --git a/packages/dataverse/src/Box.ts b/packages/dataverse/src/Box.ts index d92336b..5ad0e6d 100644 --- a/packages/dataverse/src/Box.ts +++ b/packages/dataverse/src/Box.ts @@ -1,6 +1,5 @@ import type {Prism} from './prism/Interface' import prism from './prism/prism' -import EventEmitter from './utils/EventEmitter' /** * Common interface for Box types. Boxes wrap a single value. @@ -39,7 +38,7 @@ export interface IBox { */ export default class Box implements IBox { private _publicPrism: Prism - private _emitter = new EventEmitter() + private _changeListeners = new Set<(newVal: V) => void>() /** * @param _value - The initial value of the Box. @@ -51,8 +50,8 @@ export default class Box implements IBox { protected _value: V, ) { const subscribe = (listener: (val: V) => void) => { - this._emitter.addEventListener('change', listener) - return () => this._emitter.removeEventListener('change', listener) + this._changeListeners.add(listener) + return () => this._changeListeners.delete(listener) } const getValue = () => this._value @@ -69,7 +68,9 @@ export default class Box implements IBox { set(v: V) { if (v === this._value) return this._value = v - this._emitter.emit('change', v) + this._changeListeners.forEach((listener) => { + listener(v) + }) } /** diff --git a/packages/dataverse/src/utils/EventEmitter.ts b/packages/dataverse/src/utils/EventEmitter.ts deleted file mode 100644 index 1f8c1bf..0000000 --- a/packages/dataverse/src/utils/EventEmitter.ts +++ /dev/null @@ -1,56 +0,0 @@ -import forEach from 'lodash-es/forEach' -import without from 'lodash-es/without' -import type {$FixMe} from '../types' - -type Listener = (v: $FixMe) => void - -/** - * A simple barebones event emitter - */ -export default class EventEmitter { - _listenersByType: {[eventName: string]: Array} - constructor() { - this._listenersByType = {} - } - - addEventListener(eventName: string, listener: Listener) { - const listeners = - this._listenersByType[eventName] || - (this._listenersByType[eventName] = []) - - listeners.push(listener) - - return this - } - - removeEventListener(eventName: string, listener: Listener) { - const listeners = this._listenersByType[eventName] - if (listeners) { - const newListeners = without(listeners, listener) - if (newListeners.length === 0) { - delete this._listenersByType[eventName] - } else { - this._listenersByType[eventName] = newListeners - } - } - - return this - } - - emit(eventName: string, payload: unknown) { - const listeners = this.getListenersFor(eventName) - if (listeners) { - forEach(listeners, (listener) => { - listener(payload) - }) - } - } - - getListenersFor(eventName: string) { - return this._listenersByType[eventName] - } - - hasListenersFor(eventName: string) { - return this.getListenersFor(eventName) ? true : false - } -}