Change the signature of Derivation.changes() to Derivation.onChange()

This way, we no longer expose the `Tappable` interface so we can remove it later.
This commit is contained in:
Aria Minaei 2022-12-01 13:40:02 +01:00
parent f1cb8edc91
commit f2bb24ef99
5 changed files with 11 additions and 16 deletions

View file

@ -1,6 +1,5 @@
import type Ticker from '../Ticker' import type Ticker from '../Ticker'
import type {$IntentionalAny, VoidFn} from '../types' import type {$IntentionalAny, VoidFn} from '../types'
import type Tappable from '../utils/Tappable'
type IDependent = (msgComingFrom: IDerivation<$IntentionalAny>) => void type IDependent = (msgComingFrom: IDerivation<$IntentionalAny>) => void
@ -21,7 +20,7 @@ export interface IDerivation<V> {
/** /**
* Returns a `Tappable` of the changes of this derivation. * Returns a `Tappable` of the changes of this derivation.
*/ */
changes(ticker: Ticker): Tappable<V> onChange(ticker: Ticker, listener: (v: V) => void): VoidFn
onStale(cb: () => void): VoidFn onStale(cb: () => void): VoidFn
@ -37,7 +36,7 @@ export interface IDerivation<V> {
* @param ticker - The ticker to use for batching. * @param ticker - The ticker to use for batching.
* @param fn - The callback to call on update. * @param fn - The callback to call on update.
* *
* @see changes * @see onChange
*/ */
tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn

View file

@ -19,7 +19,7 @@ export default function* iterateOver<V>(
const ticker = new Ticker() const ticker = new Ticker()
const untap = d.changes(ticker).tap((v) => {}) const untap = d.onChange(ticker, (v) => {})
try { try {
while (true) { while (true) {

View file

@ -21,7 +21,7 @@ describe('prism', () => {
expect(d.getValue()).toEqual('fooboo') expect(d.getValue()).toEqual('fooboo')
const changes: Array<$FixMe> = [] const changes: Array<$FixMe> = []
d.changes(ticker).tap((c) => { d.onChange(ticker, (c) => {
changes.push(c) changes.push(c)
}) })
@ -112,7 +112,7 @@ describe('prism', () => {
return n return n
}) })
const untap = derivation.changes(ticker).tap((change) => { const untap = derivation.onChange(ticker, (change) => {
sequence.push({change}) sequence.push({change})
}) })
@ -172,7 +172,7 @@ describe('prism', () => {
return n return n
}) })
const untap = derivation.changes(ticker).tap((change) => { const untap = derivation.onChange(ticker, (change) => {
sequence.push({change}) sequence.push({change})
}) })

View file

@ -1,7 +1,6 @@
import type Ticker from '../../Ticker' import type Ticker from '../../Ticker'
import type {$IntentionalAny, VoidFn} from '../../types' import type {$IntentionalAny, VoidFn} from '../../types'
import Stack from '../../utils/Stack' import Stack from '../../utils/Stack'
import type Tappable from '../../utils/Tappable'
import DerivationEmitter from '../DerivationEmitter' import DerivationEmitter from '../DerivationEmitter'
import type {IDerivation} from '../IDerivation' import type {IDerivation} from '../IDerivation'
import {isDerivation} from '../IDerivation' import {isDerivation} from '../IDerivation'
@ -223,11 +222,8 @@ class PrismDerivation<V> implements IDerivation<V> {
return this._state.hot return this._state.hot
} }
/** onChange(ticker: Ticker, listener: (v: V) => void): VoidFn {
* Returns a `Tappable` of the changes of this derivation. return new DerivationEmitter(this, ticker).tappable().tap(listener)
*/
changes(ticker: Ticker): Tappable<V> {
return new DerivationEmitter(this, ticker).tappable()
} }
/** /**
@ -256,10 +252,10 @@ class PrismDerivation<V> implements IDerivation<V> {
* @param ticker - The ticker to use for batching. * @param ticker - The ticker to use for batching.
* @param fn - The callback to call on update. * @param fn - The callback to call on update.
* *
* @see changes * @see onChange
*/ */
tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn { tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn {
const untap = this.changes(ticker).tap(fn) const untap = this.onChange(ticker, fn)
fn(this.getValue()) fn(this.getValue())
return untap return untap
} }

View file

@ -20,7 +20,7 @@ describe(`v2 atom`, () => {
expect(d.getValue()).toEqual(0) expect(d.getValue()).toEqual(0)
const ticker = new Ticker() const ticker = new Ticker()
const changes: number[] = [] const changes: number[] = []
d.changes(ticker).tap((c) => { d.onChange(ticker, (c) => {
changes.push(c) changes.push(c)
}) })
a.setState({...data, bar: 1}) a.setState({...data, bar: 1})