Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add allowStateChangesInsideComputed to allow changes inside a computed value #1706

Merged
merged 11 commits into from
Sep 25, 2018
12 changes: 12 additions & 0 deletions src/core/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,15 @@ export function allowStateChangesStart(allowStateChanges: boolean) {
export function allowStateChangesEnd(prev: boolean) {
globalState.allowStateChanges = prev
}

export function allowStateChangesInsideComputed<T>(func: () => T): T {
const prev = globalState.computationDepth
globalState.computationDepth = 0
let res: T
try {
res = func()
} finally {
globalState.computationDepth += prev
mweststrate marked this conversation as resolved.
Show resolved Hide resolved
}
return res
}
11 changes: 1 addition & 10 deletions src/core/derivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,8 @@ export function isComputingDerivation() {
}

export function checkIfStateModificationsAreAllowed(atom: IAtom) {
const hasObservers = atom.observers.size > 0
// Should never be possible to change an observed observable from inside computed, see #798
if (globalState.computationDepth > 0 && hasObservers)
fail(
process.env.NODE_ENV !== "production" &&
`Computed values are not allowed to cause side effects by changing observables that are already being observed. Tried to modify: ${
atom.name
}`
)
// Should not be possible to change observed state outside strict mode, except during initialization, see #563
if (!globalState.allowStateChanges && (hasObservers || globalState.enforceActions === "strict"))
if (!globalState.allowStateChanges && (atom.observers.size > 0 || globalState.enforceActions === "strict"))
fail(
process.env.NODE_ENV !== "production" &&
(globalState.enforceActions
Expand Down
1 change: 1 addition & 0 deletions src/core/globalstate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export class MobXGlobals {
* To ensure that those functions stay pure.
*/
allowStateChanges = true

/**
* If strict mode is enabled, state changes are by default not allowed
*/
Expand Down
1 change: 1 addition & 0 deletions src/mobx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export {
getAtom,
getAdministration as _getAdministration,
allowStateChanges as _allowStateChanges,
allowStateChangesInsideComputed as _allowStateChangesInsideComputed,
Lambda,
isArrayLike,
$mobx,
Expand Down
53 changes: 53 additions & 0 deletions test/base/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,59 @@ test("should be possible to change unobserved state in an action called from com
mobx._resetGlobalState()
})

test("should be possible to change observed state in an action called from computed if run inside _allowStateChangesInsideComputed", () => {
const a = mobx.observable.box(2)
const d = mobx.autorun(() => {
a.get()
})

const testAction = mobx.action(() => {
mobx._allowStateChangesInsideComputed(() => {
a.set(3)
})
expect(a.get()).toBe(3)
expect(() => {
a.set(4)
}).toThrowError(
/Computed values are not allowed to cause side effects by changing observables that are already being observed/
)
})

const c = mobx.computed(() => {
testAction()
return a.get()
})

c.get()

mobx._resetGlobalState()
d()
})

test("should be possible to change observed state in an action called from computed if run inside _allowStateChangesInsideComputed - 2", () => {
const a = mobx.observable.box(2)
const d = mobx.autorun(() => {
a.get()
})

const testAction = mobx.action(() => {
a.set(3)
})

const c = mobx.computed(() => {
mobx._allowStateChanges(true, () => {
testAction()
})
return a.get()
})

c.get()

mobx._resetGlobalState()
d()
})


test("should not be possible to change observed state in an action called from computed", () => {
var a = mobx.observable.box(2)
var d = mobx.autorun(() => {
Expand Down
1 change: 1 addition & 0 deletions test/base/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ test("correct api should be exposed", function() {
"$mobx", // adminstration symbol
"action",
"_allowStateChanges",
"_allowStateChangesInsideComputed",
"autorun",
"comparer",
"computed",
Expand Down