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

Added warning when using autorun + autorunAsync with an action (#576) #582

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@
"state management",
"data flow"
]
}
}
13 changes: 10 additions & 3 deletions src/api/autorun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -231,4 +238,4 @@ export function reaction<T>(arg1: any, arg2: any, arg3: any, arg4?: any, arg5?:

r.schedule();
return r.getDisposer();
}
}
7 changes: 7 additions & 0 deletions test/autorun.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
7 changes: 7 additions & 0 deletions test/autorunAsync.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});