| 3 | const NAMESPACE = 'loading'; |
| 4 | |
| 5 | function createLoading(opts = {}) { |
| 6 | const namespace = opts.namespace || NAMESPACE; |
| 7 | |
| 8 | const { only = [], except = [] } = opts; |
| 9 | if (only.length > 0 && except.length > 0) { |
| 10 | throw Error('It is ambiguous to configurate `only` and `except` items at the same time.'); |
| 11 | } |
| 12 | |
| 13 | const initialState = { |
| 14 | global: false, |
| 15 | models: {}, |
| 16 | effects: {}, |
| 17 | }; |
| 18 | |
| 19 | const extraReducers = { |
| 20 | [namespace](state = initialState, { type, payload }) { |
| 21 | const { namespace, actionType } = payload || {}; |
| 22 | let ret; |
| 23 | switch (type) { |
| 24 | case SHOW: |
| 25 | ret = { |
| 26 | ...state, |
| 27 | global: true, |
| 28 | models: { ...state.models, [namespace]: true }, |
| 29 | effects: { ...state.effects, [actionType]: true }, |
| 30 | }; |
| 31 | break; |
| 32 | case HIDE: // eslint-disable-line |
| 33 | const effects = { ...state.effects, [actionType]: false }; |
| 34 | const models = { |
| 35 | ...state.models, |
| 36 | [namespace]: Object.keys(effects).some((actionType) => { |
| 37 | const _namespace = actionType.split('/')[0]; |
| 38 | if (_namespace !== namespace) return false; |
| 39 | return effects[actionType]; |
| 40 | }), |
| 41 | }; |
| 42 | const global = Object.keys(models).some((namespace) => { |
| 43 | return models[namespace]; |
| 44 | }); |
| 45 | ret = { |
| 46 | ...state, |
| 47 | global, |
| 48 | models, |
| 49 | effects, |
| 50 | }; |
| 51 | break; |
| 52 | default: |
| 53 | ret = state; |
| 54 | break; |
| 55 | } |
| 56 | return ret; |
| 57 | }, |
| 58 | }; |
| 59 | |
| 60 | function onEffect(effect, { put }, model, actionType) { |
| 61 | const { namespace } = model; |
| 62 | if ( |