Skip to content

Commit

Permalink
Fixes a subtle bug where mutation on and off flag isn't in the transa…
Browse files Browse the repository at this point in the history
…ction (#93)

* Fixes a subtle bug where mutation on and off flag isn't in the transaction
  • Loading branch information
kenotron authored Jan 31, 2018
1 parent 065a9b0 commit 8f3cbd7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "satcheljs",
"version": "3.1.0",
"version": "3.1.1",
"description": "Store implementation for functional reactive flux.",
"lint-staged": {
"*.{ts,tsx}": [
Expand Down
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ export { default as applyMiddleware } from './applyMiddleware';
export { default as createStore } from './createStore';
export { dispatch } from './dispatcher';
export { default as mutator } from './mutator';
export { default as orchestrator } from './orchestrator';
import { default as orchestrator } from './orchestrator';
export { default as getRootStore } from './getRootStore';
export { mutatorAction, orchestratorAction } from './simpleSubscribers';
export { useStrict };

// exporting an alias for orchestrator called "flow"
export const flow = orchestrator;
export { orchestrator };

// Default to MobX strict mode
useStrict(true);
10 changes: 5 additions & 5 deletions src/mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ export default function mutator<T extends ActionMessage>(
}

// Wrap the callback in a MobX action so it can modify the store
let wrappedTarget = action(target);

// Subscribe to the action
subscribe(actionId, (actionMessage: T) => {
let wrappedTarget = action((actionMessage: T) => {
try {
getGlobalContext().inMutator = true;
if (wrappedTarget(actionMessage)) {
if (target(actionMessage)) {
throw new Error('Mutators cannot return a value and cannot be async.');
}
} finally {
getGlobalContext().inMutator = false;
}
});

// Subscribe to the action
subscribe(actionId, wrappedTarget);

return target;
}
2 changes: 1 addition & 1 deletion test/mutatorTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('mutator', () => {
mutator(actionCreator, callback);

// Assert
expect(mobx.action).toHaveBeenCalledWith(callback);
expect(mobx.action).toHaveBeenCalled();
});

it('returns the target function', () => {
Expand Down

0 comments on commit 8f3cbd7

Please sign in to comment.