Skip to content

Commit b7ef25a

Browse files
fix(overmind): fix events typing and base
1 parent a8e99bf commit b7ef25a

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

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

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ describe('Statemachine', () => {
1111
current: 'BAR'
1212
}
1313

14-
const state = statemachine<States>({
15-
FOO: ['BAR'],
16-
BAR: ['FOO']
14+
const state = statemachine<States, { type: 'TEST'}>({
15+
TEST: () => {}
1716
}).create({
1817
current: 'FOO'
1918
})
@@ -40,9 +39,8 @@ describe('Statemachine', () => {
4039
foo: string
4140
}
4241

43-
const state = statemachine<States, never, BaseState>({
44-
FOO: ['BAR'],
45-
BAR: ['FOO']
42+
const state = statemachine<States, { type: 'TEST' }, BaseState>({
43+
TEST: () => {}
4644
}).create({
4745
current: 'FOO',
4846
}, {
@@ -273,9 +271,8 @@ describe('Statemachine', () => {
273271
}
274272
}
275273

276-
const state = statemachine<States>({
277-
FOO: ['BAR'],
278-
BAR: ['FOO']
274+
const state = statemachine<States, { type: 'TEST'}>({
275+
TEST: () => {}
279276
}).create({
280277
current: 'FOO',
281278
obj: {

packages/node_modules/overmind/src/statemachine.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ type TEvents = {
1515
}
1616

1717

18-
export type StatemachineTransitions<States extends TState, Events extends TEvents> = {
19-
[Type in Events["type"]]: ((state: States, payload: Events extends { type: Type } ? Events["data"] : never) => States | void)
18+
export type StatemachineTransitions<States extends TState, Events extends TEvents, BaseState extends IState> = {
19+
[Type in Events["type"]]: [BaseState] extends [never] ?
20+
((state: States, payload: Events extends { type: Type } ? Events["data"] : never) => States | void) :
21+
((state: States & BaseState, payload: Events extends { type: Type } ? Events["data"] : never) => States | void)
2022
}
2123

2224
export interface MachineMethods<States extends TState, Events extends TEvents, BaseState extends IState> {
@@ -40,7 +42,7 @@ const BASE_STATE = Symbol('BASE_STATE')
4042
export class StateMachine<State extends TState, Events extends TEvents, BaseState extends IState> {
4143
current: State["current"]
4244
private [INITIAL_STATE]: State["current"]
43-
private [TRANSITIONS]: StatemachineTransitions<State, Events>
45+
private [TRANSITIONS]: StatemachineTransitions<State, Events, BaseState>
4446
private [STATE]: any
4547
private [BASE_STATE]: BaseState
4648
private [IS_DISPOSED] = false
@@ -55,7 +57,7 @@ export class StateMachine<State extends TState, Events extends TEvents, BaseStat
5557
})
5658
this[VALUE][IS_DISPOSED] = true
5759
}
58-
constructor(transitions: StatemachineTransitions<State, Events>, state: State, baseState: BaseState) {
60+
constructor(transitions: StatemachineTransitions<State, Events, BaseState>, state: State, baseState: BaseState) {
5961
this[STATE] = state
6062
this[BASE_STATE] = baseState
6163
this[INITIAL_STATE] = state.current
@@ -105,7 +107,7 @@ export type StatemachineFactory<States extends TState, Events extends TEvents, B
105107
create(state: States, baseState: BaseState): Statemachine<States, Events, BaseState>
106108
}
107109

108-
export function statemachine<States extends TState, Events extends TEvents = never, BaseState extends IState = never>(transitions: StatemachineTransitions<States, Events>): StatemachineFactory<States, Events, BaseState> {
110+
export function statemachine<States extends TState, Events extends TEvents, BaseState extends IState = never>(transitions: StatemachineTransitions<States, Events, BaseState>): StatemachineFactory<States, Events, BaseState> {
109111
return {
110112
create(state, baseState) {
111113
return new StateMachine(transitions, state as any, baseState as any)

0 commit comments

Comments
 (0)