Skip to content

Commit 41c00be

Browse files
docs(overmind): docs for parallel statecharts
1 parent 6c79c94 commit 41c00be

File tree

5 files changed

+167
-12
lines changed

5 files changed

+167
-12
lines changed

packages/overmind-website/examples/guide/statecharts/define.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export default statechart(config, loginChart)
5555
]
5656
: [
5757
{
58-
fileName: 'overmind/login/index.ts',
58+
fileName: 'overmind/login/index.js',
5959
code: `
6060
import { statechart } from 'overmind/config'
6161
import * as actions from './actions'

packages/overmind-website/examples/guide/statecharts/nested.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export default statechart(config, dashboardChart)
102102
]
103103
: [
104104
{
105-
fileName: 'overmind/login/index.ts',
105+
fileName: 'overmind/login/index.js',
106106
code: `
107107
import { statechart } from 'overmind/config'
108108
import * as actions from './actions'
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
export default (ts, view) =>
2+
ts
3+
? [
4+
{
5+
fileName: 'overmind/files/index.ts',
6+
code: `
7+
import { Statechart, statechart } from 'overmind/config'
8+
import * as actions from './actions'
9+
import { state } from './state'
10+
11+
const config = {
12+
state,
13+
actions
14+
}
15+
16+
enum UploadState {
17+
IDLE = 'IDLE',
18+
PENDING = 'PENDING',
19+
SUCCESS = 'SUCCESS',
20+
ERROR = 'ERROR'
21+
}
22+
23+
const uploadChart: Statechart<typeof config, UploadState> = {
24+
initial: UploadState.IDLE,
25+
states: {
26+
[UploadState.IDLE]: {
27+
on: {
28+
upload: UploadState.PENDING,
29+
}
30+
},
31+
[LoginState.PENDING]: {
32+
on: {
33+
resolveUpload: UploadState.SUCCESS,
34+
rejectUpload: UploadState.ERROR
35+
}
36+
},
37+
[LoginState.SUCCESS]: {
38+
on: {
39+
reset: UploadState.IDLE
40+
}
41+
},
42+
[LoginState.ERROR]: {
43+
on: {
44+
reset: UploadState.IDLE
45+
}
46+
}
47+
}
48+
}
49+
50+
enum DownloadState {
51+
IDLE = 'IDLE',
52+
PENDING = 'PENDING',
53+
SUCCESS = 'SUCCESS',
54+
ERROR = 'ERROR'
55+
}
56+
57+
const downloadChart: Statechart<typeof config, DownloadState> = {
58+
initial: DownloadState.IDLE,
59+
states: {
60+
[DownloadState.IDLE]: {
61+
on: {
62+
download: DownloadState.PENDING,
63+
}
64+
},
65+
[DownloadState.PENDING]: {
66+
on: {
67+
resolveDownload: DownloadState.SUCCESS,
68+
rejectDownload: DownloadState.ERROR
69+
}
70+
},
71+
[DownloadState.SUCCESS]: {
72+
on: {
73+
reset: DownloadState.IDLE
74+
}
75+
},
76+
[DownloadState.ERROR]: {
77+
on: {
78+
reset: DownloadState.IDLE
79+
}
80+
}
81+
}
82+
}
83+
84+
export default statechart(config, [
85+
uploadChart,
86+
downloadChart
87+
])
88+
`,
89+
},
90+
]
91+
: [
92+
{
93+
fileName: 'overmind/login/index.ts',
94+
code: `
95+
import { statechart } from 'overmind/config'
96+
import * as actions from './actions'
97+
import { state } from './state'
98+
99+
const config = {
100+
state,
101+
actions
102+
}
103+
104+
const loginChart = {
105+
initial: 'LOGIN',
106+
states: {
107+
LOGIN: {
108+
on: {
109+
changeUsername: null,
110+
changePassword: null,
111+
login: 'AUTHENTICATING'
112+
}
113+
},
114+
AUTHENTICATING: {
115+
on: {
116+
resolveUser: 'AUTHENTICATED',
117+
rejectUser: 'ERROR'
118+
}
119+
},
120+
AUTHENTICATED: {
121+
on: {
122+
logout: 'LOGIN'
123+
}
124+
},
125+
ERROR: {
126+
on: {
127+
tryAgain: 'LOGIN'
128+
}
129+
}
130+
}
131+
}
132+
133+
export default statechart(config, loginChart)
134+
`,
135+
},
136+
]

packages/overmind-website/guides/intermediate/06_statecharts.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,29 @@ What to take notice of in this example is that we simply **spread** a chart into
131131

132132
In this example we also took advantage of the **entry** and **exit** hooks of a transition state. These also points to actions. When a transition is made into the transition state the **entry** will run. This behavior is nested. When an exit hook exists and a transition is made away from the transition state it will also run. This behivor is also nested of course.
133133

134+
## Parallel statecharts
135+
136+
It is also possible to define your charts in a parallel manner. Basically wherever you would insert a chart, you can insert an array of charts. Parallel charts does not affect each other, but they are affected by the statechart as a whole. That means if you have nested parallel charts their **entry** and **exit** actions will run when transitioning the statehcart at a higher level.
137+
138+
```marksy
139+
h(Example, { name: "guide/statecharts/parallel.ts" })
140+
```
141+
142+
In this example we passed an array to the chart configuration itself. But the **chart** property used to nest charts can also hold an array of charts.
143+
144+
```js
145+
{
146+
initial: 'foo',
147+
states: {
148+
foo: {
149+
chart: [chartA, chartB]
150+
},
151+
bar: {}
152+
}
153+
}
154+
```
155+
156+
134157
## Devtools
135158

136159
The Overmind devtools understands statecharts. That means you are able to get an overview of available statecharts and even manipulate them directly in the devtools.

packages/overmind-website/src/overmind/index.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { IConfig } from 'overmind'
22
import { createHook } from 'overmind-react'
3-
import { merge, namespaced, statechart } from 'overmind/config'
43

54
import * as actions from './actions'
65
import * as effects from './effects'
@@ -36,15 +35,12 @@ const chart = {
3635
},
3736
}
3837

39-
export const config = statechart(
40-
{
41-
onInitialize,
42-
state,
43-
actions,
44-
effects,
45-
},
46-
[chart, chart2]
47-
)
38+
export const config = {
39+
onInitialize,
40+
state,
41+
actions,
42+
effects,
43+
}
4844

4945
declare module 'overmind' {
5046
interface Config extends IConfig<typeof config> {}

0 commit comments

Comments
 (0)