Skip to content

Commit 1042620

Browse files
committed
Fix reactor isDispatching getting stuck if noop action
1 parent d09c9e7 commit 1042620

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

src/reactor.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,15 @@ class Reactor {
128128

129129
if (this.__batchDepth > 0) {
130130
this.__batchDispatchCount++
131-
} else if (this.state !== prevState) {
132-
try {
133-
this.__notify()
134-
} catch (e) {
135-
this.__isDispatching = false
136-
throw e
131+
} else {
132+
if (this.state !== prevState) {
133+
try {
134+
this.__notify()
135+
} catch (e) {
136+
this.__isDispatching = false
137+
throw e
138+
}
137139
}
138-
139140
this.__isDispatching = false
140141
}
141142
}

tests/reactor-tests.js

+15
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ describe('Reactor', () => {
209209

210210
expect(() => reactor.dispatch('setTax', 5)).not.toThrow()
211211
})
212+
213+
it('should allow subsequent dispatches if a dispatched action doesnt cause state change', () => {
214+
reactor.dispatch('noop')
215+
216+
expect(() => reactor.dispatch('setTax', 5)).not.toThrow()
217+
})
218+
212219
it('should allow subsequent dispatches if an observer throws an error', () => {
213220
var unWatchFn = reactor.observe([], state => {
214221
throw new Error('observer error')
@@ -1109,6 +1116,14 @@ describe('Reactor', () => {
11091116
}).not.toThrow()
11101117
})
11111118

1119+
it('should allow subsequent dispatches if batched action doesnt cause state change', () => {
1120+
reactor.batch(() => {
1121+
reactor.dispatch('noop')
1122+
})
1123+
1124+
expect(() => reactor.dispatch('add', 'one')).not.toThrow()
1125+
})
1126+
11121127
it('should allow subsequent dispatches if an error is raised in an observer', () => {
11131128
var unWatchFn = reactor.observe([], state => {
11141129
throw new Error('observe error')

0 commit comments

Comments
 (0)