Skip to content

Commit ab92f00

Browse files
authored
Merge pull request #1410 from mobxjs/custom-type-env
[CustomType] Add 'environment' as second argument for options.fromSnapshot
2 parents aaf8041 + 8dc35f3 commit ab92f00

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

packages/mobx-state-tree/__tests__/core/custom-type.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ class Decimal {
3030
{
3131
const DecimalPrimitive = types.custom<string, Decimal>({
3232
name: "Decimal",
33-
fromSnapshot(value: string) {
33+
fromSnapshot(value: string, env: any) {
34+
if (env && env.test) env.test(value)
3435
return new Decimal(value)
3536
},
3637
toSnapshot(value: Decimal) {
@@ -106,6 +107,12 @@ class Decimal {
106107
p.stop()
107108
expect(p.patches).toMatchSnapshot()
108109
})
110+
111+
test("passes environment to fromSnapshot", () => {
112+
const env = { test: jest.fn() }
113+
Wallet.create({ balance: "3.0" }, env)
114+
expect(env.test).toBeCalledWith("3.0")
115+
})
109116
}
110117

111118
{

packages/mobx-state-tree/src/types/utility-types/custom.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import {
1313
export interface CustomTypeOptions<S, T> {
1414
/** Friendly name */
1515
name: string
16-
/** given a serialized value, how to turn it into the target type */
17-
fromSnapshot(snapshot: S): T
16+
/** given a serialized value and environment, how to turn it into the target type */
17+
fromSnapshot(snapshot: S, env?: any): T
1818
/** return the serialization of the current value */
1919
toSnapshot(value: T): S
2020
/** if true, this is a converted value, if false, it's a snapshot */
@@ -33,8 +33,8 @@ export interface CustomTypeOptions<S, T> {
3333
* export interface CustomTypeOptions<S, T> {
3434
* // Friendly name
3535
* name: string
36-
* // given a serialized value, how to turn it into the target type
37-
* fromSnapshot(snapshot: S): T
36+
* // given a serialized value and environment, how to turn it into the target type
37+
* fromSnapshot(snapshot: S, env: any): T
3838
* // return the serialization of the current value
3939
* toSnapshot(value: T): S
4040
* // if true, this is a converted value, if false, it's a snapshot
@@ -116,7 +116,7 @@ export class CustomType<S, T> extends SimpleType<S | T, S, T> {
116116
): this["N"] {
117117
const valueToStore: T = this.options.isTargetType(initialValue)
118118
? (initialValue as T)
119-
: this.options.fromSnapshot(initialValue as S)
119+
: this.options.fromSnapshot(initialValue as S, parent && parent.root.environment)
120120
return createScalarNode(this, parent, subpath, environment, valueToStore)
121121
}
122122

@@ -132,7 +132,9 @@ export class CustomType<S, T> extends SimpleType<S | T, S, T> {
132132
return current
133133
}
134134
}
135-
const valueToStore: T = isSnapshot ? this.options.fromSnapshot(value as S) : (value as T)
135+
const valueToStore: T = isSnapshot
136+
? this.options.fromSnapshot(value as S, parent.root.environment)
137+
: (value as T)
136138
const newNode = this.instantiate(parent, subpath, undefined, valueToStore)
137139
current.die() // noop if detaching
138140
return newNode

0 commit comments

Comments
 (0)