From f1cb8edc91ea56a0f9162cb436402521f711de10 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Thu, 1 Dec 2022 13:33:52 +0100 Subject: [PATCH] Simplify `Derivation.onStale()` --- .../derivations/DerivationValuelessEmitter.ts | 51 ------------------- .../dataverse/src/derivations/prism/prism.ts | 8 ++- 2 files changed, 6 insertions(+), 53 deletions(-) delete mode 100644 packages/dataverse/src/derivations/DerivationValuelessEmitter.ts diff --git a/packages/dataverse/src/derivations/DerivationValuelessEmitter.ts b/packages/dataverse/src/derivations/DerivationValuelessEmitter.ts deleted file mode 100644 index 45d8c9b..0000000 --- a/packages/dataverse/src/derivations/DerivationValuelessEmitter.ts +++ /dev/null @@ -1,51 +0,0 @@ -import Emitter from '../utils/Emitter' -import type {default as Tappable} from '../utils/Tappable' -import type {IDerivation} from './IDerivation' - -/** - * Like DerivationEmitter, but with a different performance model. DerivationValuelessEmitter emits every time the - * derivation is updated, even if the value didn't change, and tappers are called without the value. The advantage of - * this is that you have control over when the underlying derivation is freshened, it won't automatically be freshened - * by the emitter. - */ -export default class DerivationValuelessEmitter { - _derivation: IDerivation - _emitter: Emitter - _hadTappers: boolean - - constructor( - derivation: IDerivation, - readonly dontEmitValues: boolean = false, - ) { - this._derivation = derivation - this._emitter = new Emitter() - this._emitter.onNumberOfTappersChange(() => { - this._reactToNumberOfTappersChange() - }) - this._hadTappers = false - return this - } - - private _possiblyMarkAsStale = () => { - this._emitter.emit(undefined) - } - - _reactToNumberOfTappersChange() { - const hasTappers = this._emitter.hasTappers() - if (hasTappers !== this._hadTappers) { - this._hadTappers = hasTappers - if (hasTappers) { - this._derivation.addDependent(this._possiblyMarkAsStale) - } else { - this._derivation.removeDependent(this._possiblyMarkAsStale) - } - } - } - - /** - * The tappable associated with the emitter. You can use it to tap (subscribe to) the underlying derivation. - */ - tappable(): Tappable { - return this._emitter.tappable - } -} diff --git a/packages/dataverse/src/derivations/prism/prism.ts b/packages/dataverse/src/derivations/prism/prism.ts index d3cd2fe..dfaf6d7 100644 --- a/packages/dataverse/src/derivations/prism/prism.ts +++ b/packages/dataverse/src/derivations/prism/prism.ts @@ -3,7 +3,6 @@ import type {$IntentionalAny, VoidFn} from '../../types' import Stack from '../../utils/Stack' import type Tappable from '../../utils/Tappable' import DerivationEmitter from '../DerivationEmitter' -import DerivationValuelessEmitter from '../DerivationValuelessEmitter' import type {IDerivation} from '../IDerivation' import {isDerivation} from '../IDerivation' import { @@ -235,7 +234,12 @@ class PrismDerivation implements IDerivation { * Returns a tappable that fires every time the prism's state goes from `fresh-\>stale.` */ onStale(callback: () => void): VoidFn { - return new DerivationValuelessEmitter(this).tappable().tap(callback) + const untap = () => { + this.removeDependent(fn) + } + const fn = () => callback() + this.addDependent(fn) + return untap } /**