Skip to content

Commit 53fde2a

Browse files
refactor(overmind): add nested charts on explicit chart property because of typing
1 parent c2eb6de commit 53fde2a

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { IAction, createOvermind } from '../'
22
import { Statechart, statechart } from './'
33

4-
describe.only('Statecharts', () => {
4+
describe('Statecharts', () => {
55
test('should wrap configs', () => {
66
const config = {}
77

@@ -203,7 +203,7 @@ describe.only('Statecharts', () => {
203203
})
204204
expect(instance.state.matches('bar')).toEqual(true)
205205
})
206-
test.only('should allow nesting', () => {
206+
test('should allow nesting', () => {
207207
const fooEntry: Action = ({ state }) => {
208208
state.transitions.push('fooEntry')
209209
}
@@ -262,7 +262,7 @@ describe.only('Statecharts', () => {
262262
on: {
263263
changeToBar: 'bar',
264264
},
265-
...nestedChart,
265+
chart: nestedChart,
266266
},
267267
bar: {},
268268
},

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
import {
2-
IConfiguration,
32
IConfig,
4-
pipe,
5-
filter,
6-
mutate,
3+
IConfiguration,
74
IDerive,
5+
filter,
86
map,
7+
mutate,
8+
pipe,
99
} from '../'
1010

1111
export interface Statechart<C extends IConfiguration, S extends string> {
1212
initial: S
1313
states: {
14-
[N in S]: Partial<Statechart<C, any>> & {
14+
[N in S]: {
1515
entry?: keyof C['actions']
1616
exit?: keyof C['actions']
17+
chart?: Statechart<C, any>
1718
on?: {
1819
[N in keyof C['actions']]?:
1920
| S
@@ -33,7 +34,6 @@ function getActionTransition(
3334
state: { state: string[] }
3435
) {
3536
const path = state.state.slice()
36-
let canTransition = false
3737

3838
while (path.length) {
3939
const target = getStateTarget(chart, path)
@@ -61,8 +61,8 @@ function getCanTransitionActions(actions, chart, state) {
6161

6262
function getInitialState(chart, path = []) {
6363
const newPath = path.concat(chart.initial)
64-
if (chart.states[chart.initial].initial) {
65-
return getInitialState(chart.states[chart.initial], newPath)
64+
if (chart.states[chart.initial].chart) {
65+
return getInitialState(chart.states[chart.initial].chart, newPath)
6666
}
6767
return newPath
6868
}
@@ -92,7 +92,7 @@ function createNewStatePath(
9292
const stateTarget = getStateTarget(chart, newStatePath)
9393

9494
// If we have more nested state, go grab the initial states
95-
if (stateTarget.initial) {
95+
if (stateTarget.chart) {
9696
return newStatePath.concat(getInitialState(stateTarget))
9797
}
9898

@@ -104,7 +104,11 @@ function getTarget(source, path) {
104104
}
105105

106106
function getStateTarget(chart, path) {
107-
return path.reduce((aggr, key) => aggr.states[key], chart)
107+
return path.reduce(
108+
(aggr, key, index) =>
109+
index === path.length - 1 ? aggr[key] : aggr[key].chart.states,
110+
chart.states
111+
)
108112
}
109113

110114
export function statechart<C extends IConfiguration, S extends string>(

0 commit comments

Comments
 (0)