(state: State, action: Action)
| 62 | | {type: 'timeout'}; |
| 63 | |
| 64 | function reducer(state: State, action: Action): State { |
| 65 | switch (action.type) { |
| 66 | case 'register': { |
| 67 | if (state.loaded.get(action.url) !== false) { |
| 68 | let loaded = new Map(state.loaded); |
| 69 | loaded.set(action.url, false); |
| 70 | return { |
| 71 | loadedAll: false, |
| 72 | // If we had previously loaded all items, then reset the timed out state |
| 73 | // since this is the first item of a new batch. |
| 74 | timedOut: state.loadedAll ? false : state.timedOut, |
| 75 | loadStartTime: state.loadedAll ? Date.now() : state.loadStartTime, |
| 76 | loaded |
| 77 | }; |
| 78 | } |
| 79 | return state; |
| 80 | } |
| 81 | case 'unregister': { |
| 82 | if (state.loaded.has(action.url)) { |
| 83 | let loaded = new Map(state.loaded); |
| 84 | loaded.delete(action.url); |
| 85 | return { |
| 86 | loadedAll: isAllLoaded(loaded), |
| 87 | timedOut: state.timedOut, |
| 88 | loadStartTime: state.loadStartTime, |
| 89 | loaded |
| 90 | }; |
| 91 | } |
| 92 | return state; |
| 93 | } |
| 94 | case 'load': { |
| 95 | if (state.loaded.get(action.url) === false) { |
| 96 | let loaded = new Map(state.loaded); |
| 97 | loaded.set(action.url, true); |
| 98 | return { |
| 99 | loadedAll: isAllLoaded(loaded), |
| 100 | timedOut: state.timedOut, |
| 101 | loadStartTime: state.loadStartTime, |
| 102 | loaded |
| 103 | }; |
| 104 | } |
| 105 | return state; |
| 106 | } |
| 107 | case 'timeout': { |
| 108 | if (!state.loadedAll && !state.timedOut) { |
| 109 | return { |
| 110 | ...state, |
| 111 | timedOut: true |
| 112 | }; |
| 113 | } |
| 114 | return state; |
| 115 | } |
| 116 | default: |
| 117 | return state; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | function isAllLoaded(loaded: Map<string, boolean>) { |
nothing calls this directly
no test coverage detected