Implement a basic benchmark test, and deprecate derivation.map()/flatMap()
This starts a new workspace at `packages/benchmarks` where future benchmarks are going to sit. For now, it only contains a basic profile of a `sequence.play()` setup. It also removes all uses of `AbstractDerivation.map()/flatMap()` and uses prisms instead.
This commit is contained in:
parent
45b548660c
commit
ae8be59366
26 changed files with 37584 additions and 56 deletions
|
@ -1,10 +1,11 @@
|
|||
import type {IdentityDerivationProvider} from './Atom'
|
||||
import {val} from './Atom'
|
||||
import type {Pointer} from './pointer'
|
||||
import pointer from './pointer'
|
||||
import type {IBox} from './Box'
|
||||
import Box from './Box'
|
||||
import type {$FixMe, $IntentionalAny} from './types'
|
||||
import {valueDerivation} from './Atom'
|
||||
import prism from './derivations/prism/prism'
|
||||
|
||||
/**
|
||||
* Allows creating pointer-derivations where the pointer can be switched out.
|
||||
|
@ -48,12 +49,13 @@ export default class PointerProxy<O extends {}>
|
|||
* @param path - The path to create the derivation at.
|
||||
*/
|
||||
getIdentityDerivation(path: Array<string | number>) {
|
||||
return this._currentPointerBox.derivation.flatMap((p) => {
|
||||
return prism(() => {
|
||||
const currentPointer = this._currentPointerBox.derivation.getValue()
|
||||
const subPointer = path.reduce(
|
||||
(pointerSoFar, pathItem) => (pointerSoFar as $IntentionalAny)[pathItem],
|
||||
p,
|
||||
currentPointer,
|
||||
)
|
||||
return valueDerivation(subPointer)
|
||||
return val(subPointer)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -251,10 +251,9 @@ export default abstract class AbstractDerivation<V> implements IDerivation<V> {
|
|||
protected _becomeCold() {}
|
||||
|
||||
/**
|
||||
* Creates a new derivation from this derivation using the provided mapping function. The new derivation's value will be
|
||||
* `fn(thisDerivation.getValue())`.
|
||||
* A simple mapping function similar to Array.map()
|
||||
*
|
||||
* @param fn - The mapping function to use. Note: it accepts a plain value, not a derivation.
|
||||
* @deprecated This is a remnant of the old monadic api. Now it's functionally equal to `prism(() => fn(der.getValue()))`, so use that instead.
|
||||
*/
|
||||
map<T>(fn: (v: V) => T): IDerivation<T> {
|
||||
return map(this, fn)
|
||||
|
@ -264,6 +263,8 @@ export default abstract class AbstractDerivation<V> implements IDerivation<V> {
|
|||
* Same as {@link AbstractDerivation.map}, but the mapping function can also return a derivation, in which case the derivation returned
|
||||
* by `flatMap` takes the value of that derivation.
|
||||
*
|
||||
* @deprecated This is a remnant of the old monadic api. Now it's functionally equal to `prism(() => val(fn(val(der))))`
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* // Simply using map() here would return the inner derivation when we call getValue()
|
||||
|
|
|
@ -21,7 +21,7 @@ const makeMapDerivationClass = () =>
|
|||
|
||||
let cls: ReturnType<typeof makeMapDerivationClass> | undefined = undefined
|
||||
|
||||
export default function flatMap<V, R>(
|
||||
export default function map<V, R>(
|
||||
dep: IDerivation<V>,
|
||||
fn: (v: V) => R,
|
||||
): IDerivation<R> {
|
||||
|
|
|
@ -4,7 +4,6 @@
|
|||
import Atom, {val} from '../../Atom'
|
||||
import Ticker from '../../Ticker'
|
||||
import type {$FixMe, $IntentionalAny} from '../../types'
|
||||
import ConstantDerivation from '../ConstantDerivation'
|
||||
import iterateAndCountTicks from '../iterateAndCountTicks'
|
||||
import prism, {PrismDerivation} from './prism'
|
||||
|
||||
|
@ -31,8 +30,8 @@ describe('prism', () => {
|
|||
expect(changes).toMatchObject(['foo2boo'])
|
||||
})
|
||||
it('should only collect immediate dependencies', () => {
|
||||
const aD = new ConstantDerivation(1)
|
||||
const bD = aD.map((v) => v * 2)
|
||||
const aD = prism(() => 1)
|
||||
const bD = prism(() => aD.getValue() * 2)
|
||||
const cD = prism(() => {
|
||||
return bD.getValue()
|
||||
})
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue