Skip to content

Commit cd7322a

Browse files
docs(website): statecharts api
1 parent 02aa210 commit cd7322a

File tree

9 files changed

+239
-107
lines changed

9 files changed

+239
-107
lines changed

packages/overmind-website/api/statecharts.md

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
Statecharts is a configuration factory, just like **merge**, **namespaced** and **lazy**. It allows you to restrict what actions are to be run in certain states.
44

5-
## statechart
5+
## statecharts
66

7-
The factory function you use to wrap an Overmind configuration.
7+
The factory function you use to wrap an Overmind configuration. You add one or multiple charts to the configuration, where the key is the *id* of the chart.
88

99
```marksy
1010
h(Example, { name: "api/statecharts_statechart" })
@@ -47,11 +47,59 @@ h(Example, { name: "api/statecharts_on" })
4747
```
4848

4949
## nested
50-
A nested statechart will operate within its parent transition state. The means when the parent transition state is entered or exited any defined **entry** and **exit** actions will be run. When the parent enters its transition state the **initial** state of the child statechart will be activated.
50+
A nested statechart will operate within its parent transition state. The means when the parent transition state is entered or exited any defined **entry** and **exit** actions will be run. When the parent enters its transition state the **initial** state of the child statechart(s) will be activated.
5151

5252
```marksy
5353
h(Example, { name: "api/statecharts_nested" })
5454
```
5555

56+
## parallel
57+
Multiple statecharts will run in parallel. Either for the factory configuration or nested charts. You can add the same chart multiple times behind different ids.
5658

59+
```marksy
60+
h(Example, { name: "api/statecharts_parallel" })
61+
```
5762

63+
## matches
64+
65+
The matches API is used in your components to identify what state your charts are in. It is accessed on the **state**.
66+
67+
```js
68+
// Given that you have added statecharts to the root configuration
69+
state.matches({
70+
chartA: {
71+
STATE_A: true
72+
}
73+
})
74+
75+
// Nested chart
76+
state.matches({
77+
chartA: {
78+
STATE_A: {
79+
nestedChartA: {
80+
FOO: true
81+
}
82+
}
83+
}
84+
})
85+
86+
// Parallel
87+
state.matches({
88+
chartA: {
89+
STATE_A: true
90+
},
91+
chartB: {
92+
STATE_B: true
93+
}
94+
})
95+
96+
// Negative check
97+
state.matches({
98+
chartA: {
99+
STATE_A: true
100+
},
101+
chartB: {
102+
STATE_B: false
103+
}
104+
})
105+
```

packages/overmind-website/examples/api/statecharts_entry.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default (ts) =>
33
? [
44
{
55
code: `
6-
import { Statechart, statechart } from 'overmind/config'
6+
import { Statechart, statecharts } from 'overmind/config'
77
import * as actions from './actions'
88
import { state } from './state'
99
@@ -12,29 +12,29 @@ const config = {
1212
state
1313
}
1414
15-
enum States {
16-
STATE_A = 'STATE_A',
17-
STATEA_B = 'STATE_B'
18-
}
19-
20-
const chart: Statechart<typeof config, States> = {
21-
initial: States.STATE_A,
15+
const chart: Statechart<typeof config, {
16+
STATE_A: void
17+
STATE_B: void
18+
}> = {
19+
initial: 'STATE_A',
2220
states: {
23-
[States.STATE_A]: {
21+
STATE_A: {
2422
entry: 'someActionName'
2523
},
26-
[States.STATE_B]: {}
24+
STATE_B: {}
2725
}
2826
}
2927
30-
export default statechart(config, chart)
28+
export default statecharts(config, {
29+
chartId: chart
30+
})
3131
`,
3232
},
3333
]
3434
: [
3535
{
3636
code: `
37-
import { statechart } from 'overmind/config'
37+
import { statecharts } from 'overmind/config'
3838
import * as actions from './actions'
3939
import { state } from './state'
4040
@@ -53,7 +53,9 @@ const chart = {
5353
}
5454
}
5555
56-
export default statechart(config, chart)
56+
export default statecharts(config, {
57+
chartId: chart
58+
})
5759
`,
5860
},
5961
]

packages/overmind-website/examples/api/statecharts_exit.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default (ts) =>
33
? [
44
{
55
code: `
6-
import { Statechart, statechart } from 'overmind/config'
6+
import { Statechart, statecharts } from 'overmind/config'
77
import * as actions from './actions'
88
import { state } from './state'
99
@@ -12,30 +12,30 @@ const config = {
1212
state
1313
}
1414
15-
enum States {
16-
STATE_A = 'STATE_A',
17-
STATEA_B = 'STATE_B'
18-
}
19-
20-
const chart: Statechart<typeof config, States> = {
21-
initial: States.STATE_A,
15+
const chart: Statechart<typeof config, {
16+
STATE_A: void
17+
STATE_B: void
18+
}> = {
19+
initial: 'STATE_A',
2220
states: {
23-
[States.STATE_A]: {
21+
STATE_A: {
2422
entry: 'someActionName',
2523
exit: 'someOtherActionName'
2624
},
27-
[States.STATE_B]: {}
25+
STATE_B: {}
2826
}
2927
}
3028
31-
export default statechart(config, chart)
29+
export default statecharts(config, {
30+
chartId: chart
31+
})
3232
`,
3333
},
3434
]
3535
: [
3636
{
3737
code: `
38-
import { statechart } from 'overmind/config'
38+
import { statecharts } from 'overmind/config'
3939
import * as actions from './actions'
4040
import { state } from './state'
4141
@@ -55,7 +55,9 @@ const chart = {
5555
}
5656
}
5757
58-
export default statechart(config, chart)
58+
export default statecharts(config, {
59+
chartId: chart
60+
})
5961
`,
6062
},
6163
]

