Skip to content

Commit

Permalink
feat(overmind): completely rework the typing
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
new typing approach, unified actions with operators
  • Loading branch information
christianalfoni committed Jan 23, 2021
1 parent 4a694e7 commit cd3ff4e
Show file tree
Hide file tree
Showing 17 changed files with 2,085 additions and 2,363 deletions.
912 changes: 912 additions & 0 deletions packages/node_modules/overmind/src/Overmind.ts

Large diffs are not rendered by default.

86 changes: 86 additions & 0 deletions packages/node_modules/overmind/src/OvermindMock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as proxyStateTree from 'proxy-state-tree'
import { IConfiguration } from './types'
import * as internalTypes from './internalTypes'
import { Overmind } from './Overmind'
import * as utils from './utils'

export interface OvermindMock<Config extends IConfiguration>
extends Overmind<Config> {
onInitialize: () => Promise<proxyStateTree.IMutation[]>
mutations: proxyStateTree.IMutation[]
}

export function createOvermindMock<Config extends IConfiguration>(
config: Config
): OvermindMock<Config>
export function createOvermindMock<Config extends IConfiguration>(
config: Config,
setInitialState: (state: Config['state']) => void
): OvermindMock<Config>
export function createOvermindMock<Config extends IConfiguration>(
config: Config,
mockedEffects: internalTypes.NestedPartial<Config['effects']>
): OvermindMock<Config>
export function createOvermindMock<Config extends IConfiguration>(
config: Config,
mockedEffects: internalTypes.NestedPartial<Config['effects']>,
setInitialState: (state: Config['state']) => void
): OvermindMock<Config>
export function createOvermindMock<Config extends IConfiguration>(
...args:
| [Config]
| [Config, (state: Config['state']) => void]
| [Config, internalTypes.NestedPartial<Config['effects']>]
| [
Config,
internalTypes.NestedPartial<Config['effects']>,
(state: Config['state']) => void
]
): OvermindMock<Config> {
const setState = typeof args[1] === 'function' ? args[1] : args[2]
const mockedEffects = typeof args[1] === 'function' ? undefined : args[1]

const state = utils.deepCopy(args[0].state)

if (setState) {
;(setState as any)(state)
}
const mock = new Overmind(
Object.assign({}, args[0], {
state,
}),
{
devtools: false,
},
{
mode: utils.MODE_TEST,
options: {
effectsCallback: (effect) => {
const mockedEffect = (effect.name
? effect.name.split('.')
: []
).reduce((aggr, key) => (aggr ? aggr[key] : aggr), mockedEffects)

if (!mockedEffect || (mockedEffect && !mockedEffect[effect.method])) {
throw new Error(
`The effect "${effect.name}" with method ${
effect.method
} has not been mocked`
)
}
return mockedEffect[effect.method](...effect.args)
},
},
} as internalTypes.TestMode
) as OvermindMock<Config>

const action = (mock as any).createAction(
'onInitialize',
args[0].onInitialize
)

mock.onInitialize = () => action(mock)
mock.mutations = []

return mock as any
}
32 changes: 32 additions & 0 deletions packages/node_modules/overmind/src/OvermindSSR.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { IConfiguration } from './types'
import { Overmind } from './Overmind'
import * as proxyStateTree from 'proxy-state-tree'
import * as utils from './utils'
import * as internalTypes from './internalTypes'

export interface OvermindSSR<Config extends IConfiguration>
extends Overmind<Config> {
hydrate(): proxyStateTree.IMutation[]
}

export function createOvermindSSR<Config extends IConfiguration>(
config: Config
): OvermindSSR<Config> {
const ssr = new Overmind(
config,
{
devtools: false,
},
{
mode: utils.MODE_SSR,
} as internalTypes.SSRMode
) as any

const mutationTree = ssr.proxyStateTreeInstance.getMutationTree()

ssr.state = mutationTree.state
ssr.hydrate = () => {
return mutationTree.flush().mutations
}
return ssr
}
13 changes: 9 additions & 4 deletions packages/node_modules/overmind/src/config/lazy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function lazy<T extends LazyConfiguration>(
object
> & {
lazy: {
loadConfig: IAction<any, keyof T>
loadConfig: IAction<keyof T, any>
}
}
} {
Expand All @@ -72,7 +72,9 @@ export function lazy<T extends LazyConfiguration>(
lazy: {
loadConfig({ state, execution, ...rest }, key) {
const configToLoad = configurations[key]
const namespacePath = execution.namespacePath.slice(0, execution.namespacePath.length - 1).concat(key)
const namespacePath = execution.namespacePath
.slice(0, execution.namespacePath.length - 1)
.concat(key)
return configToLoad().then((loadedConfig) => {
const newConfig = namespaced({
[key]: loadedConfig,
Expand All @@ -83,14 +85,17 @@ export function lazy<T extends LazyConfiguration>(
if (newConfig.effects && newConfig.effects[key])
app.effects[key] = newConfig.effects[key]
if (newConfig.actions && newConfig.actions[key])
app.actions[key] = app.getActions(newConfig.actions[key], namespacePath)
app.actions[key] = app.getActions(
newConfig.actions[key],
namespacePath
)
if (newConfig.onInitialize)
newConfig.onInitialize(
{
state,
execution: {
...execution,
namespacePath
namespacePath,
},
...rest,
},
Expand Down
Loading

0 comments on commit cd3ff4e

Please sign in to comment.