Skip to content

Commit

Permalink
Replaces useStrict with configure
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Feb 27, 2018
1 parent 65a0b98 commit 856aa26
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 41 deletions.
8 changes: 8 additions & 0 deletions src/api/configure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { globalState } from "../core/globalstate"

export function configure(options: { enforceActions?: boolean }): void {
if (options.enforceActions !== undefined) {
globalState.enforceActions = !!options.enforceActions
globalState.allowStateChanges = !options.enforceActions
}
}
5 changes: 0 additions & 5 deletions src/core/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,6 @@ function endAction(runInfo: IActionRunInfo) {
if (runInfo.notifySpy) spyReportEnd({ time: Date.now() - runInfo.startTime })
}

export function useStrict(strict: boolean): void {
globalState.strictMode = strict
globalState.allowStateChanges = !strict
}

export function allowStateChanges<T>(allowStateChanges: boolean, func: () => T): T {
const prev = allowStateChangesStart(allowStateChanges)
let res: T
Expand Down
2 changes: 1 addition & 1 deletion src/core/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export function checkIfStateModificationsAreAllowed(atom: IAtom) {
if (!globalState.allowStateChanges && hasObservers)
fail(
process.env.NODE_ENV !== "production" &&
(globalState.strictMode
(globalState.enforceActions
? "Since strict-mode is enabled, changing observed observable values outside actions is not allowed. Please wrap the code in an `action` if this change is intended. Tried to modify: "
: "Side effects like changing state are not allowed at this point. Are you trying to modify state from, for example, the render function of a React component? Tried to modify: ") +
atom.name
Expand Down
6 changes: 3 additions & 3 deletions src/core/globalstate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { IObservable } from "./observable"
/**
* These values will persist if global state is reset
*/
const persistentKeys = ["mobxGuid", "spyListeners", "strictMode", "runId"]
const persistentKeys = ["mobxGuid", "spyListeners", "enforceActions", "runId"]

export class MobXGlobals {
/**
Expand Down Expand Up @@ -68,7 +68,7 @@ export class MobXGlobals {
/**
* If strict mode is enabled, state changes are by default not allowed
*/
strictMode = false
enforceActions = false

/**
* Spy callbacks
Expand Down Expand Up @@ -119,5 +119,5 @@ export function resetGlobalState() {
const defaultGlobals = new MobXGlobals()
for (let key in defaultGlobals)
if (persistentKeys.indexOf(key) === -1) globalState[key] = defaultGlobals[key]
globalState.allowStateChanges = !globalState.strictMode
globalState.allowStateChanges = !globalState.enforceActions
}
3 changes: 2 additions & 1 deletion src/mobx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export { Reaction, IReactionPublic, IReactionDisposer } from "./core/reaction"
export { IDerivation, untracked, IDerivationState } from "./core/derivation"
export { IAtom, createAtom } from "./core/atom"

export { useStrict, IAction } from "./core/action"
export { IAction } from "./core/action"
export { spy } from "./core/spy"
export { IComputedValue } from "./core/computedvalue"

Expand Down Expand Up @@ -77,6 +77,7 @@ export { autorun, when, reaction, IReactionOptions } from "./api/autorun"
export { action, isAction, runInAction, IActionFactory } from "./api/action"
export { keys, values, set, remove } from "./api/object-api"
export { decorate } from "./api/decorate"
export { configure } from "./api/configure"
export { onBecomeObserved, onBecomeUnobserved } from "./api/become-observed"

export { toJS } from "./api/tojs"
Expand Down
4 changes: 2 additions & 2 deletions test/base/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ test("#286 exceptions in actions should not affect global state", () => {
})

test("runInAction", () => {
mobx.useStrict(true)
mobx.configure({ enforceActions: true })
var values = []
var events = []
var spyDisposer = mobx.spy(ev => {
Expand Down Expand Up @@ -309,7 +309,7 @@ test("runInAction", () => {
{ arguments: [], name: "<unnamed action>" }
])

mobx.useStrict(false)
mobx.configure({ enforceActions: false })
spyDisposer()

d()
Expand Down
2 changes: 1 addition & 1 deletion test/base/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ test("correct api should be exposed", function() {
"autorun",
"comparer",
"computed",
"configure",
"createAtom",
"decorate",
"deepEqual",
Expand Down Expand Up @@ -54,7 +55,6 @@ test("correct api should be exposed", function() {
"trace",
"transaction",
"untracked",
"useStrict",
"values",
"when"
].sort()
Expand Down
6 changes: 3 additions & 3 deletions test/base/babel-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
isComputedProp,
spy,
isAction,
useStrict
configure
} from "../../src/mobx.ts"
import * as mobx from "../../src/mobx.ts"

Expand Down Expand Up @@ -388,13 +388,13 @@ test("custom action decorator on field (babel)", function() {
})

test("267 (babel) should be possible to declare properties observable outside strict mode", () => {
useStrict(true)
configure({ enforceActions: true })

class Store {
@observable timer
}

useStrict(false)
configure({ enforceActions: false })
})

test("288 atom not detected for object property", () => {
Expand Down
1 change: 0 additions & 1 deletion test/base/decorate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
isComputedProp,
spy,
isAction,
useStrict,
decorate
} from "../../src/mobx"

Expand Down
4 changes: 2 additions & 2 deletions test/base/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,7 @@ test("work with 'toString' key", () => {
})

test("issue 940, should not be possible to change maps outside strict mode", () => {
mobx.useStrict(true)
mobx.configure({ enforceActions: true })

try {
const m = mobx.observable.map()
Expand All @@ -559,7 +559,7 @@ test("issue 940, should not be possible to change maps outside strict mode", ()

d()
} finally {
mobx.useStrict(false)
mobx.configure({ enforceActions: false })
}
})

Expand Down
28 changes: 14 additions & 14 deletions test/base/strict-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var strictError = /Since strict-mode is enabled, changing observed observable va

test("strict mode should not allow changes outside action", () => {
var a = mobx.observable.box(2)
mobx.useStrict(true)
mobx.configure({ enforceActions: true })

// allowed, a is not observed
a.set(3)
Expand All @@ -14,7 +14,7 @@ test("strict mode should not allow changes outside action", () => {
expect(() => a.set(3)).toThrowError(strictError)
d()

mobx.useStrict(false)
mobx.configure({ enforceActions: false })
a.set(4)
expect(a.get()).toBe(4)
})
Expand All @@ -23,32 +23,32 @@ test("actions can modify observed state in strict mode", () => {
var a = mobx.observable.box(2)
var d = mobx.autorun(() => a.get())

mobx.useStrict(true)
mobx.configure({ enforceActions: true })
mobx.action(() => {
a.set(3)
var b = mobx.observable.box(4)
})()

mobx.useStrict(false)
mobx.configure({ enforceActions: false })
d()
})

test("actions can modify non-observed state in strict mode", () => {
var a = mobx.observable.box(2)

mobx.useStrict(true)
mobx.configure({ enforceActions: true })
mobx.action(() => {
a.set(3)
var b = mobx.observable.box(4)
})()

mobx.useStrict(false)
mobx.configure({ enforceActions: false })
})

test("reactions cannot modify state in strict mode", () => {
var a = mobx.observable.box(3)
var b = mobx.observable.box(4)
mobx.useStrict(true)
mobx.configure({ enforceActions: true })
mobx._resetGlobalState() // should preserve strict mode

var bd = mobx.autorun(() => {
Expand All @@ -70,7 +70,7 @@ test("reactions cannot modify state in strict mode", () => {

expect(() => a.set(5)).toThrowError(strictError)

mobx.useStrict(false)
mobx.configure({ enforceActions: false })
d()
bd()
})
Expand All @@ -83,7 +83,7 @@ test("action inside reaction in strict mode can modify state", () => {
b.get() // make sure it is observed
})

mobx.useStrict(true)
mobx.configure({ enforceActions: true })
var act = mobx.action(() => b.set(b.get() + 1))

var d = mobx.autorun(() => {
Expand All @@ -102,7 +102,7 @@ test("action inside reaction in strict mode can modify state", () => {
setA(16)
expect(b.get()).toBe(4)

mobx.useStrict(false)
mobx.configure({ enforceActions: false })
bd()
d()
})
Expand All @@ -112,7 +112,7 @@ test("cannot create or modify objects in strict mode without action", () => {
var ar = mobx.observable([1])
var map = mobx.observable.map({ a: 2 })

mobx.useStrict(true)
mobx.configure({ enforceActions: true })

// introducing new observables is ok!
// mobx.observable({ a: 2, b: function() { return this.a }});
Expand All @@ -128,7 +128,7 @@ test("cannot create or modify objects in strict mode without action", () => {
// t.throws(() => map.set("b", 4), strictError);
// t.throws(() => map.delete("a"), strictError);

mobx.useStrict(false)
mobx.configure({ enforceActions: false })

// can modify again
obj.a = 42
Expand All @@ -139,7 +139,7 @@ test("can create objects in strict mode with action", () => {
var ar = mobx.observable([1])
var map = mobx.observable.map({ a: 2 })

mobx.useStrict(true)
mobx.configure({ enforceActions: true })

mobx.action(() => {
mobx.observable({
Expand All @@ -160,7 +160,7 @@ test("can create objects in strict mode with action", () => {
map.delete("a")
})()

mobx.useStrict(false)
mobx.configure({ enforceActions: false })
})

test("strict mode checks", function() {
Expand Down
15 changes: 7 additions & 8 deletions test/base/typescript-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
isObservable,
isObservableProp,
isObservableObject,
Atom,
transaction,
IObjectChange,
spy,
useStrict,
configure,
isAction,
decorate
decorate,
IAtom
} from "../../src/mobx"
import * as mobx from "../../src/mobx"

Expand Down Expand Up @@ -153,7 +153,7 @@ test("scope", () => {
y: number
}

const Thing = function() {
const Thing = function(this: any) {
extendObservable(this, {
y: 3,
// this will work here
Expand Down Expand Up @@ -276,7 +276,7 @@ test("atom clock example", done => {
const time_factor = 50 // speed up / slow down tests

class Clock {
atom: Atom
atom: IAtom
intervalHandler: number | null = null
currentDateTime: string | undefined = undefined

Expand Down Expand Up @@ -626,13 +626,13 @@ test("custom action decorator on field (typescript)", () => {
})

test("267 (typescript) should be possible to declare properties observable outside strict mode", () => {
useStrict(true)
configure({ enforceActions: true })

class Store {
@observable timer: number | null = null
}

useStrict(false)
configure({ enforceActions: false })
})

test("288 atom not detected for object property", () => {
Expand Down Expand Up @@ -1221,7 +1221,6 @@ test("computed comparer works with extendObservable (TS)", () => {

public hour: number
public minute: number
public time: { hour: number; minute: number }
}
const time = new Time(9, 0)

Expand Down

0 comments on commit 856aa26

Please sign in to comment.