From 73bb484d5092203e2d8ddc2ad0ce872f2af3d53b Mon Sep 17 00:00:00 2001 From: Brian Hung Date: Fri, 11 Aug 2023 17:46:51 -0700 Subject: [PATCH 1/4] add parent to preProcessor and postProcessor --- .../types/utility-types/snapshotProcessor.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) 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..52d35cb72 100644 --- a/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts +++ b/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts @@ -59,9 +59,9 @@ class SnapshotProcessor extends BaseType< return `snapshotProcessor(${this._subtype.describe()})` } - private preProcessSnapshot(sn: this["C"]): IT["CreationType"] { + private preProcessSnapshot(sn: this["C"], parent: AnyObjectNode | null): IT["CreationType"] { if (this._processors.preProcessor) { - return this._processors.preProcessor.call(null, sn) + return this._processors.preProcessor.call(null, sn, parent) } return sn as any } @@ -74,9 +74,9 @@ 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) as any } return sn } @@ -87,7 +87,7 @@ class SnapshotProcessor extends BaseType< const oldGetSnapshot = node.getSnapshot node.getSnapshot = () => { - return this.postProcessSnapshot(oldGetSnapshot.call(node)) as any + return this.postProcessSnapshot(oldGetSnapshot.call(node), node.parent ? node.parent.storedValue : null) as any } if (!isUnionType(this._subtype)) { @@ -105,7 +105,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 +124,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 +136,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 ? node.parent.storedValue : null) : sn } isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult { @@ -171,7 +171,7 @@ class SnapshotProcessor extends BaseType< if (!(this._subtype instanceof ComplexType)) { return false } - const processedSn = this.preProcessSnapshot(snapshot) + const processedSn = this.preProcessSnapshot(snapshot, current.parent ? current.parent.storedValue : null) return this._subtype.isMatchingSnapshotId(current as any, processedSn) } } @@ -205,12 +205,12 @@ export interface ISnapshotProcessors { /** * Function that transforms an input snapshot. */ - preProcessor?(snapshot: CustomC): C + preProcessor?(snapshot: CustomC, parent: AnyObjectNode | null): C /** * Function that transforms an output snapshot. * @param snapshot */ - postProcessor?(snapshot: S): CustomS + postProcessor?(snapshot: S, parent: AnyObjectNode | null): CustomS } /** From 54a3f6104040affd2d4e47bb29995f4d77c3f460 Mon Sep 17 00:00:00 2001 From: BrianHung Date: Sun, 13 Aug 2023 16:56:48 -0700 Subject: [PATCH 2/4] fix preProcessSnapshot default null parent --- .../types/utility-types/snapshotProcessor.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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 52d35cb72..23af9fc07 100644 --- a/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts +++ b/packages/mobx-state-tree/src/types/utility-types/snapshotProcessor.ts @@ -59,7 +59,10 @@ class SnapshotProcessor extends BaseType< return `snapshotProcessor(${this._subtype.describe()})` } - private preProcessSnapshot(sn: this["C"], parent: AnyObjectNode | null): IT["CreationType"] { + private preProcessSnapshot( + sn: this["C"], + parent: AnyObjectNode | null = null + ): IT["CreationType"] { if (this._processors.preProcessor) { return this._processors.preProcessor.call(null, sn, parent) } @@ -87,7 +90,10 @@ class SnapshotProcessor extends BaseType< const oldGetSnapshot = node.getSnapshot node.getSnapshot = () => { - return this.postProcessSnapshot(oldGetSnapshot.call(node), node.parent ? node.parent.storedValue : null) as any + return this.postProcessSnapshot( + oldGetSnapshot.call(node), + node.parent ? node.parent.storedValue : null + ) as any } if (!isUnionType(this._subtype)) { @@ -136,7 +142,9 @@ class SnapshotProcessor extends BaseType< getSnapshot(node: this["N"], applyPostProcess: boolean = true): this["S"] { const sn = this._subtype.getSnapshot(node) - return applyPostProcess ? this.postProcessSnapshot(sn, node.parent ? node.parent.storedValue : null) : sn + return applyPostProcess + ? this.postProcessSnapshot(sn, node.parent ? node.parent.storedValue : null) + : sn } isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult { @@ -171,7 +179,10 @@ class SnapshotProcessor extends BaseType< if (!(this._subtype instanceof ComplexType)) { return false } - const processedSn = this.preProcessSnapshot(snapshot, current.parent ? current.parent.storedValue : null) + const processedSn = this.preProcessSnapshot( + snapshot, + current.parent ? current.parent.storedValue : null + ) return this._subtype.isMatchingSnapshotId(current as any, processedSn) } } From ec38a226deb3b1f1d28595a6848cf44e2509b829 Mon Sep 17 00:00:00 2001 From: BrianHung Date: Mon, 14 Aug 2023 11:03:12 -0700 Subject: [PATCH 3/4] fix types --- .../types/utility-types/snapshotProcessor.ts | 32 ++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) 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 23af9fc07..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 */ @@ -64,7 +65,7 @@ class SnapshotProcessor extends BaseType< parent: AnyObjectNode | null = null ): IT["CreationType"] { if (this._processors.preProcessor) { - return this._processors.preProcessor.call(null, sn, parent) + return this._processors.preProcessor.call(null, sn, parent && parent.storedValue) } return sn as any } @@ -79,7 +80,11 @@ class SnapshotProcessor extends BaseType< private postProcessSnapshot(sn: IT["SnapshotType"], parent: AnyObjectNode | null): this["S"] { if (this._processors.postProcessor) { - return this._processors.postProcessor.call(null, sn, parent) as any + return this._processors.postProcessor.call( + null, + sn, + parent && parent.storedValue + ) as any } return sn } @@ -89,12 +94,8 @@ class SnapshotProcessor extends BaseType< proxyNodeTypeMethods(node.type, this, "create") const oldGetSnapshot = node.getSnapshot - node.getSnapshot = () => { - return this.postProcessSnapshot( - oldGetSnapshot.call(node), - node.parent ? node.parent.storedValue : null - ) as any - } + node.getSnapshot = () => + this.postProcessSnapshot(oldGetSnapshot.call(node), node.parent) as any if (!isUnionType(this._subtype)) { node.getReconciliationType = () => { @@ -142,9 +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, node.parent ? node.parent.storedValue : null) - : sn + return applyPostProcess ? this.postProcessSnapshot(sn, node.parent) : sn } isValidSnapshot(value: this["C"], context: IValidationContext): IValidationResult { @@ -179,10 +178,7 @@ class SnapshotProcessor extends BaseType< if (!(this._subtype instanceof ComplexType)) { return false } - const processedSn = this.preProcessSnapshot( - snapshot, - current.parent ? current.parent.storedValue : null - ) + const processedSn = this.preProcessSnapshot(snapshot, current.parent) return this._subtype.isMatchingSnapshotId(current as any, processedSn) } } @@ -216,12 +212,12 @@ export interface ISnapshotProcessors { /** * Function that transforms an input snapshot. */ - preProcessor?(snapshot: CustomC, parent: AnyObjectNode | null): C + preProcessor?(snapshot: CustomC, parent: IAnyStateTreeNode | null): C /** * Function that transforms an output snapshot. * @param snapshot */ - postProcessor?(snapshot: S, parent: AnyObjectNode | null): CustomS + postProcessor?(snapshot: S, parent: IAnyStateTreeNode | null): CustomS } /** From af3c8e71d97d342fbcbf204c2eb0bbed7a7f28c7 Mon Sep 17 00:00:00 2001 From: BrianHung Date: Mon, 14 Aug 2023 11:06:35 -0700 Subject: [PATCH 4/4] fix bug where AnyObjectNode instead of IAnyStateTreeNode #2050 --- .../mobx-state-tree/src/types/utility-types/optional.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) 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