Skip to content

Commit b20d22e

Browse files
fix(overmind): fix merging getters
1 parent 8b3807a commit b20d22e

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

packages/node_modules/overmind/src/config/config.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { merge, namespaced, lazy } from './'
21
import { Overmind } from '../'
2+
import { lazy, merge, namespaced } from './'
33

44
describe('Config', () => {
55
test('should merge configs', () => {

packages/node_modules/overmind/src/config/merge.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
1+
import isPlainObject from 'is-plain-obj'
2+
13
import { IConfiguration } from '../'
24

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+
331
export function merge<A extends IConfiguration, B extends IConfiguration>(
432
configA: A,
533
configB: B
@@ -94,6 +122,9 @@ export function merge<
94122
configH: H,
95123
configI: I
96124
): A & B & C & D & E & F & G & H & I
125+
126+
127+
97128
export function merge(...configurations: IConfiguration[]): IConfiguration {
98129
const initializers = configurations.reduce(
99130
(aggr, config) =>
@@ -135,10 +166,7 @@ export function merge(...configurations: IConfiguration[]): IConfiguration {
135166
}
136167
return {
137168
onInitialize: aggr.onInitialize,
138-
state: {
139-
...aggr.state,
140-
...config.state,
141-
},
169+
state: copy(aggr.state, config.state || {}),
142170
effects: {
143171
...aggr.effects,
144172
...config.effects,

0 commit comments

Comments
 (0)