зеркало из https://github.com/microsoft/satcheljs.git
Use catch instead of finally for browser compatibility (#161)
This commit is contained in:
Родитель
0c63b419ac
Коммит
be5cb2f0b6
|
@ -1,5 +1,4 @@
|
||||||
import { action } from 'mobx';
|
import { action } from 'mobx';
|
||||||
|
|
||||||
import ActionCreator from './interfaces/ActionCreator';
|
import ActionCreator from './interfaces/ActionCreator';
|
||||||
import ActionMessage from './interfaces/ActionMessage';
|
import ActionMessage from './interfaces/ActionMessage';
|
||||||
import MutatorFunction from './interfaces/MutatorFunction';
|
import MutatorFunction from './interfaces/MutatorFunction';
|
||||||
|
@ -19,11 +18,14 @@ export default function mutator<TAction extends ActionMessage, TReturn>(
|
||||||
// Wrap the callback in a MobX action so it can modify the store
|
// Wrap the callback in a MobX action so it can modify the store
|
||||||
const actionType = getPrivateActionType(actionCreator);
|
const actionType = getPrivateActionType(actionCreator);
|
||||||
let wrappedTarget = action(actionType, (actionMessage: TAction) => {
|
let wrappedTarget = action(actionType, (actionMessage: TAction) => {
|
||||||
|
const globalContext = getGlobalContext();
|
||||||
try {
|
try {
|
||||||
getGlobalContext().currentMutator = actionType;
|
globalContext.currentMutator = actionType;
|
||||||
target(actionMessage);
|
target(actionMessage);
|
||||||
} finally {
|
globalContext.currentMutator = null;
|
||||||
getGlobalContext().currentMutator = null;
|
} catch (e) {
|
||||||
|
globalContext.currentMutator = null;
|
||||||
|
throw e;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -80,4 +80,27 @@ describe('mutator', () => {
|
||||||
// Assert
|
// Assert
|
||||||
expect(mockGlobalContext.currentMutator).toBe(null);
|
expect(mockGlobalContext.currentMutator).toBe(null);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('sets the currentMutator back to null if error is thrown', () => {
|
||||||
|
// Arrange
|
||||||
|
let actionCreator: any = {
|
||||||
|
__SATCHELJS_ACTION_ID: 'testAction',
|
||||||
|
__SATCHELJS_ACTION_TYPE: 'testActionType',
|
||||||
|
};
|
||||||
|
let callback: any = () => {
|
||||||
|
throw new Error('Error in Mutator');
|
||||||
|
};
|
||||||
|
mutator(actionCreator, callback);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
let subscribedCallback = (dispatcher.subscribe as jasmine.Spy).calls.argsFor(0)[1];
|
||||||
|
try {
|
||||||
|
subscribedCallback();
|
||||||
|
} catch {
|
||||||
|
// no op
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
expect(mockGlobalContext.currentMutator).toBe(null);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Загрузка…
Ссылка в новой задаче