From f2bb24ef994d249b6cbd6658532225f499da1dde Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Thu, 1 Dec 2022 13:40:02 +0100 Subject: [PATCH] Change the signature of `Derivation.changes()` to `Derivation.onChange()` This way, we no longer expose the `Tappable` interface so we can remove it later. --- packages/dataverse/src/derivations/IDerivation.ts | 5 ++--- packages/dataverse/src/derivations/iterateOver.ts | 2 +- .../dataverse/src/derivations/prism/prism.test.ts | 6 +++--- packages/dataverse/src/derivations/prism/prism.ts | 12 ++++-------- packages/dataverse/src/integration.test.ts | 2 +- 5 files changed, 11 insertions(+), 16 deletions(-) diff --git a/packages/dataverse/src/derivations/IDerivation.ts b/packages/dataverse/src/derivations/IDerivation.ts index ac10668..fc03d3f 100644 --- a/packages/dataverse/src/derivations/IDerivation.ts +++ b/packages/dataverse/src/derivations/IDerivation.ts @@ -1,6 +1,5 @@ import type Ticker from '../Ticker' import type {$IntentionalAny, VoidFn} from '../types' -import type Tappable from '../utils/Tappable' type IDependent = (msgComingFrom: IDerivation<$IntentionalAny>) => void @@ -21,7 +20,7 @@ export interface IDerivation { /** * Returns a `Tappable` of the changes of this derivation. */ - changes(ticker: Ticker): Tappable + onChange(ticker: Ticker, listener: (v: V) => void): VoidFn onStale(cb: () => void): VoidFn @@ -37,7 +36,7 @@ export interface IDerivation { * @param ticker - The ticker to use for batching. * @param fn - The callback to call on update. * - * @see changes + * @see onChange */ tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn diff --git a/packages/dataverse/src/derivations/iterateOver.ts b/packages/dataverse/src/derivations/iterateOver.ts index f800002..73681ec 100644 --- a/packages/dataverse/src/derivations/iterateOver.ts +++ b/packages/dataverse/src/derivations/iterateOver.ts @@ -19,7 +19,7 @@ export default function* iterateOver( const ticker = new Ticker() - const untap = d.changes(ticker).tap((v) => {}) + const untap = d.onChange(ticker, (v) => {}) try { while (true) { diff --git a/packages/dataverse/src/derivations/prism/prism.test.ts b/packages/dataverse/src/derivations/prism/prism.test.ts index 3251933..c955e54 100644 --- a/packages/dataverse/src/derivations/prism/prism.test.ts +++ b/packages/dataverse/src/derivations/prism/prism.test.ts @@ -21,7 +21,7 @@ describe('prism', () => { expect(d.getValue()).toEqual('fooboo') const changes: Array<$FixMe> = [] - d.changes(ticker).tap((c) => { + d.onChange(ticker, (c) => { changes.push(c) }) @@ -112,7 +112,7 @@ describe('prism', () => { return n }) - const untap = derivation.changes(ticker).tap((change) => { + const untap = derivation.onChange(ticker, (change) => { sequence.push({change}) }) @@ -172,7 +172,7 @@ describe('prism', () => { return n }) - const untap = derivation.changes(ticker).tap((change) => { + const untap = derivation.onChange(ticker, (change) => { sequence.push({change}) }) diff --git a/packages/dataverse/src/derivations/prism/prism.ts b/packages/dataverse/src/derivations/prism/prism.ts index dfaf6d7..5e7d80e 100644 --- a/packages/dataverse/src/derivations/prism/prism.ts +++ b/packages/dataverse/src/derivations/prism/prism.ts @@ -1,7 +1,6 @@ import type Ticker from '../../Ticker' import type {$IntentionalAny, VoidFn} from '../../types' import Stack from '../../utils/Stack' -import type Tappable from '../../utils/Tappable' import DerivationEmitter from '../DerivationEmitter' import type {IDerivation} from '../IDerivation' import {isDerivation} from '../IDerivation' @@ -223,11 +222,8 @@ class PrismDerivation implements IDerivation { return this._state.hot } - /** - * Returns a `Tappable` of the changes of this derivation. - */ - changes(ticker: Ticker): Tappable { - return new DerivationEmitter(this, ticker).tappable() + onChange(ticker: Ticker, listener: (v: V) => void): VoidFn { + return new DerivationEmitter(this, ticker).tappable().tap(listener) } /** @@ -256,10 +252,10 @@ class PrismDerivation implements IDerivation { * @param ticker - The ticker to use for batching. * @param fn - The callback to call on update. * - * @see changes + * @see onChange */ tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn { - const untap = this.changes(ticker).tap(fn) + const untap = this.onChange(ticker, fn) fn(this.getValue()) return untap } diff --git a/packages/dataverse/src/integration.test.ts b/packages/dataverse/src/integration.test.ts index 7bb03a8..5f17fec 100644 --- a/packages/dataverse/src/integration.test.ts +++ b/packages/dataverse/src/integration.test.ts @@ -20,7 +20,7 @@ describe(`v2 atom`, () => { expect(d.getValue()).toEqual(0) const ticker = new Ticker() const changes: number[] = [] - d.changes(ticker).tap((c) => { + d.onChange(ticker, (c) => { changes.push(c) }) a.setState({...data, bar: 1})