Remove Derivation.tapImmediate()
This is now just an extra flag on `Derivation.onChange()`
This commit is contained in:
parent
f2bb24ef99
commit
a24a149a52
10 changed files with 60 additions and 59 deletions
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -13,9 +13,9 @@ import {Box, prism, Ticker, val} from '@theatre/dataverse'
|
|||
*
|
||||
* Without going into the details of `prism`, `Ticker`, and `val`, note that by wrapping our `ToolsetConfig` in a prism, our
|
||||
* ```ts
|
||||
* ... .tapImmediate(Ticker.raf, (toolset) => {
|
||||
* ... .onChange(Ticker.raf, (toolset) => {
|
||||
* set(toolset)
|
||||
* })
|
||||
* }, true)
|
||||
* ```
|
||||
* code will be called whenever `val(obj.props.exampleProp)` changes (whenever the user clicks the switch and the `onChange` callback is called).
|
||||
* This will ensure that our switch's value matches its state and is reflected in the UI via `set(toolset)`.
|
||||
|
@ -56,9 +56,13 @@ studio.extend({
|
|||
},
|
||||
])
|
||||
// listen to changes to this derivation using the requestAnimationFrame shared ticker
|
||||
.tapImmediate(Ticker.raf, (value) => {
|
||||
set(value)
|
||||
})
|
||||
.onChange(
|
||||
Ticker.raf,
|
||||
(value) => {
|
||||
set(value)
|
||||
},
|
||||
true,
|
||||
)
|
||||
|
||||
return untapFn
|
||||
},
|
||||
|
|
|
@ -35,9 +35,13 @@ const r3fExtension: IExtension = {
|
|||
},
|
||||
]
|
||||
})
|
||||
return calc.tapImmediate(Ticker.raf, () => {
|
||||
set(calc.getValue())
|
||||
})
|
||||
return calc.onChange(
|
||||
Ticker.raf,
|
||||
() => {
|
||||
set(calc.getValue())
|
||||
},
|
||||
true,
|
||||
)
|
||||
},
|
||||
'snapshot-editor': (set, studio) => {
|
||||
const {createSnapshot} = useExtensionStore.getState()
|
||||
|
@ -136,9 +140,13 @@ const r3fExtension: IExtension = {
|
|||
},
|
||||
]
|
||||
})
|
||||
return calc.tapImmediate(Ticker.raf, () => {
|
||||
set(calc.getValue())
|
||||
})
|
||||
return calc.onChange(
|
||||
Ticker.raf,
|
||||
() => {
|
||||
set(calc.getValue())
|
||||
},
|
||||
true,
|
||||
)
|
||||
},
|
||||
},
|
||||
panes: [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue