Skip to content

Commit

Permalink
cherry-picked enforceAction changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Aug 27, 2018
1 parent 7b9cb35 commit 15a3eaf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
33 changes: 27 additions & 6 deletions src/api/configure.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { globalState, isolateGlobalState } from "../core/globalstate"
import { reserveArrayBuffer } from "../types/observablearray"
import { setReactionScheduler } from "../core/reaction"
import { deprecated } from "../utils/utils"

export function configure(options: {
enforceActions?: boolean | "strict"
enforceActions?: boolean | "strict" | "never" | "always" | "observed"
computedRequiresReaction?: boolean
isolateGlobalState?: boolean
disableErrorBoundaries?: boolean
Expand All @@ -18,11 +19,31 @@ export function configure(options: {
reactionScheduler
} = options
if (enforceActions !== undefined) {
if (typeof enforceActions !== "boolean" && enforceActions !== "strict")
return fail(`Invalid configuration for 'enforceActions': ${enforceActions}`)
globalState.enforceActions = enforceActions
globalState.allowStateChanges =
enforceActions === true || enforceActions === "strict" ? false : true
if (typeof enforceActions === "boolean" || enforceActions === "strict")
deprecated(
`Deprecated value for 'enforceActions', use 'false' => '"never"', 'true' => '"observed"', '"strict"' => "'always'" instead`
)
let ea
switch (enforceActions) {
case true:
case "observed":
ea = true
break
case false:
case "never":
ea = false
break
case "strict":
case "always":
ea = "strict"
break
default:
fail(
`Invalid value for 'enforceActions': '${enforceActions}', expected 'never', 'always' or 'observed'`
)
}
globalState.enforceActions = ea
globalState.allowStateChanges = ea === true || ea === "strict" ? false : true
}
if (computedRequiresReaction !== undefined) {
globalState.computedRequiresReaction = !!computedRequiresReaction
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.configure({ enforceActions: true })
mobx.configure({ enforceActions: "observed" })
var values = []
var events = []
var spyDisposer = mobx.spy(ev => {
Expand Down Expand Up @@ -309,7 +309,7 @@ test("runInAction", () => {
{ arguments: [], name: "<unnamed action>" }
])

mobx.configure({ enforceActions: false })
mobx.configure({ enforceActions: "never" })
spyDisposer()

d()
Expand Down
2 changes: 1 addition & 1 deletion test/base/strict-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ test("strict mode checks", function() {

test("enforceActions 'strict' does not allow changing unobserved observables", () => {
try {
mobx.configure({ enforceActions: "strict" })
mobx.configure({ enforceActions: "always" })
const x = mobx.observable({
a: 1,
b: 2
Expand Down

0 comments on commit 15a3eaf

Please sign in to comment.