diff --git a/packages/mobx-state-tree/src/types/utility-types/optional.ts b/packages/mobx-state-tree/src/types/utility-types/optional.ts index 04c994c0f..f9565f553 100644 --- a/packages/mobx-state-tree/src/types/utility-types/optional.ts +++ b/packages/mobx-state-tree/src/types/utility-types/optional.ts @@ -13,10 +13,11 @@ import { BaseType, assertIsType, ExtractCSTWithSTN, - devMode + devMode, + IAnyStateTreeNode } from "../../internal" -type IFunctionReturn = (parent: AnyObjectNode | null) => T +type IFunctionReturn = (parent: IAnyStateTreeNode | null) => T type IOptionalValue = C | IFunctionReturn @@ -91,7 +92,9 @@ export class OptionalValue< getDefaultInstanceOrSnapshot(parent: AnyObjectNode | null): this["C"] | this["T"] { const defaultInstanceOrSnapshot = typeof this._defaultValue === "function" - ? (this._defaultValue as IFunctionReturn)(parent) + ? (this._defaultValue as IFunctionReturn)( + parent && parent.storedValue + ) : this._defaultValue // while static values are already snapshots and checked on types.optional diff --git a/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts b/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts index 80f6931d9..8df29342f 100644 --- a/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts +++ b/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts @@ -14,7 +14,8 @@ import { devMode, ComplexType, typeCheckFailure, - isUnionType + isUnionType, + IAnyStateTreeNode } from "../../internal" /** @hidden */ @@ -59,9 +60,12 @@ class SnapshotProcessor extends BaseType< return `snapshotProcessor(${this._subtype.describe()})` } - private preProcessSnapshot(sn: this["C"]): IT["CreationType"] { + private preProcessSnapshot( + sn: this["C"], + parent: AnyObjectNode | null = null + ): IT["CreationType"] { if (this._processors.preProcessor) { - return this._processors.preProcessor.call(null, sn) + return this._processors.preProcessor.call(null, sn, parent && parent.storedValue) } return sn as any } @@ -74,9 +78,13 @@ class SnapshotProcessor extends BaseType< } } - private postProcessSnapshot(sn: IT["SnapshotType"]): this["S"] { + private postProcessSnapshot(sn: IT["SnapshotType"], parent: AnyObjectNode | null): this["S"] { if (this._processors.postProcessor) { - return this._processors.postProcessor.call(null, sn) as any + return this._processors.postProcessor.call( + null, + sn, + parent && parent.storedValue + ) as any } return sn } @@ -86,9 +94,8 @@ class SnapshotProcessor extends BaseType< proxyNodeTypeMethods(node.type, this, "create") const oldGetSnapshot = node.getSnapshot - node.getSnapshot = () => { - return this.postProcessSnapshot(oldGetSnapshot.call(node)) as any - } + node.getSnapshot = () => + this.postProcessSnapshot(oldGetSnapshot.call(node), node.parent) as any if (!isUnionType(this._subtype)) { node.getReconciliationType = () => { @@ -105,7 +112,7 @@ class SnapshotProcessor extends BaseType< ): this["N"] { const processedInitialValue = isStateTreeNode(initialValue) ? initialValue - : this.preProcessSnapshot(initialValue) + : this.preProcessSnapshot(initialValue, parent) const node = this._subtype.instantiate( parent, subpath, @@ -124,7 +131,7 @@ class SnapshotProcessor extends BaseType< ): this["N"] { const node = this._subtype.reconcile( current, - isStateTreeNode(newValue) ? newValue : this.preProcessSnapshot(newValue), + isStateTreeNode(newValue) ? newValue : this.preProcessSnapshot(newValue, parent), parent, subpath ) as any @@ -136,7 +143,7 @@ class SnapshotProcessor extends BaseType< getSnapshot(node: this["N"], applyPostProcess: boolean = true): this["S"] { const sn = this._subtype.getSnapshot(node) - return applyPostProcess ? this.postProcessSnapshot(sn) : sn + return applyPostProcess ? this.postProcessSnapshot(sn, node.parent) : sn } isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult { @@ -171,7 +178,7 @@ class SnapshotProcessor extends BaseType< if (!(this._subtype instanceof ComplexType)) { return false } - const processedSn = this.preProcessSnapshot(snapshot) + const processedSn = this.preProcessSnapshot(snapshot, current.parent) return this._subtype.isMatchingSnapshotId(current as any, processedSn) } } @@ -205,12 +212,12 @@ export interface ISnapshotProcessors { /** * Function that transforms an input snapshot. */ - preProcessor?(snapshot: CustomC): C + preProcessor?(snapshot: CustomC, parent: IAnyStateTreeNode | null): C /** * Function that transforms an output snapshot. * @param snapshot */ - postProcessor?(snapshot: S): CustomS + postProcessor?(snapshot: S, parent: IAnyStateTreeNode | null): CustomS } /**