Skip to content

Commit

Permalink
Implemented #464 isStrictModeEnabled
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Aug 30, 2016
1 parent bfeeeb7 commit 00c780b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.5.0

* Introduced `isStrictModeEnabled()`, deprecated `useStrict()` without arguments, see #464
* Fixed #505, accessing an observable property throws before it is initialized

# 2.4.4

* Fixed #503: map.delete returns boolean
Expand Down
11 changes: 8 additions & 3 deletions src/core/action.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {transactionStart, transactionEnd} from "../core/transaction";
import {invariant} from "../utils/utils";
import {invariant, deprecated} from "../utils/utils";
import {untrackedStart, untrackedEnd} from "../core/derivation";
import {isSpyEnabled, spyReportStart, spyReportEnd} from "../core/spy";
import {ComputedValue} from "../core/computedvalue";
Expand Down Expand Up @@ -55,15 +55,20 @@ export function executeAction(actionName: string, fn: Function, scope: any, args
export function useStrict(): boolean;
export function useStrict(strict: boolean);
export function useStrict(strict?: boolean): any {
if (arguments.length === 0)
if (arguments.length === 0) {
deprecated("`useStrict` without arguments is deprecated, use `isStrictModeEnabled()` instead");
return globalState.strictMode;
else {
} else {
invariant(globalState.derivationStack.length === 0, "It is not allowed to set `useStrict` when a derivation is running");
globalState.strictMode = strict;
globalState.allowStateChanges = !strict;
}
}

export function isStrictModeEnabled(): boolean {
return globalState.strictMode;
}

export function allowStateChanges<T>(allowStateChanges: boolean, func: () => T): T {
const prev = allowStateChangesStart(allowStateChanges);
const res = func();
Expand Down
2 changes: 1 addition & 1 deletion src/mobx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export { IAtom, Atom, BaseAtom } from "./core/ato
export { IObservable, IDepTreeNode } from "./core/observable";
export { Reaction, IReactionPublic } from "./core/reaction";
export { IDerivation, untracked } from "./core/derivation";
export { useStrict } from "./core/action";
export { useStrict, isStrictModeEnabled } from "./core/action";
export { spy } from "./core/spy";
export { transaction } from "./core/transaction";
export { IComputedValue } from "./core/computedvalue";
Expand Down
1 change: 1 addition & 0 deletions test/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ test('correct api should be exposed', function(t) {
'isObservableArray',
'isObservableMap',
'isObservableObject',
'isStrictModeEnabled',
'map',
'observable',
'observe',
Expand Down
17 changes: 10 additions & 7 deletions test/strict-mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ var strictError = /It is not allowed to create or change state outside an `actio

test('strict mode should not allow changes outside action', t => {
var a = mobx.observable(2);
t.equal(mobx.isStrictModeEnabled(), false)
mobx.useStrict(true);
t.equal(mobx.isStrictModeEnabled(), true)
t.throws(() => a.set(3), strictError);
mobx.useStrict(false);
t.equal(mobx.isStrictModeEnabled(), false)
a.set(4);
t.equal(a.get(), 4);
t.end();
Expand Down Expand Up @@ -38,14 +41,14 @@ test('reactions cannot modify state in strict mode', t => {
b.set(3)
}, strictError);
});

d = mobx.autorun(() => {
if (a.get() > 5)
b.set(7);
});

mobx.action(() => a.set(4))(); // ok

t.throws(() => a.set(5), strictError);

mobx.useStrict(false);
Expand All @@ -59,7 +62,7 @@ test('action inside reaction in strict mode can modify state', t => {

mobx.useStrict(true);
var act = mobx.action(() => b.set(b.get() + 1));

var d = mobx.autorun(() => {
if (a.get() % 2 === 0)
act();
Expand All @@ -74,9 +77,9 @@ test('action inside reaction in strict mode can modify state', t => {
t.equal(b.get(), 3);
setA(5);
t.equal(b.get(), 3);
setA(16);
setA(16);
t.equal(b.get(), 4, "b should not be 55");

mobx.useStrict(false);
t.end();
});
Expand All @@ -103,7 +106,7 @@ test('cannot create or modify objects in strict mode without action', t => {
t.throws(() => map.delete("a"), strictError);

mobx.useStrict(false);

// can modify again
obj.a = 42;

Expand Down

0 comments on commit 00c780b

Please sign in to comment.