Skip to content

Commit

Permalink
add parent to preProcessor and postProcessor (#2065)
Browse files Browse the repository at this point in the history
* add parent to preProcessor and postProcessor

* fix preProcessSnapshot default null parent

* fix types

* fix bug where AnyObjectNode instead of IAnyStateTreeNode #2050
  • Loading branch information
BrianHung authored Aug 18, 2023
1 parent b6d9dcb commit 94bb7d0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
9 changes: 6 additions & 3 deletions packages/mobx-state-tree/src/types/utility-types/optional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import {
BaseType,
assertIsType,
ExtractCSTWithSTN,
devMode
devMode,
IAnyStateTreeNode
} from "../../internal"

type IFunctionReturn<T> = (parent: AnyObjectNode | null) => T
type IFunctionReturn<T> = (parent: IAnyStateTreeNode | null) => T

type IOptionalValue<C, T> = C | IFunctionReturn<C | T>

Expand Down Expand Up @@ -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<this["C"] | this["T"]>)(parent)
? (this._defaultValue as IFunctionReturn<this["C"] | this["T"]>)(
parent && parent.storedValue
)
: this._defaultValue

// while static values are already snapshots and checked on types.optional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
devMode,
ComplexType,
typeCheckFailure,
isUnionType
isUnionType,
IAnyStateTreeNode
} from "../../internal"

/** @hidden */
Expand Down Expand Up @@ -59,9 +60,12 @@ class SnapshotProcessor<IT extends IAnyType, CustomC, CustomS> 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
}
Expand All @@ -74,9 +78,13 @@ class SnapshotProcessor<IT extends IAnyType, CustomC, CustomS> 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
}
Expand All @@ -86,9 +94,8 @@ class SnapshotProcessor<IT extends IAnyType, CustomC, CustomS> 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 = () => {
Expand All @@ -105,7 +112,7 @@ class SnapshotProcessor<IT extends IAnyType, CustomC, CustomS> extends BaseType<
): this["N"] {
const processedInitialValue = isStateTreeNode(initialValue)
? initialValue
: this.preProcessSnapshot(initialValue)
: this.preProcessSnapshot(initialValue, parent)
const node = this._subtype.instantiate(
parent,
subpath,
Expand All @@ -124,7 +131,7 @@ class SnapshotProcessor<IT extends IAnyType, CustomC, CustomS> 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
Expand All @@ -136,7 +143,7 @@ class SnapshotProcessor<IT extends IAnyType, CustomC, CustomS> 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 {
Expand Down Expand Up @@ -171,7 +178,7 @@ class SnapshotProcessor<IT extends IAnyType, CustomC, CustomS> 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)
}
}
Expand Down Expand Up @@ -205,12 +212,12 @@ export interface ISnapshotProcessors<C, CustomC, S, CustomS> {
/**
* 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
}

/**
Expand Down

0 comments on commit 94bb7d0

Please sign in to comment.