import { serializable, Serializable } from "typescript-serializable-mixin/index.js" import { AnyFunction, ClassUnion, Mixin } from "../../class/Mixin.js" import { FieldAtom } from "../../replica2/Atom.js" import { AtomState } from "../atom/Atom.js" import { CalculationFunction, CalculationModeSync } from "../CalculationMode.js" import { BoxImmutable, BoxUnbound, BoxUnboundPre } from "./Box.js" import { CalculableBoxUnbound } from "./CalculableBox.js" //--------------------------------------------------------------------------------------------------------------------- type ArrayMutation = { kind : 'splice', at : number, removeCount : number, newElements : unknown[] } | { kind : 'set', elements : unknown[] } //--------------------------------------------------------------------------------------------------------------------- @serializable({ id : 'ReactiveArrayQuark' }) export class ReactiveArrayQuark extends Mixin( [ BoxImmutable ], (base : ClassUnion) => class ReactiveArrayQuark extends base { mutations : ArrayMutation[] = [] appliedMutations : number = 0 value : BoxUnbound[] = undefined hasProposedValue () : boolean { return this.mutations.length > 0 } } ){} const zeroArrayQuark = new ReactiveArrayQuark(undefined) zeroArrayQuark.freeze() ReactiveArrayQuark.zero = zeroArrayQuark //--------------------------------------------------------------------------------------------------------------------- @serializable({ id : 'ReactiveArrayAtom' }) export class ReactiveArray extends Mixin( [ FieldAtom, CalculableBoxUnbound ], (base : ClassUnion) => // @ts-ignore class ReactiveArray extends base { // @ts-ignore declare static new (config? : Partial>) : ReactiveArray V : BoxUnbound[] OwnV : V immutable : ReactiveArrayQuark // level : AtomCalculationPriorityLevel = AtomCalculationPriorityLevel.DependsOnlyOnDependsOnlyOnUserInput lazy : boolean = false sync : boolean = true override persistent : boolean = true hasProposedValue () : boolean { return this.immutable.hasProposedValue() } buildDefaultImmutable () : ReactiveArrayQuark { const defaultBoxImmutable = new ReactiveArrayQuark(this) defaultBoxImmutable.previous = zeroArrayQuark as any return defaultBoxImmutable } itemBox (at : number) : BoxUnbound { return this.read()[ at ] } item (at : number) : V { return this.read()[ at ].read() } addMutation (mutation : ArrayMutation) { this.propagateStaleDeep(true) const quark = this.immutableForWrite() quark.mutations.push(mutation) this.state = AtomState.Stale if (this.graph) { this.graph.onDataWrite(this) } } splice (at : number, removeCount : number, ...newElements : unknown[]) { this.addMutation({ kind : 'splice', at, removeCount, newElements }) } push (...newElements : unknown[]) { const currentValue = this.read() as BoxUnbound[] this.splice(currentValue.length, 0, ...newElements) } map (func : AnyFunction) /*: MappedReactiveArrayAtom */{ const mappedReactiveArrayAtom = MappedReactiveArrayAtom as any const mappedArray = mappedReactiveArrayAtom.new() Object.assign(mappedArray, { source : this, func }) this.graph.addAtom(mappedArray) return mappedArray } get (at : number) { return this.itemBox(at).read() } set (at : number, newValue : unknown) { this.itemBox(at).write(newValue) } // @ts-ignore get context () : unknown { return this } set context (value : unknown) { } get calculation () : CalculationFunction { return this.calculate } set calculation (value : CalculationFunction) { } calculate () : BoxUnbound[] { const prevValue = this.immutable.read() let newValue : BoxUnbound[] = prevValue ? prevValue.slice() : [] const mutations = this.immutable.mutations for (let i = this.immutable.appliedMutations; i < mutations.length; i++) { const mutation = mutations[ i ] switch (mutation.kind) { case 'splice': const newBoxes = mutation.newElements.map(el => { const newBox = BoxUnbound.new(el) this.graph.addAtom(newBox) return newBox }) newValue.splice(mutation.at, mutation.removeCount, ...newBoxes) break case 'set': newValue = mutation.elements.map(el => { const newBox = BoxUnbound.new(el) this.graph.addAtom(newBox) return newBox }) break default: const a : never = mutation } } this.immutable.appliedMutations = mutations.length return newValue } write (value : V[]) { this.addMutation({ kind : 'set', elements : value }) } readValues () : this[ 'OwnV' ][] { return this.read().map(box => box.read()) } } ){} export interface ReactiveArray { OwnV : V V : BoxUnbound[] } //--------------------------------------------------------------------------------------------------------------------- @serializable({ id : 'MappedReactiveArrayAtom' }) export class MappedReactiveArrayAtom extends Mixin( [ ReactiveArray, Serializable ], (base : ClassUnion) => // @ts-ignore class MappedReactiveArrayAtom extends base { // @ts-ignore declare static new (config? : Partial>) : MappedReactiveArrayAtom source : ReactiveArray = undefined sourceQuark : ReactiveArrayQuark = undefined sourceQuarkAppliedMutations : number = 0 func : AnyFunction = undefined override persistent : boolean = true addMutation (mutation : ArrayMutation) { throw new Error("Mapped array is not mutable") } mapElements (elements : unknown[]) : CalculableBoxUnbound[] { return elements.map(el => { const newBox = CalculableBoxUnbound.new({ calculation : () => { let value = el // should only read 1 level while (value instanceof BoxUnboundPre) value = value.read() return this.func(value) } }) this.graph.addAtom(newBox) return newBox }) } applyMutation (newValue : BoxUnbound[], mutation : ArrayMutation) : BoxUnbound[] { switch (mutation.kind) { case 'splice': const newBoxes = this.mapElements(mutation.newElements) newValue.splice(mutation.at, mutation.removeCount, ...newBoxes) this.immutable.mutations.push({ kind : 'splice', at : mutation.at, removeCount : mutation.removeCount, newElements : newBoxes }) break case 'set': newValue = this.mapElements(mutation.elements) this.immutable.mutations.push({ kind : 'set', elements : newValue }) break default: const a : never = mutation } return newValue } calculate () : BoxUnbound[] { const sourceValue = this.source.read() if (this.sourceQuark) { const pendingSourceQuarks : ReactiveArrayQuark[] = [] let quark : ReactiveArrayQuark = this.source.immutable while (quark) { pendingSourceQuarks.push(quark) if (quark === this.sourceQuark) break quark = quark.previous } let newValue : BoxUnbound[] = this.immutable.read().slice() for (let i = pendingSourceQuarks.length - 1; i >= 0; i--) { const pendingSourceQuark = pendingSourceQuarks[ i ] const mutations = pendingSourceQuark.mutations const startAt = pendingSourceQuark === this.sourceQuark ? this.sourceQuarkAppliedMutations : 0 for (let i = startAt; i < mutations.length; i++) newValue = this.applyMutation(newValue, mutations[ i ]) } this.sourceQuark = this.source.immutable this.sourceQuarkAppliedMutations = this.source.immutable.appliedMutations return newValue } else { this.sourceQuark = this.source.immutable this.sourceQuarkAppliedMutations = this.source.immutable.appliedMutations return this.mapElements(sourceValue) } } } ){}