import { observable } from "mobx" import { tweakPlainObject } from "../tweaker/tweakPlainObject" import { isPlainObject, setOwnProp } from "../utils" import { withErrorPathSegment } from "../utils/errorDiagnostics" import { type FromSnapshotContext, internalFromSnapshot, observableOptions, registerSnapshotter, } from "./fromSnapshot" import type { SnapshotInOfObject } from "./SnapshotOf" import { SnapshotterAndReconcilerPriority } from "./SnapshotterAndReconcilerPriority" function fromPlainObjectSnapshot(sn: SnapshotInOfObject, ctx: FromSnapshotContext): object { const plainObj: Record = {} const snKeys = Object.keys(sn) const snKeysLen = snKeys.length for (let i = 0; i < snKeysLen; i++) { const k = snKeys[i] const v = sn[k] setOwnProp( plainObj, k, withErrorPathSegment(k, () => internalFromSnapshot(v, ctx)) ) } return tweakPlainObject( observable.object(plainObj, undefined, observableOptions), undefined, undefined, true, false ) } /** * @internal */ export function registerFromPlainObjectSnapshotter() { registerSnapshotter(SnapshotterAndReconcilerPriority.PlainObject, (sn, ctx) => { if (isPlainObject(sn)) { return fromPlainObjectSnapshot(sn, ctx) } return undefined }) }