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)) - // }) - // }, - // }) - // } -}