Remove Derivation.tapImmediate()

This is now just an extra flag on `Derivation.onChange()`
This commit is contained in:
Aria Minaei 2022-12-01 13:45:27 +01:00
parent f2bb24ef99
commit a24a149a52
10 changed files with 60 additions and 59 deletions

View file

@ -18,9 +18,13 @@ export interface IDerivation<V> {
isHot: boolean
/**
* Returns a `Tappable` of the changes of this derivation.
* Calls `listener` with a fresh value every time the prism _has_ a new value, throttled by Ticker.
*/
onChange(ticker: Ticker, listener: (v: V) => void): VoidFn
onChange(
ticker: Ticker,
listener: (v: V) => void,
immediate?: boolean,
): VoidFn
onStale(cb: () => void): VoidFn
@ -29,17 +33,6 @@ export interface IDerivation<V> {
*/
keepHot(): VoidFn
/**
* Convenience method that taps (subscribes to) the derivation using `this.changes(ticker).tap(fn)` and immediately calls
* the callback with the current value.
*
* @param ticker - The ticker to use for batching.
* @param fn - The callback to call on update.
*
* @see onChange
*/
tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn
/**
* Add a derivation as a dependent of this derivation.
*

View file

@ -222,8 +222,18 @@ class PrismDerivation<V> implements IDerivation<V> {
return this._state.hot
}
onChange(ticker: Ticker, listener: (v: V) => void): VoidFn {
return new DerivationEmitter(this, ticker).tappable().tap(listener)
onChange(
ticker: Ticker,
listener: (v: V) => void,
immediate: boolean = false,
): VoidFn {
const unsubscribe = new DerivationEmitter(this, ticker)
.tappable()
.tap(listener)
if (immediate) {
listener(this.getValue())
}
return unsubscribe
}
/**
@ -245,21 +255,6 @@ class PrismDerivation<V> implements IDerivation<V> {
return this.onStale(() => {})
}
/**
* Convenience method that taps (subscribes to) the derivation using `this.changes(ticker).tap(fn)` and immediately calls
* the callback with the current value.
*
* @param ticker - The ticker to use for batching.
* @param fn - The callback to call on update.
*
* @see onChange
*/
tapImmediate(ticker: Ticker, fn: (cb: V) => void): VoidFn {
const untap = this.onChange(ticker, fn)
fn(this.getValue())
return untap
}
/**
* Add a derivation as a dependent of this derivation.
*

View file

@ -72,13 +72,6 @@ export default class Tappable<V> {
}
}
/*
* tapImmediate(cb: Listener<V>): Untap {
* const ret = this.tap(cb)
* return ret
* }
*/
private _removeTapperById(id: number) {
this._tappers.delete(id)
this._check()