Skip to content

Commit

Permalink
fix: mobx 6.1.0 compatibility (#1656 by @urugator)
Browse files Browse the repository at this point in the history
  • Loading branch information
urugator authored Jan 31, 2021
1 parent d767e80 commit a212858
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 40 deletions.
4 changes: 2 additions & 2 deletions packages/mobx-state-tree/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"jest": "^26.1.0",
"jest-junit": "^11.0.1",
"lerna": "^3.13.1",
"mobx": "^6.0.4",
"mobx": "^6.1.0",
"rollup": "^2.18.1",
"rollup-plugin-commonjs": "^10.0.0",
"rollup-plugin-filesize": "^9.0.1",
Expand All @@ -75,7 +75,7 @@
"typescript": "^3.5.3"
},
"peerDependencies": {
"mobx": "^6.0.0"
"mobx": "^6.1.0"
},
"keywords": [
"mobx",
Expand Down
19 changes: 10 additions & 9 deletions packages/mobx-state-tree/src/core/node/object-node.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// noinspection ES6UnusedImports
import { action, computed, reaction, _allowStateChangesInsideComputed, makeObservable } from "mobx"
import { action, computed, IComputedValue, reaction, _allowStateChangesInsideComputed } from "mobx"
import {
addHiddenFinalProp,
ComplexType,
Expand All @@ -15,7 +15,6 @@ import {
IJsonPatch,
IMiddleware,
IMiddlewareHandler,
invalidateComputed,
IReversibleJsonPatch,
NodeLifeCycle,
resolveNodeByPathParts,
Expand Down Expand Up @@ -117,6 +116,7 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {
private _initialSnapshot: C
private _cachedInitialSnapshot?: S
private _cachedInitialSnapshotCreated = false
private _snapshotComputed: IComputedValue<S>

constructor(
complexType: ComplexType<C, S, T>,
Expand All @@ -126,7 +126,7 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {
initialValue: C
) {
super(complexType, parent, subpath, environment)
makeObservable(this)
this._snapshotComputed = computed<S>(() => freeze(this.getSnapshot()))

this.unbox = this.unbox.bind(this)

Expand Down Expand Up @@ -177,7 +177,6 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {
}
}

@action
createObservableInstance(): void {
if (devMode()) {
if (this.state !== NodeLifeCycle.INITIALIZING) {
Expand Down Expand Up @@ -232,7 +231,7 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {

// NOTE: we need to touch snapshot, because non-observable
// "_observableInstanceState" field was touched
invalidateComputed(this, "snapshot")
;(this._snapshotComputed as any).trackAndCompute()

if (this.isRoot) this._addSnapshotReaction()

Expand Down Expand Up @@ -344,9 +343,8 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {
private _snapshotUponDeath?: S

// advantage of using computed for a snapshot is that nicely respects transactions etc.
@computed
get snapshot(): S {
return freeze(this.getSnapshot())
return this._snapshotComputed.get()
}

// NOTE: we use this method to get snapshot without creating @computed overhead
Expand Down Expand Up @@ -489,7 +487,6 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {
})
}

@action
detach(): void {
if (!this.isAlive) throw fail(`Error while detaching, node is not alive.`)

Expand Down Expand Up @@ -528,7 +525,6 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {
addHiddenFinalProp(this.storedValue, "toJSON", toJSON)
}

@action
die(): void {
if (!this.isAlive || this.state === NodeLifeCycle.DETACHING) return
this.aboutToDie()
Expand Down Expand Up @@ -710,6 +706,11 @@ export class ObjectNode<C, S, T> extends BaseNode<C, S, T> {

// #endregion
}
ObjectNode.prototype.createObservableInstance = action(
ObjectNode.prototype.createObservableInstance
)
ObjectNode.prototype.detach = action(ObjectNode.prototype.detach)
ObjectNode.prototype.die = action(ObjectNode.prototype.die)

/**
* @internal
Expand Down
6 changes: 2 additions & 4 deletions packages/mobx-state-tree/src/core/node/scalar-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SimpleType,
devMode
} from "../../internal"
import { action, makeObservable } from "mobx"
import { action } from "mobx"

/**
* @internal
Expand All @@ -31,8 +31,6 @@ export class ScalarNode<C, S, T> extends BaseNode<C, S, T> {
initialSnapshot: C
) {
super(simpleType, parent, subpath, environment)
makeObservable(this)

try {
this.storedValue = simpleType.createNewInstance(initialSnapshot)
} catch (e) {
Expand Down Expand Up @@ -95,7 +93,6 @@ export class ScalarNode<C, S, T> extends BaseNode<C, S, T> {
return `${this.type.name}@${path}${this.isAlive ? "" : " [dead]"}`
}

@action
die(): void {
if (!this.isAlive || this.state === NodeLifeCycle.DETACHING) return
this.aboutToDie()
Expand All @@ -118,3 +115,4 @@ export class ScalarNode<C, S, T> extends BaseNode<C, S, T> {
this.fireInternalHook(name)
}
}
ScalarNode.prototype.die = action(ScalarNode.prototype.die)
7 changes: 3 additions & 4 deletions packages/mobx-state-tree/src/core/type/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { action, makeObservable } from "mobx"
import { action } from "mobx"

import {
fail,
Expand Down Expand Up @@ -292,11 +292,9 @@ export abstract class BaseType<C, S, T, N extends BaseNode<any, any, any> = Base
readonly name: string

constructor(name: string) {
makeObservable(this)
this.name = name
}

@action
create(snapshot?: C, environment?: any) {
typecheckInternal(this, snapshot)
return this.instantiate(null, "", environment, snapshot!).value
Expand Down Expand Up @@ -368,6 +366,7 @@ export abstract class BaseType<C, S, T, N extends BaseNode<any, any, any> = Base

abstract getSubTypes(): IAnyType[] | IAnyType | null | typeof cannotDetermineSubtype
}
BaseType.prototype.create = action(BaseType.prototype.create)

/**
* @internal
Expand Down Expand Up @@ -396,7 +395,6 @@ export abstract class ComplexType<C, S, T> extends BaseType<C, S, T, ObjectNode<
super(name)
}

@action
create(snapshot: C = this.getDefaultSnapshot(), environment?: any) {
return super.create(snapshot, environment)
}
Expand Down Expand Up @@ -476,6 +474,7 @@ export abstract class ComplexType<C, S, T> extends BaseType<C, S, T, ObjectNode<
return null
}
}
ComplexType.prototype.create = action(ComplexType.prototype.create)

/**
* @internal
Expand Down
6 changes: 2 additions & 4 deletions packages/mobx-state-tree/src/types/complex-types/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import {
intercept,
IObservableArray,
observable,
observe,
makeObservable
observe
} from "mobx"
import {
addHiddenFinalProp,
Expand Down Expand Up @@ -94,7 +93,6 @@ export class ArrayType<IT extends IAnyType> extends ComplexType<
hookInitializers: Array<IHooksGetter<IMSTArray<IT>>> = []
) {
super(name)
makeObservable(this)
this.hookInitializers = hookInitializers
}

Expand Down Expand Up @@ -279,7 +277,6 @@ export class ArrayType<IT extends IAnyType> extends ComplexType<
}
}

@action
applySnapshot(node: this["N"], snapshot: this["C"]): void {
typecheckInternal(this, snapshot)
const target = node.storedValue
Expand Down Expand Up @@ -310,6 +307,7 @@ export class ArrayType<IT extends IAnyType> extends ComplexType<
node.storedValue.splice(Number(subpath), 1)
}
}
ArrayType.prototype.applySnapshot = action(ArrayType.prototype.applySnapshot)

/**
* `types.array` - Creates an index based collection type who's children are all of a uniform declared type.
Expand Down
6 changes: 2 additions & 4 deletions packages/mobx-state-tree/src/types/complex-types/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import {
observable,
ObservableMap,
observe,
values,
makeObservable
values
} from "mobx"
import {
ComplexType,
Expand Down Expand Up @@ -236,7 +235,6 @@ export class MapType<IT extends IAnyType> extends ComplexType<
super(name)
this._determineIdentifierMode()
this.hookInitializers = hookInitializers
makeObservable(this)
}

hooks(hooks: IHooksGetter<IMSTMap<IT>>) {
Expand Down Expand Up @@ -444,7 +442,6 @@ export class MapType<IT extends IAnyType> extends ComplexType<
}
}

@action
applySnapshot(node: this["N"], snapshot: this["C"]): void {
typecheckInternal(this, snapshot)
const target = node.storedValue
Expand Down Expand Up @@ -488,6 +485,7 @@ export class MapType<IT extends IAnyType> extends ComplexType<
node.storedValue.delete(subpath)
}
}
MapType.prototype.applySnapshot = action(MapType.prototype.applySnapshot)

/**
* `types.map` - Creates a key based collection type who's children are all of a uniform declared type.
Expand Down
6 changes: 2 additions & 4 deletions packages/mobx-state-tree/src/types/complex-types/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
observe,
set,
IObjectDidChange,
makeObservable,
extendObservable
makeObservable
} from "mobx"
import {
addHiddenFinalProp,
Expand Down Expand Up @@ -345,7 +344,6 @@ export class ModelType<

constructor(opts: ModelTypeConfig) {
super(opts.name || defaultObjectOptions.name)
makeObservable(this)
Object.assign(this, defaultObjectOptions, opts)
// ensures that any default value gets converted to its related type
this.properties = toPropertiesObject(this.properties) as PROPS
Expand Down Expand Up @@ -673,7 +671,6 @@ export class ModelType<
;(node.storedValue as any)[subpath] = patch.value
}

@action
applySnapshot(node: this["N"], snapshot: this["C"]): void {
const preProcessedSnapshot = this.applySnapshotPreProcessor(snapshot)
typecheckInternal(this, preProcessedSnapshot)
Expand Down Expand Up @@ -739,6 +736,7 @@ export class ModelType<
;(node.storedValue as any)[subpath] = undefined
}
}
ModelType.prototype.applySnapshot = action(ModelType.prototype.applySnapshot)

export function model<P extends ModelPropertiesDeclaration = {}>(
name: string,
Expand Down
9 changes: 0 additions & 9 deletions packages/mobx-state-tree/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,15 +349,6 @@ export function argsToArray(args: IArguments): any[] {
return res
}

/**
* @internal
* @hidden
*/
export function invalidateComputed(target: any, propName: string) {
const atom = getAtom(target, propName) as any
atom.trackAndCompute()
}

/**
* @internal
* @hidden
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6852,6 +6852,11 @@ mobx@^6.0.4:
resolved "https://registry.yarnpkg.com/mobx/-/mobx-6.0.4.tgz#8fc3e3629a3346f8afddf5bd954411974744dad1"
integrity sha512-wT2QJT9tW19VSHo9x7RPKU3z/I2Ps6wUS8Kb1OO+kzmg7UY3n4AkcaYG6jq95Lp1R9ohjC/NGYuT2PtuvBjhFg==

mobx@^6.1.0:
version "6.1.0"
resolved "https://registry.yarnpkg.com/mobx/-/mobx-6.1.0.tgz#4a50d151c7396abc7058db8f4153ef866fb7d6b4"
integrity sha512-AU3z1oIep0wu7BdFFjd/0e0K1SGVcXi0TX8GPKKCkdnQEjJ3w/PgOct63Yk7V0/di/bnxb8+UehWC/Lld+MmeA==

modify-values@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/modify-values/-/modify-values-1.0.1.tgz#b3939fa605546474e3e3e3c63d64bd43b4ee6022"
Expand Down

0 comments on commit a212858

Please sign in to comment.