From 99b31b9f3fdf55548866e71cc515bf1e3a4acb3c Mon Sep 17 00:00:00 2001 From: Jeff Hansen Date: Tue, 27 Sep 2016 09:00:58 +0200 Subject: [PATCH 1/2] Added warning when using autorun + autorunAsync with an action (#576) Tests do **not** run. --- package.json | 2 +- src/api/autorun.ts | 13 ++++++++++--- test/autorun.js | 7 +++++++ test/autorunAsync.js | 7 +++++++ 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 1d2bdf3f4..c02b5f11c 100644 --- a/package.json +++ b/package.json @@ -73,4 +73,4 @@ "state management", "data flow" ] -} \ No newline at end of file +} diff --git a/src/api/autorun.ts b/src/api/autorun.ts index b973a5dff..33daf3d8d 100644 --- a/src/api/autorun.ts +++ b/src/api/autorun.ts @@ -2,7 +2,7 @@ import {Lambda, getNextId, deprecated, invariant, valueDidChange} from "../utils import {assertUnwrapped, ValueMode, getValueModeFromValue} from "../types/modifiers"; import {Reaction, IReactionPublic} from "../core/reaction"; import {untrackedStart, untrackedEnd} from "../core/derivation"; -import {action} from "../api/action"; +import {action, isAction} from "../api/action"; /** * Creates a reactive view and keeps it alive, so that the view is always @@ -37,6 +37,10 @@ export function autorun(arg1: any, arg2: any, arg3?: any) { assertUnwrapped(view, "autorun methods cannot have modifiers"); invariant(typeof view === "function", "autorun expects a function"); + invariant( + isAction(view) === false, + "Warning: attempted to pass an action to autorun. Actions are untracked and will not trigger on state changes. Use `reaction` or wrap only your state modification code in an action." + ); if (scope) view = view.bind(scope); @@ -121,7 +125,10 @@ export function autorunAsync(arg1: any, arg2: any, arg3?: any, arg4?: any) { delay = arg2; scope = arg3; } - + invariant( + isAction(func) === false, + "Warning: attempted to pass an action to autorunAsync. Actions are untracked and will not trigger on state changes. Use `reaction` or wrap only your state modification code in an action." + ); if (delay === void 0) delay = 1; @@ -231,4 +238,4 @@ export function reaction(arg1: any, arg2: any, arg3: any, arg4?: any, arg5?: r.schedule(); return r.getDisposer(); -} \ No newline at end of file +} diff --git a/test/autorun.js b/test/autorun.js index 706d2507b..1fa99251b 100644 --- a/test/autorun.js +++ b/test/autorun.js @@ -37,3 +37,10 @@ test('autorun can be disposed on first run', function(t) { t.end() }); + +test('autorun warns when passed an action', function(t) { + var action = m.action(() => {}); + t.plan(1); + t.throws(() => m.autorun(action), /attempted to pass an action to autorun/); + t.end(); +}); diff --git a/test/autorunAsync.js b/test/autorunAsync.js index c06ea080e..29d09b8ae 100644 --- a/test/autorunAsync.js +++ b/test/autorunAsync.js @@ -125,3 +125,10 @@ test('autorunAsync passes Reaction as an argument to view function', function(t) t.end(); }, 100); }); + +test('autorunAsync warns when passed an action', function(t) { + var action = m.action(() => {}); + t.plan(1); + t.throws(() => m.autorunAsync(action), /attempted to pass an action to autorunAsync/); + t.end(); +}); From 4edf50ed9c6fb49303d4cc193c8357f3c21c4204 Mon Sep 17 00:00:00 2001 From: Michel Weststrate Date: Thu, 29 Sep 2016 09:38:41 +0200 Subject: [PATCH 2/2] Fixed broken unit test, fixes pr #582 --- test/action.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/action.js b/test/action.js index 0f574657d..905defa6c 100644 --- a/test/action.js +++ b/test/action.js @@ -330,7 +330,9 @@ test('action in autorun does not keep / make computed values alive', t => { callComputedTwice() t.equal(calls, 5) - runWithMemoizing(mobx.action(callComputedTwice)) + runWithMemoizing(function() { + mobx.runInAction(callComputedTwice) + }) t.equal(calls, 6) callComputedTwice()