import { computed, observable } from "../../../node_modules/mobx/lib/mobx.module.js" import { Benchmark } from "../../../src/benchmark/Benchmark.js" import { Box } from "../../../src/chrono2/data/Box.js" import { CalculableBox } from "../../../src/chrono2/data/CalculableBox.js" import { CalculableBoxGen } from "../../../src/chrono2/data/CalculableBoxGen.js" import { Base } from "../../../src/class/Base.js" import { AnyFunction } from "../../../src/class/Mixin.js" //--------------------------------------------------------------------------------------------------------------------- // might look silly to have such "abstract" class, but idea is that hopefully v8 // will figure out that all its subclasses has the same "shape" and `.READ(), .WRITE()` // calls won't become "megamorphic" export class BoxAbstract { box : unknown READ () : V { throw new Error("Abstract method called") } WRITE (value : V) { throw new Error("Abstract method called") } } export interface BoxMobxRaw { get () : V set (value : V) } export class BoxMobx extends BoxAbstract { box : any constructor (box) { super() this.box = box } get () : V { return this.box.get() } set (value : V) { return this.box.set(value) } READ () : V { return this.box.get() } WRITE (value : V) { return this.box.set(value) } } export class BoxChronoGraph2 extends BoxAbstract { box : Box constructor (box) { super() this.box = box } read () : V { return this.box.read() } write (value : V) { return this.box.write(value) } READ () : V { return this.box.read() } WRITE (value : V) { return this.box.write(value) } } //--------------------------------------------------------------------------------------------------------------------- export interface ReactiveDataGenerator { rawBox (initialValue : V, name? : string) : RawBox box (initialValue : V, name? : string) : BoxAbstract rawComputed (func : AnyFunction, context? : any, name? : string) : RawBox computed (func : AnyFunction, context? : any, name? : string) : BoxAbstract computedStrict (func : AnyFunction, context? : any, name? : string) : BoxAbstract } //--------------------------------------------------------------------------------------------------------------------- export interface ReactiveDataGeneratorWithGenerators extends ReactiveDataGenerator { computedGen (func : AnyFunction>, context? : any, name? : string) : BoxAbstract } //--------------------------------------------------------------------------------------------------------------------- export class ReactiveDataGeneratorMobx implements ReactiveDataGenerator> { // used to measure the allocation performance rawBox (initialValue : V, name? : string) : BoxMobx { return observable.box(initialValue) } box (initialValue : V, name? : string) : BoxMobx { return new BoxMobx(observable.box(initialValue)) } // used to measure the allocation performance rawComputed (func : AnyFunction, context? : any, name? : string) : BoxMobxRaw { return computed(func, { keepAlive : true, context : context, name : name }) } computed (func : AnyFunction, context? : any, name? : string) : BoxMobx { return new BoxMobx(computed(func, { keepAlive : true, context : context, name : name })) } computedStrict (func : AnyFunction, context? : any, name? : string) : BoxMobx { return new BoxMobx(computed(func, { keepAlive : true, context : context, name : name })) } } export const reactiveDataGeneratorMobx = new ReactiveDataGeneratorMobx() //--------------------------------------------------------------------------------------------------------------------- export class ReactiveDataGeneratorChronoGraph2 extends Base implements ReactiveDataGeneratorWithGenerators> { // used to measure the allocation performance rawBox (initialValue : V, name? : string) : Box { return Box.new(initialValue, name) } box (initialValue : V, name? : string) : BoxChronoGraph2 { return new BoxChronoGraph2(Box.new(initialValue, name)) } // used to measure the allocation performance rawComputed (func : AnyFunction, context? : any, name? : string) : Box { return CalculableBox.new({ calculation : func, context : context, name : name }) } computed (func : AnyFunction, context? : any, name? : string) : BoxChronoGraph2 { return new BoxChronoGraph2(CalculableBox.new({ calculation : func, context : context, name : name })) } computedStrict (func : AnyFunction, context? : any, name? : string) : BoxChronoGraph2 { return new BoxChronoGraph2(CalculableBox.new({ calculation : func, context : context, name : name, lazy : false })) } computedGen (func : AnyFunction>, context? : any, name? : string) : BoxChronoGraph2 { return new BoxChronoGraph2(CalculableBoxGen.new({ calculation : func, context : context, name : name, lazy : false })) } } export const reactiveDataGeneratorChronoGraph2 = new ReactiveDataGeneratorChronoGraph2() //--------------------------------------------------------------------------------------------------------------------- export type ReactiveDataGenerationResult = { boxes : BoxAbstract[], counter : number } //--------------------------------------------------------------------------------------------------------------------- export type PostBenchInfo = { totalCount : number result : number } //--------------------------------------------------------------------------------------------------------------------- export class ReactiveDataBenchmark< StateT extends ReactiveDataGenerationResult = ReactiveDataGenerationResult, InfoT extends PostBenchInfo = PostBenchInfo > extends Benchmark { gatherInfo (state : StateT) : InfoT { const { boxes } = state return { totalCount : state.counter, result : boxes[ boxes.length - 1 ].READ() as number } as InfoT } stringifyInfo (info : InfoT) : string { return `Total calculations: ${info.totalCount}\nResult in last box: ${info.result}` } } //--------------------------------------------------------------------------------------------------------------------- declare const window : any declare const location : any export const launchIfStandaloneProcess = (run : AnyFunction, moduleName : string) : Res => { if ( typeof process !== undefined && process.version && String(process.argv[ 1 ]).includes(moduleName) || typeof window !== "undefined" && String(location.href).includes(moduleName) ) { return run() } }