packages/overmind-website/examples/api/statecharts_initial.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default (ts) =>
33
? [
44
{
55
code: `
6-
import { Statechart, statechart } from 'overmind/config'
6+
import { Statechart, statecharts } from 'overmind/config'
77
import * as actions from './actions'
88
import { state } from './state'
99
@@ -12,23 +12,22 @@ const config = {
1212
state
1313
}
1414
15-
enum States {
16-
STATE_A = 'STATE_A',
17-
STATEA_B = 'STATE_B'
18-
}
19-
20-
const chart: Statechart<typeof config, States> = {
21-
initial: States.STATE_A
15+
const chart: Statechart<typeof config, {
16+
STATE_A: void
17+
}> = {
18+
initial: 'STATE_A'
2219
}
2320
24-
export default statechart(config, chart)
21+
export default statecharts(config, {
22+
chartId: chart
23+
})
2524
`,
2625
},
2726
]
2827
: [
2928
{
3029
code: `
31-
import { statechart } from 'overmind/config'
30+
import { statecharts } from 'overmind/config'
3231
import * as actions from './actions'
3332
import { state } from './state'
3433
@@ -41,7 +40,9 @@ const chart = {
4140
initial: 'STATE_A'
4241
}
4342
44-
export default statechart(config, chart)
43+
export default statecharts(config, {
44+
chartId: chart
45+
})
4546
`,
4647
},
4748
]

packages/overmind-website/examples/api/statecharts_nested.ts

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ export default (ts) =>
33
? [
44
{
55
code: `
6-
import { Statechart, statechart } from 'overmind/config'
6+
import { Statechart, statecharts } from 'overmind/config'
77
import * as actions from './actions'
88
import { state } from './state'
99
@@ -12,57 +12,57 @@ const config = {
1212
state
1313
}
1414
15-
enum NestedStates {
16-
FOO = 'FOO',
17-
BAR = 'BAR'
18-
}
19-
20-
const nestedChart: Statechart<typeof config, NestedStates> = {
21-
initial: NestedStates.FOO,
15+
const nestedChart: Statechart<typeof config, {
16+
FOO: void
17+
BAR: void
18+
}> = {
19+
initial: 'FOO',
2220
states: {
23-
[NestedStates.FOO]: {
21+
FOO: {
2422
on: {
25-
transitionToBar: NestedStates.BAR
23+
transitionToBar: 'BAR'
2624
}
2725
},
28-
[NestedStates.BAR]: {
26+
BAR: {
2927
on: {
30-
transitionToFoo: NestedStates.FOO
28+
transitionToFoo: 'FOO'
3129
}
3230
}
3331
}
3432
}
3533
36-
enum States {
37-
STATE_A = 'STATE_A',
38-
STATEA_B = 'STATE_B'
39-
}
40-
41-
const chart: Statechart<typeof config, States> = {
42-
initial: States.STATE_A,
34+
const chart: Statechart<typeof config, {
35+
STATE_A: {
36+
nestedChartId: typeof nestedChart
37+
}
38+
STATE_B: void
39+
}> = {
40+
initial: 'STATE_A',
4341
states: {
44-
[States.STATE_A]: {
42+
STATE_A: {
4543
on: {
46-
transitionToStateB: States.STATE_B
44+
transitionToStateB: 'STATE_B'
4745
},
48-
chart: nestedChart
46+
charts: { nestedChartId: nestedChart }
4947
},
50-
[States.STATE_B]: {
48+
STATE_B: {
5149
on: {
52-
transitionToStateA: States.STATE_A
50+
transitionToStateA: 'STATE_A'
5351
}
5452
}
5553
}
5654
}
5755
58-
export default statechart(config, chart)
56+
export default statecharts(config, {
57+
chartId: chart
58+
})
5959
`,
6060
},
6161
]
6262
: [
6363
{
6464
code: `
65-
import { Statechart, statechart } from 'overmind/config'
65+
import { Statechart, statecharts } from 'overmind/config'
6666
import * as actions from './actions'
6767
import { state } from './state'
6868
@@ -88,15 +88,14 @@ const nestedChart = {
8888
}
8989
}
9090
91-
9291
const chart = {
9392
initial: 'STATE_A',
9493
states: {
9594
STATE_A: {
9695
on: {
9796
transitionToStateB: 'STATE_B'
9897
},
99-
chart: nestedChart
98+
charts: { nestedChartId: nestedChart }
10099
},
101100
STATE_B: {
102101
on: {
@@ -106,7 +105,9 @@ const chart = {
106105
}
107106
}
108107
109-
export default statechart(config, chart)
108+
export default statecharts(config, {
109+
chartId: chart
110+
})
110111
`,
111112
},
112113
]

0 commit comments

Comments
 (0)