Skip to content

Commit

Permalink
Merge pull request #185 from rematch/fix/loading-throw
Browse files Browse the repository at this point in the history
fix issue with loading effect on throw
  • Loading branch information
ShMcK committed Dec 16, 2017
2 parents 9333374 + fe5e711 commit 7a4adc4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 26 deletions.
2 changes: 1 addition & 1 deletion plugins/loading/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion plugins/loading/package.json
@@ -1,6 +1,6 @@
{
"name": "@rematch/loading",
"version": "0.3.0",
"version": "0.3.1",
"description": "Loading indicator plugin for Rematch",
"keywords": [
"@rematch",
Expand Down
9 changes: 5 additions & 4 deletions plugins/loading/src/index.ts
Expand Up @@ -19,7 +19,7 @@ interface LoadingConfig {
blacklist?: string[],
}

const loadingPlugin = (config: LoadingConfig = {}): PluginCreator => {
const loadingPlugin = (config: LoadingConfig = {}): any => {
// validate config
if (config.name && typeof config.name !== 'string') {
throw new Error('loading plugin config name must be a string')
Expand Down Expand Up @@ -78,11 +78,12 @@ const loadingPlugin = (config: LoadingConfig = {}): PluginCreator => {
loading.state.effects[name][action] = false
const fn = dispatch[name][action]
// create function with pre & post loading calls
const dispatchWithHooks = async (props) => {
const dispatchWithHooks = (props) => {
dispatch.loading.show({ name, action })
await fn(props)
return fn(props)
.catch(() => dispatch.loading.hide({ name, action }))
.then(() => dispatch.loading.hide({ name, action }))
// waits for dispatch function to finish before calling "hide"
dispatch.loading.hide({ name, action })
}
// replace existing effect with new dispatch
dispatch[name][action] = dispatchWithHooks
Expand Down
40 changes: 20 additions & 20 deletions plugins/loading/test/loading.test.js
Expand Up @@ -189,24 +189,24 @@ describe('loading', () => {
expect(createStore).toThrow()
})

// test('should handle "hide" if effect throws', () => {
// const {
// init, dispatch
// } = require('../../../src')
// const loadingPlugin = require('../src').default
// const count = {
// state: 0,
// effects: {
// throwError() {
// throw new Error('effect error')
// }
// }
// }
// const store = init({
// models: { count },
// plugins: [loadingPlugin()]
// })
// dispatch.count.throwError()
// expect(store.getState().loading.global).toBe(false)
// })
test('should handle "hide" if effect throws', async () => {
const {
init, dispatch
} = require('../../../src')
const loadingPlugin = require('../src').default
const count = {
state: 0,
effects: {
throwError() {
throw new Error('effect error')
}
}
}
const store = init({
models: { count },
plugins: [loadingPlugin()]
})
await dispatch.count.throwError()
expect(store.getState().loading.global).toBe(false)
})
})

0 comments on commit 7a4adc4

Please sign in to comment.