Skip to content

Commit 1cb144f

Browse files
fix(overmind): fix context issue with statemachine
1 parent 7d7177c commit 1cb144f

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

packages/node_modules/overmind/src/statemachine.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { VALUE } from 'proxy-state-tree'
2+
13
import { Derive } from './'
24

35
export type StatemachineDefinition<States extends string> = {
@@ -14,17 +16,19 @@ export type Statemachine<States extends string> = {
1416
[State in States]: <T>(entry?: () => T, exit?: () => void) => T
1517
}
1618

19+
const CURRENT_EXIT = Symbol('CURRENT_EXIT')
20+
1721
class Machine<States extends string> {
1822
current: States
19-
private _currentExit: (() => void) | undefined
23+
private [CURRENT_EXIT]: (() => void) | undefined
2024
constructor(definition: StatemachineDefinition<States>) {
2125
this.current = definition.initial
2226

2327
Object.keys(definition.states).reduce((aggr, key) => {
24-
aggr[key] = (entry, exit) => {
28+
aggr[key] = function (entry, exit) {
2529
if (definition.states[this.current].includes(key as any)) {
26-
if (this._currentExit) this._currentExit()
27-
this._currentExit = exit
30+
if (this[CURRENT_EXIT]) this._currentExit()
31+
this[VALUE][CURRENT_EXIT] = exit
2832
this.current = key as any
2933
return entry && entry()
3034
}
@@ -34,9 +38,10 @@ class Machine<States extends string> {
3438
}, this)
3539
}
3640
reset() {
37-
if (this._currentExit) {
38-
this._currentExit()
39-
this._currentExit = undefined
41+
const exit = this[CURRENT_EXIT]
42+
if (typeof exit === 'function') {
43+
exit()
44+
this[VALUE][CURRENT_EXIT] = undefined
4045
}
4146
}
4247
}

packages/overmind-website/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const overmind = createOvermind(
2020
}
2121
)
2222

23+
overmind.actions.login()
24+
2325
setConfig({
2426
ignoreSFC: true, // RHL will be __completely__ disabled for SFC
2527
pureRender: true, // RHL will not change render method

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,7 @@ export const changeQuery: Operator<string> = pipe(
124124
export const viewHelpGotIt: Action = ({ state }) => {
125125
state.showViewHelp = false
126126
}
127+
128+
export const login: Action = ({ state }) => {
129+
state.mode.authenticating(() => {})
130+
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Statemachine, statemachine } from 'overmind'
2+
13
import {
24
Api,
35
Demo,
@@ -8,6 +10,12 @@ import {
810
Video,
911
} from './types'
1012

13+
type Mode =
14+
| 'unauthenticated'
15+
| 'authenticating'
16+
| 'authenticated'
17+
| 'unauthenticating'
18+
1119
type State = {
1220
page: Page
1321
currentGuide: GuideParams
@@ -31,6 +39,7 @@ type State = {
3139
versions: {
3240
[name: string]: string
3341
}
42+
mode: Statemachine<Mode>
3443
}
3544

3645
const state: State = {
@@ -54,6 +63,15 @@ const state: State = {
5463
isLoadingVideos: false,
5564
showViewHelp: false,
5665
versions: {},
66+
mode: statemachine<Mode>({
67+
initial: 'unauthenticated',
68+
states: {
69+
unauthenticated: ['authenticating'],
70+
authenticating: ['unauthenticated', 'authenticated'],
71+
authenticated: ['unauthenticating'],
72+
unauthenticating: ['unauthenticated', 'authenticated'],
73+
},
74+
}),
5775
}
5876

5977
export default state

0 commit comments

Comments
 (0)