|
| 1 | +import isPlainObject from 'is-plain-obj' |
| 2 | + |
1 | 3 | import { IConfiguration } from '../' |
2 | 4 |
|
| 5 | +function copy(target, source) { |
| 6 | + return Object.keys(source).reduce((aggr, key) => { |
| 7 | + if (key === '__esModule') { |
| 8 | + return aggr |
| 9 | + } |
| 10 | + |
| 11 | + if (isPlainObject(source[key])) { |
| 12 | + aggr[key] = copy(target[key] || {}, source[key]) |
| 13 | + } else if (Array.isArray(source[key])) { |
| 14 | + aggr[key] = source[key] |
| 15 | + } else { |
| 16 | + const originalDescriptor = Object.getOwnPropertyDescriptor(source, key) |
| 17 | + const isAGetter = originalDescriptor && 'get' in originalDescriptor |
| 18 | + const value = source[key] |
| 19 | + |
| 20 | + if (isAGetter) { |
| 21 | + Object.defineProperty(aggr, key, originalDescriptor as any) |
| 22 | + } else { |
| 23 | + aggr[key] = value |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + return aggr |
| 28 | + }, target) |
| 29 | +} |
| 30 | + |
3 | 31 | export function merge<A extends IConfiguration, B extends IConfiguration>( |
4 | 32 | configA: A, |
5 | 33 | configB: B |
@@ -94,6 +122,9 @@ export function merge< |
94 | 122 | configH: H, |
95 | 123 | configI: I |
96 | 124 | ): A & B & C & D & E & F & G & H & I |
| 125 | + |
| 126 | + |
| 127 | + |
97 | 128 | export function merge(...configurations: IConfiguration[]): IConfiguration { |
98 | 129 | const initializers = configurations.reduce( |
99 | 130 | (aggr, config) => |
@@ -135,10 +166,7 @@ export function merge(...configurations: IConfiguration[]): IConfiguration { |
135 | 166 | } |
136 | 167 | return { |
137 | 168 | onInitialize: aggr.onInitialize, |
138 | | - state: { |
139 | | - ...aggr.state, |
140 | | - ...config.state, |
141 | | - }, |
| 169 | + state: copy(aggr.state, config.state || {}), |
142 | 170 | effects: { |
143 | 171 | ...aggr.effects, |
144 | 172 | ...config.effects, |
|
0 commit comments