From 867cf51acb4ec7696ccefe83de7aca5d1718d5d8 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Thu, 1 Dec 2022 15:16:37 +0100 Subject: [PATCH] Remove `Tappable` and `Emitter` --- packages/dataverse/src/prism/prism.ts | 2 +- packages/dataverse/src/utils/Emitter.test.ts | 19 ---- packages/dataverse/src/utils/Emitter.ts | 76 ---------------- packages/dataverse/src/utils/Tappable.ts | 92 -------------------- 4 files changed, 1 insertion(+), 188 deletions(-) delete mode 100644 packages/dataverse/src/utils/Emitter.test.ts delete mode 100644 packages/dataverse/src/utils/Emitter.ts delete mode 100644 packages/dataverse/src/utils/Tappable.ts diff --git a/packages/dataverse/src/prism/prism.ts b/packages/dataverse/src/prism/prism.ts index 2dc1888..d49692d 100644 --- a/packages/dataverse/src/prism/prism.ts +++ b/packages/dataverse/src/prism/prism.ts @@ -254,7 +254,7 @@ class PrismInstance implements Prism { } /** - * Returns a tappable that fires every time the prism's state goes from `fresh-\>stale.` + * Calls `callback` every time the prism's state goes from `fresh-\>stale.` Returns an `unsubscribe()` function. */ onStale(callback: () => void): VoidFn { const untap = () => { diff --git a/packages/dataverse/src/utils/Emitter.test.ts b/packages/dataverse/src/utils/Emitter.test.ts deleted file mode 100644 index bf2cf31..0000000 --- a/packages/dataverse/src/utils/Emitter.test.ts +++ /dev/null @@ -1,19 +0,0 @@ -import Emitter from './Emitter' - -describe('DataVerse.Emitter', () => { - it('should work', () => { - const e: Emitter = new Emitter() - e.emit('no one will see this') - e.emit('nor this') - - const tappedEvents: string[] = [] - const untap = e.tappable.tap((payload) => { - tappedEvents.push(payload) - }) - e.emit('foo') - e.emit('bar') - untap() - e.emit('baz') - expect(tappedEvents).toMatchObject(['foo', 'bar']) - }) -}) diff --git a/packages/dataverse/src/utils/Emitter.ts b/packages/dataverse/src/utils/Emitter.ts deleted file mode 100644 index 5cc3124..0000000 --- a/packages/dataverse/src/utils/Emitter.ts +++ /dev/null @@ -1,76 +0,0 @@ -import Tappable from './Tappable' - -type Tapper = (v: V) => void -type Untap = () => void - -/** - * An event emitter. Emit events that others can tap (subscribe to). - */ -export default class Emitter { - private _tappers: Map void> - private _lastTapperId: number - private _onNumberOfTappersChangeListener: undefined | ((n: number) => void) - - /** - * The Tappable associated with this emitter. You can use this to tap (subscribe to) events emitted. - */ - readonly tappable: Tappable - - constructor() { - this._lastTapperId = 0 - this._tappers = new Map() - this.tappable = new Tappable({ - tapToSource: (cb: Tapper) => this._tap(cb), - }) - } - - _tap(cb: Tapper): Untap { - const tapperId = this._lastTapperId++ - this._tappers.set(tapperId, cb) - this._onNumberOfTappersChangeListener?.(this._tappers.size) - return () => { - this._removeTapperById(tapperId) - } - } - - _removeTapperById(id: number) { - const oldSize = this._tappers.size - this._tappers.delete(id) - const newSize = this._tappers.size - if (oldSize !== newSize) { - this._onNumberOfTappersChangeListener?.(newSize) - } - } - - /** - * Emit a value. - * - * @param payload - The value to be emitted. - */ - emit(payload: V) { - for (const cb of this._tappers.values()) { - cb(payload) - } - } - - /** - * Checks whether the emitter has tappers (subscribers). - */ - hasTappers() { - return this._tappers.size !== 0 - } - - /** - * Calls callback when the number of tappers (subscribers) changes. - * - * @example - * ```ts - * emitter.onNumberOfTappersChange((n) => { - * console.log("number of tappers changed:", n) - * }) - * ``` - */ - onNumberOfTappersChange(cb: (n: number) => void) { - this._onNumberOfTappersChangeListener = cb - } -} diff --git a/packages/dataverse/src/utils/Tappable.ts b/packages/dataverse/src/utils/Tappable.ts deleted file mode 100644 index 560b9db..0000000 --- a/packages/dataverse/src/utils/Tappable.ts +++ /dev/null @@ -1,92 +0,0 @@ -type Untap = () => void -type UntapFromSource = () => void - -interface IProps { - tapToSource: (cb: (payload: V) => void) => UntapFromSource -} - -type Listener = ((v: V) => void) | (() => void) - -/** - * Represents a data-source that can be tapped (subscribed to). - */ -export default class Tappable { - private _props: IProps - private _tappers: Map - private _untapFromSource: null | UntapFromSource - private _lastTapperId: number - private _untapFromSourceTimeout: null | NodeJS.Timer = null - - constructor(props: IProps) { - this._lastTapperId = 0 - this._untapFromSource = null - this._props = props - this._tappers = new Map() - } - - private _check() { - if (this._untapFromSource) { - if (this._tappers.size === 0) { - this._scheduleToUntapFromSource() - /* - * this._untapFromSource() - * this._untapFromSource = null - */ - } - } else { - if (this._tappers.size !== 0) { - this._untapFromSource = this._props.tapToSource(this._cb) - } - } - } - - private _scheduleToUntapFromSource() { - if (this._untapFromSourceTimeout !== null) return - this._untapFromSourceTimeout = setTimeout(() => { - this._untapFromSourceTimeout = null - if (this._tappers.size === 0) { - this._untapFromSource!() - - this._untapFromSource = null - } - }, 0) - } - - private _cb: any = (arg: any): void => { - for (const cb of this._tappers.values()) { - cb(arg) - } - } - - /** - * Tap (subscribe to) the data source. - * - * @param cb - The callback to be called on a change. - */ - tap(cb: Listener): Untap { - const tapperId = this._lastTapperId++ - this._tappers.set(tapperId, cb) - this._check() - return () => { - this._removeTapperById(tapperId) - } - } - - private _removeTapperById(id: number) { - this._tappers.delete(id) - this._check() - } - - // /** - // * @deprecated - // */ - // map(transform: {bivarianceHack(v: V): T}['bivarianceHack']): Tappable { - // return new Tappable({ - // tapToSource: (cb: (v: T) => void) => { - // return this.tap((v: $IntentionalAny) => { - // return cb(transform(v)) - // }) - // }, - // }) - // } -}