Skip to content

Commit cd3ff4e

Browse files
feat(overmind): completely rework the typing
BREAKING CHANGE: new typing approach, unified actions with operators
1 parent 4a694e7 commit cd3ff4e

File tree

17 files changed

+2085
-2363
lines changed

17 files changed

+2085
-2363
lines changed

packages/node_modules/overmind/src/Overmind.ts

Lines changed: 912 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import * as proxyStateTree from 'proxy-state-tree'
2+
import { IConfiguration } from './types'
3+
import * as internalTypes from './internalTypes'
4+
import { Overmind } from './Overmind'
5+
import * as utils from './utils'
6+
7+
export interface OvermindMock<Config extends IConfiguration>
8+
extends Overmind<Config> {
9+
onInitialize: () => Promise<proxyStateTree.IMutation[]>
10+
mutations: proxyStateTree.IMutation[]
11+
}
12+
13+
export function createOvermindMock<Config extends IConfiguration>(
14+
config: Config
15+
): OvermindMock<Config>
16+
export function createOvermindMock<Config extends IConfiguration>(
17+
config: Config,
18+
setInitialState: (state: Config['state']) => void
19+
): OvermindMock<Config>
20+
export function createOvermindMock<Config extends IConfiguration>(
21+
config: Config,
22+
mockedEffects: internalTypes.NestedPartial<Config['effects']>
23+
): OvermindMock<Config>
24+
export function createOvermindMock<Config extends IConfiguration>(
25+
config: Config,
26+
mockedEffects: internalTypes.NestedPartial<Config['effects']>,
27+
setInitialState: (state: Config['state']) => void
28+
): OvermindMock<Config>
29+
export function createOvermindMock<Config extends IConfiguration>(
30+
...args:
31+
| [Config]
32+
| [Config, (state: Config['state']) => void]
33+
| [Config, internalTypes.NestedPartial<Config['effects']>]
34+
| [
35+
Config,
36+
internalTypes.NestedPartial<Config['effects']>,
37+
(state: Config['state']) => void
38+
]
39+
): OvermindMock<Config> {
40+
const setState = typeof args[1] === 'function' ? args[1] : args[2]
41+
const mockedEffects = typeof args[1] === 'function' ? undefined : args[1]
42+
43+
const state = utils.deepCopy(args[0].state)
44+
45+
if (setState) {
46+
;(setState as any)(state)
47+
}
48+
const mock = new Overmind(
49+
Object.assign({}, args[0], {
50+
state,
51+
}),
52+
{
53+
devtools: false,
54+
},
55+
{
56+
mode: utils.MODE_TEST,
57+
options: {
58+
effectsCallback: (effect) => {
59+
const mockedEffect = (effect.name
60+
? effect.name.split('.')
61+
: []
62+
).reduce((aggr, key) => (aggr ? aggr[key] : aggr), mockedEffects)
63+
64+
if (!mockedEffect || (mockedEffect && !mockedEffect[effect.method])) {
65+
throw new Error(
66+
`The effect "${effect.name}" with method ${
67+
effect.method
68+
} has not been mocked`
69+
)
70+
}
71+
return mockedEffect[effect.method](...effect.args)
72+
},
73+
},
74+
} as internalTypes.TestMode
75+
) as OvermindMock<Config>
76+
77+
const action = (mock as any).createAction(
78+
'onInitialize',
79+
args[0].onInitialize
80+
)
81+
82+
mock.onInitialize = () => action(mock)
83+
mock.mutations = []
84+
85+
return mock as any
86+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { IConfiguration } from './types'
2+
import { Overmind } from './Overmind'
3+
import * as proxyStateTree from 'proxy-state-tree'
4+
import * as utils from './utils'
5+
import * as internalTypes from './internalTypes'
6+
7+
export interface OvermindSSR<Config extends IConfiguration>
8+
extends Overmind<Config> {
9+
hydrate(): proxyStateTree.IMutation[]
10+
}
11+
12+
export function createOvermindSSR<Config extends IConfiguration>(
13+
config: Config
14+
): OvermindSSR<Config> {
15+
const ssr = new Overmind(
16+
config,
17+
{
18+
devtools: false,
19+
},
20+
{
21+
mode: utils.MODE_SSR,
22+
} as internalTypes.SSRMode
23+
) as any
24+
25+
const mutationTree = ssr.proxyStateTreeInstance.getMutationTree()
26+
27+
ssr.state = mutationTree.state
28+
ssr.hydrate = () => {
29+
return mutationTree.flush().mutations
30+
}
31+
return ssr
32+
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function lazy<T extends LazyConfiguration>(
5252
object
5353
> & {
5454
lazy: {
55-
loadConfig: IAction<any, keyof T>
55+
loadConfig: IAction<keyof T, any>
5656
}
5757
}
5858
} {
@@ -72,7 +72,9 @@ export function lazy<T extends LazyConfiguration>(
7272
lazy: {
7373
loadConfig({ state, execution, ...rest }, key) {
7474
const configToLoad = configurations[key]
75-
const namespacePath = execution.namespacePath.slice(0, execution.namespacePath.length - 1).concat(key)
75+
const namespacePath = execution.namespacePath
76+
.slice(0, execution.namespacePath.length - 1)
77+
.concat(key)
7678
return configToLoad().then((loadedConfig) => {
7779
const newConfig = namespaced({
7880
[key]: loadedConfig,
@@ -83,14 +85,17 @@ export function lazy<T extends LazyConfiguration>(
8385
if (newConfig.effects && newConfig.effects[key])
8486
app.effects[key] = newConfig.effects[key]
8587
if (newConfig.actions && newConfig.actions[key])
86-
app.actions[key] = app.getActions(newConfig.actions[key], namespacePath)
88+
app.actions[key] = app.getActions(
89+
newConfig.actions[key],
90+
namespacePath
91+
)
8792
if (newConfig.onInitialize)
8893
newConfig.onInitialize(
8994
{
9095
state,
9196
execution: {
9297
...execution,
93-
namespacePath
98+
namespacePath,
9499
},
95100
...rest,
96101
},

0 commit comments

Comments
 (0)