Skip to content

Commit 9f3c6f5

Browse files
fix(overmind): changed statemachines to class and fixed derived typing
1 parent 7652c60 commit 9f3c6f5

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ITrackStateTree,
66
} from 'proxy-state-tree'
77

8-
import { IAction, IOperator, IState } from './types'
8+
import { IAction, IConfiguration, IDerive, IOperator, IState } from './types'
99

1010
export type SubType<Base, Condition> = Pick<
1111
Base,
@@ -150,13 +150,10 @@ export interface Events {
150150
}
151151

152152
// ============= PRIVATE TYPES FOR APP
153-
154-
type Derived = (parent: any, config: any) => any
155-
156153
export type ResolveState<State extends IState> = State extends undefined
157154
? {}
158155
: {
159-
[P in keyof State]: State[P] extends Derived
156+
[P in keyof State]: State[P] extends IDerive<IConfiguration, IState, any>
160157
? ReturnType<State[P]>
161158
: State[P] extends Array<any>
162159
? State[P]

packages/node_modules/overmind/src/statemachine.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { IAction , createOvermind, createOvermindMock } from './'
2-
1+
import { IAction, createOvermind, createOvermindMock } from './'
32
import { Statemachine, statemachine } from './statemachine'
43

54
describe('Statemachine', () => {
@@ -25,8 +24,7 @@ describe('Statemachine', () => {
2524

2625
expect(overmind.state.machine.current).toBe('FOO')
2726
})
28-
test('should transition state', () => {
29-
27+
test.only('should transition state', () => {
3028
type State = {
3129
machine: Statemachine<'FOO' | 'BAR'>
3230
}

packages/node_modules/overmind/src/statemachine.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,36 @@ export type Statemachine<States extends string> = {
1111
current: States
1212
reset: () => void
1313
} & {
14-
[State in States]: Derive<any, <T>(entry?: () => T, exit?: () => void) => T>
14+
[State in States]: <T>(entry?: () => T, exit?: () => void) => T
1515
}
1616

17-
export function statemachine<States extends string>(chart: StatemachineDefinition<States>): Statemachine<States> {
18-
let currentExit
17+
class Machine<States extends string> {
18+
current: States
19+
private _currentExit: (() => void) | undefined
20+
constructor(definition: StatemachineDefinition<States>) {
21+
this.current = definition.initial
1922

20-
return {
21-
current: chart.initial,
22-
...Object.keys(chart.states).reduce((aggr, key) => {
23-
aggr[key] = () => {
24-
return function (entry, exit) {
25-
if (chart.states[this.current].includes(key as any)) {
26-
if (currentExit) currentExit()
27-
currentExit = exit
28-
this.current = key
29-
return entry && entry()
30-
}
23+
Object.keys(definition.states).reduce((aggr, key) => {
24+
aggr[key] = (entry, exit) => {
25+
if (definition.states[this.current].includes(key as any)) {
26+
if (this._currentExit) this._currentExit()
27+
this._currentExit = exit
28+
this.current = key as any
29+
return entry && entry()
3130
}
3231
}
3332

3433
return aggr
35-
}, {}),
36-
reset: () => {
37-
return function() {
38-
const caller = this
39-
caller.current = chart.initial
40-
if (currentExit) {
41-
currentExit()
42-
currentExit = null
43-
}
44-
}
34+
}, this)
35+
}
36+
reset() {
37+
if (this._currentExit) {
38+
this._currentExit()
39+
this._currentExit = undefined
4540
}
46-
} as any
41+
}
42+
}
43+
44+
export function statemachine<States extends string>(definition: StatemachineDefinition<States>): Statemachine<States> {
45+
return new Machine(definition) as any
4746
}

0 commit comments

Comments
 (0)