Skip to content

Commit d09c9e7

Browse files
committed
Dont trap reactor is isDispatching if notify() throws error
1 parent bdfeecc commit d09c9e7

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/reactor.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,17 @@ class Reactor {
125125
throw e
126126
}
127127

128+
128129
if (this.__batchDepth > 0) {
129130
this.__batchDispatchCount++
130131
} else if (this.state !== prevState) {
131-
this.__notify()
132+
try {
133+
this.__notify()
134+
} catch (e) {
135+
this.__isDispatching = false
136+
throw e
137+
}
138+
132139
this.__isDispatching = false
133140
}
134141
}
@@ -305,7 +312,12 @@ class Reactor {
305312
if (this.__batchDispatchCount > 0) {
306313
// set to true to catch if dispatch called from observer
307314
this.__isDispatching = true
308-
this.__notify()
315+
try {
316+
this.__notify()
317+
} catch (e) {
318+
this.__isDispatching = false
319+
throw e
320+
}
309321
this.__isDispatching = false
310322
}
311323
this.__batchDispatchCount = 0

tests/reactor-tests.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,21 @@ describe('Reactor', () => {
209209

210210
expect(() => reactor.dispatch('setTax', 5)).not.toThrow()
211211
})
212+
it('should allow subsequent dispatches if an observer throws an error', () => {
213+
var unWatchFn = reactor.observe([], state => {
214+
throw new Error('observer error')
215+
})
216+
217+
try {
218+
checkoutActions.setTaxPercent(1)
219+
} catch (e) {} // eslint-disable-line
220+
221+
unWatchFn()
222+
223+
expect(() => {
224+
checkoutActions.setTaxPercent(2)
225+
}).not.toThrow()
226+
})
212227
}) // when dispatching a relevant action
213228

214229
describe('#observe', () => {
@@ -987,6 +1002,9 @@ describe('Reactor', () => {
9871002
},
9881003
initialize() {
9891004
this.on('add', (state, item) => state.push(toImmutable(item)))
1005+
this.on('error', (state, payload) => {
1006+
throw new Error('store error');
1007+
})
9901008
},
9911009
}),
9921010
})
@@ -1078,6 +1096,19 @@ describe('Reactor', () => {
10781096
new Error('Dispatch may not be called while a dispatch is in progress'))
10791097
})
10801098

1099+
it('should allow subsequent dispatches if an error is raised by a store handler', () => {
1100+
expect(() => {
1101+
reactor.batch(() => {
1102+
reactor.dispatch('add', 'one')
1103+
reactor.dispatch('error')
1104+
})
1105+
}).toThrow(new Error('store error'))
1106+
1107+
expect(() => {
1108+
reactor.dispatch('add', 'three')
1109+
}).not.toThrow()
1110+
})
1111+
10811112
it('should allow subsequent dispatches if an error is raised in an observer', () => {
10821113
var unWatchFn = reactor.observe([], state => {
10831114
throw new Error('observe error')

0 commit comments

Comments
 (0)