|
1 | 1 | import { IConfiguration, IAction } from '../' |
| 2 | +import { processState } from '../utils' |
2 | 3 |
|
3 | 4 | type SubType<Base, Condition> = Pick< |
4 | 5 | Base, |
@@ -112,17 +113,35 @@ export function merge(...configurations: IConfiguration[]): IConfiguration { |
112 | 113 |
|
113 | 114 | const reducedConfigurations = configurations.reduce( |
114 | 115 | (aggr, config) => { |
115 | | - const stateDuplicates = aggr.state ? Object.keys(aggr.state).some( key => config.state ? Object.keys(config.state).includes(key) : false) : false; |
116 | | - const actionsDuplicates = aggr.actions ? Object.keys(aggr.actions).some( key => config.actions ? Object.keys(config.actions).includes(key) : false) : false; |
117 | | - const effectsDuplicates = aggr.effects ? Object.keys(aggr.effects).some( key => config.effects ? Object.keys(config.effects).includes(key) : false) : false; |
118 | | - if (stateDuplicates){ |
119 | | - throw new Error("Merge conflict: at least one state definition contains a duplicate key") |
| 116 | + const stateDuplicates = aggr.state |
| 117 | + ? Object.keys(aggr.state).some((key) => |
| 118 | + config.state ? Object.keys(config.state).includes(key) : false |
| 119 | + ) |
| 120 | + : false |
| 121 | + const actionsDuplicates = aggr.actions |
| 122 | + ? Object.keys(aggr.actions).some((key) => |
| 123 | + config.actions ? Object.keys(config.actions).includes(key) : false |
| 124 | + ) |
| 125 | + : false |
| 126 | + const effectsDuplicates = aggr.effects |
| 127 | + ? Object.keys(aggr.effects).some((key) => |
| 128 | + config.effects ? Object.keys(config.effects).includes(key) : false |
| 129 | + ) |
| 130 | + : false |
| 131 | + if (stateDuplicates) { |
| 132 | + throw new Error( |
| 133 | + 'Merge conflict: at least one state definition contains a duplicate key' |
| 134 | + ) |
120 | 135 | } |
121 | 136 | if (actionsDuplicates) { |
122 | | - throw new Error("Merge conflict: at least one actions definition contains a duplicate key") |
| 137 | + throw new Error( |
| 138 | + 'Merge conflict: at least one actions definition contains a duplicate key' |
| 139 | + ) |
123 | 140 | } |
124 | 141 | if (effectsDuplicates) { |
125 | | - throw new Error("Merge conflict: at least one effects definition contains a duplicate key") |
| 142 | + throw new Error( |
| 143 | + 'Merge conflict: at least one effects definition contains a duplicate key' |
| 144 | + ) |
126 | 145 | } |
127 | 146 | return { |
128 | 147 | onInitialize: aggr.onInitialize, |
@@ -290,13 +309,14 @@ export function lazy<T extends LazyConfiguration, B = T>( |
290 | 309 | lazy: { |
291 | 310 | loadConfig({ state, ...rest }, key) { |
292 | 311 | const configToLoad = configurations[key] |
293 | | - configToLoad().then((loadedConfig) => { |
| 312 | + |
| 313 | + return configToLoad().then((loadedConfig) => { |
294 | 314 | const newConfig = namespaced({ |
295 | 315 | [key]: loadedConfig, |
296 | 316 | }) |
297 | 317 |
|
298 | 318 | if (newConfig.state && newConfig.state[key]) |
299 | | - state[key] = newConfig.state[key] |
| 319 | + state[key] = processState(newConfig.state[key]) |
300 | 320 | if (newConfig.effects && newConfig.effects[key]) |
301 | 321 | app.effects[key] = newConfig.effects[key] |
302 | 322 | if (newConfig.actions && newConfig.actions[key]) |
|
0 commit comments