diff --git a/plugins/loading/package-lock.json b/plugins/loading/package-lock.json index 53a63a9ab..3b918da1e 100644 --- a/plugins/loading/package-lock.json +++ b/plugins/loading/package-lock.json @@ -1,6 +1,6 @@ { "name": "@rematch/loading", - "version": "0.3.0", + "version": "0.3.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/plugins/loading/package.json b/plugins/loading/package.json index 8432050b5..c5a6f4fe7 100644 --- a/plugins/loading/package.json +++ b/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", diff --git a/plugins/loading/src/index.ts b/plugins/loading/src/index.ts index f4166d0a9..6b7bee7fc 100644 --- a/plugins/loading/src/index.ts +++ b/plugins/loading/src/index.ts @@ -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') @@ -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 diff --git a/plugins/loading/test/loading.test.js b/plugins/loading/test/loading.test.js index 0f4181142..0c0b1fcb5 100644 --- a/plugins/loading/test/loading.test.js +++ b/plugins/loading/test/loading.test.js @@ -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) + }) })