Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commited5a2f6

Browse files
authored
Improve the error message for an action getting thrown inside a mutator (#139)
* The error thrown when an action called inside a mutator should give more information
1 parent133581d commited5a2f6

File tree

6 files changed

+17
-15
lines changed

6 files changed

+17
-15
lines changed

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"satcheljs",
3-
"version":"4.1.0",
3+
"version":"4.2.0",
44
"description":"Store implementation for functional reactive flux.",
55
"lint-staged": {
66
"*.{ts,tsx}": [

‎src/dispatcher.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ export function subscribe(actionId: string, callback: Subscriber<any>) {
1414
}
1515

1616
exportfunctiondispatch(actionMessage:ActionMessage){
17-
if(getGlobalContext().inMutator){
18-
thrownewError('Mutators cannot dispatch further actions.');
17+
constcurrentMutator=getGlobalContext().currentMutator;
18+
if(currentMutator){
19+
thrownewError(
20+
`Mutator (${currentMutator}) may not dispatch action (${actionMessage.type})`
21+
);
1922
}
2023

2124
letdispatchWithMiddleware=getGlobalContext().dispatchWithMiddleware||finalDispatch;

‎src/globalContext.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface GlobalContext {
1414
nextActionId:number;
1515
subscriptions:{[key:string]:Subscriber<ActionMessage>[]};
1616
dispatchWithMiddleware:DispatchFunction;
17-
inMutator:boolean;
17+
currentMutator:string|null;
1818

1919
// Legacy properties
2020
legacyInDispatch:number;
@@ -39,7 +39,7 @@ export function __resetGlobalContext() {
3939
nextActionId:0,
4040
subscriptions:{},
4141
dispatchWithMiddleware:null,
42-
inMutator:false,
42+
currentMutator:null,
4343
legacyInDispatch:0,
4444
legacyDispatchWithMiddleware:null,
4545
legacyTestMode:false,

‎src/mutator.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ export default function mutator<T extends ActionMessage>(
1919
// Wrap the callback in a MobX action so it can modify the store
2020
letwrappedTarget=action(getPrivateActionType(actionCreator),(actionMessage:T)=>{
2121
try{
22-
getGlobalContext().inMutator=true;
22+
getGlobalContext().currentMutator=actionCreator.name;
2323
if(target(actionMessage)asany){
2424
thrownewError('Mutators cannot return a value and cannot be async.');
2525
}
2626
}finally{
27-
getGlobalContext().inMutator=false;
27+
getGlobalContext().currentMutator=null;
2828
}
2929
});
3030

‎test/dispatcherTests.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ describe('dispatcher', () => {
1010
mockGlobalContext={
1111
subscriptions:{},
1212
dispatchWithMiddleware:jasmine.createSpy('dispatchWithMiddleware'),
13-
inMutator:false,
13+
currentMutator:null,
1414
};
1515

1616
spyOn(globalContext,'getGlobalContext').and.returnValue(mockGlobalContext);
@@ -73,7 +73,7 @@ describe('dispatcher', () => {
7373

7474
it('dispatch throws if called within a mutator',()=>{
7575
// Arrange
76-
mockGlobalContext.inMutator=true;
76+
mockGlobalContext.currentMutator='SomeAction';
7777

7878
// Act / Assert
7979
expect(()=>{

‎test/mutatorTests.ts‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ describe('mutator', () => {
88
letmockGlobalContext:any;
99

1010
beforeEach(()=>{
11-
mockGlobalContext={inMutator:false};
11+
mockGlobalContext={currentMutator:null};
1212
spyOn(globalContext,'getGlobalContext').and.returnValue(mockGlobalContext);
1313
spyOn(dispatcher,'subscribe');
1414
});
@@ -74,12 +74,11 @@ describe('mutator', () => {
7474
expect(subscribedCallback).toThrow();
7575
});
7676

77-
it('sets theinMutator flagtotrue for the duration of the mutator callback',()=>{
77+
it('sets thecurrentMutatortoactionMessage type for the duration of the mutator callback',()=>{
7878
// Arrange
79-
letactionCreator:any={__SATCHELJS_ACTION_ID:'testAction'};
80-
letinMutatorValue;
79+
letactionCreator:any={__SATCHELJS_ACTION_ID:'testAction',name:'testName'};
8180
letcallback=()=>{
82-
expect(mockGlobalContext.inMutator).toBeTruthy();
81+
expect(mockGlobalContext.currentMutator).toBe('testName');
8382
};
8483
mutator(actionCreator,callback);
8584

@@ -88,6 +87,6 @@ describe('mutator', () => {
8887
subscribedCallback();
8988

9089
// Assert
91-
expect(mockGlobalContext.inMutator).toBeFalsy();
90+
expect(mockGlobalContext.currentMutator).toBe(null);
9291
});
9392
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp