mobx-keystone-mindreframer
Version:
A MobX powered state management solution based on data trees with first class support for Typescript, snapshots, patches and much more
28 lines (25 loc) • 847 B
text/typescript
import { observable } from "mobx"
import { tweakArray } from "../tweaker/tweakArray"
import { isArray } from "../utils"
import {
FromSnapshotContext,
internalFromSnapshot,
observableOptions,
registerSnapshotter,
} from "./fromSnapshot"
import { SnapshotInOfObject } from "./SnapshotOf"
import { SnapshotterAndReconcilerPriority } from "./SnapshotterAndReconcilerPriority"
function fromArraySnapshot(sn: SnapshotInOfObject<any>, ctx: FromSnapshotContext): any[] {
const arr = observable.array([] as any[], observableOptions)
const ln = sn.length
for (let i = 0; i < ln; i++) {
arr.push(internalFromSnapshot(sn[i], ctx))
}
return tweakArray(arr, undefined, true)
}
registerSnapshotter(SnapshotterAndReconcilerPriority.Array, (sn, ctx) => {
if (isArray(sn)) {
return fromArraySnapshot(sn, ctx)
}
return undefined
})