Skip to content

Commit 909e5a4

Browse files
refactor(overmind): change to use current
1 parent 9edef8f commit 909e5a4

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

packages/node_modules/overmind/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"bundlesize": [
5050
{
5151
"path": "./dist/overmind.min.js",
52-
"maxSize": "10 kB"
52+
"maxSize": "12 kB"
5353
}
5454
]
5555
}

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

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@ describe('Statemachine', () => {
88
test('should set initial state', () => {
99

1010
type States = {
11-
state: 'FOO'
11+
current: 'FOO'
1212
} | {
13-
state: 'BAR'
13+
current: 'BAR'
1414
}
1515

1616
const state = statemachine<States>({
1717
FOO: ['BAR'],
1818
BAR: ['FOO']
1919
}, {
20-
state: 'FOO',
20+
current: 'FOO',
2121
})
2222

2323
const config = {
@@ -26,21 +26,21 @@ describe('Statemachine', () => {
2626

2727
const overmind = createOvermindMock(config)
2828

29-
expect(overmind.state.state).toBe('FOO')
29+
expect(overmind.state.current).toBe('FOO')
3030
})
3131

3232
test('should transition state', () => {
3333
type States = {
34-
state: 'FOO'
34+
current: 'FOO'
3535
} | {
36-
state: 'BAR'
36+
current: 'BAR'
3737
}
3838

3939
const state = statemachine<States>({
4040
FOO: ['BAR'],
4141
BAR: ['FOO']
4242
}, {
43-
state: 'FOO',
43+
current: 'FOO',
4444
})
4545
const transition: Action = ({ state }) => {
4646
state.transition('BAR', {})
@@ -59,22 +59,22 @@ describe('Statemachine', () => {
5959

6060

6161
overmind.actions.transition()
62-
expect(overmind.state.state).toBe('BAR')
62+
expect(overmind.state.current).toBe('BAR')
6363
})
6464

6565
test('should ignore transition to invalid state', () => {
6666

6767
type States = {
68-
state: 'FOO'
68+
current: 'FOO'
6969
} | {
70-
state: 'BAR'
70+
current: 'BAR'
7171
}
7272

7373
const state = statemachine<States>({
7474
FOO: [],
7575
BAR: ['FOO']
7676
}, {
77-
state: 'FOO'
77+
current: 'FOO'
7878
})
7979
const transition: Action = ({ state }) => {
8080
state.transition('BAR', {})
@@ -91,29 +91,29 @@ describe('Statemachine', () => {
9191

9292
const overmind = createOvermindMock(config)
9393
overmind.actions.transition()
94-
expect(overmind.state.state).toBe('FOO')
94+
expect(overmind.state.current).toBe('FOO')
9595
})
9696

9797
test('should run entry and exit transition', async () => {
9898
expect.assertions(3)
9999
type States = {
100-
state: 'FOO'
100+
current: 'FOO'
101101
} | {
102-
state: 'BAR'
102+
current: 'BAR'
103103
}
104104

105105
const state = statemachine<States>({
106106
FOO: ['BAR'],
107107
BAR: ['FOO']
108108
}, {
109-
state: 'FOO'
109+
current: 'FOO'
110110
})
111111

112112
const transition: Action = async ({ state }) => {
113113
state.transition('BAR', {}, (barState) => {
114-
expect(barState.state).toBe('BAR')
114+
expect(barState.current).toBe('BAR')
115115
state.transition('FOO', {}, (fooState) => {
116-
expect(fooState.state).toBe('FOO')
116+
expect(fooState.current).toBe('FOO')
117117
})
118118
})
119119
}
@@ -130,23 +130,23 @@ describe('Statemachine', () => {
130130
const overmind = createOvermindMock(config)
131131

132132
await overmind.actions.transition()
133-
expect(overmind.state.state).toBe('FOO')
133+
expect(overmind.state.current).toBe('FOO')
134134
})
135135

136136
test('should flush changes to transitions', () => {
137137
expect.assertions(1)
138138

139139
type States = {
140-
state: 'FOO'
140+
current: 'FOO'
141141
} | {
142-
state: 'BAR'
142+
current: 'BAR'
143143
}
144144

145145
const state = statemachine<States>({
146146
FOO: ['BAR'],
147147
BAR: ['FOO']
148148
}, {
149-
state: 'FOO'
149+
current: 'FOO'
150150
})
151151

152152
const transition: Action = ({ state }) => {
@@ -163,7 +163,7 @@ describe('Statemachine', () => {
163163
interface Action extends IAction<typeof config, void, void> {}
164164

165165
const overmind = createOvermind(config)
166-
overmind.reaction((state) => state.state, (value) => {
166+
overmind.reaction((state) => state.current, (value) => {
167167
expect(value).toBe('BAR')
168168
})
169169
overmind.actions.transition()
@@ -173,16 +173,16 @@ describe('Statemachine', () => {
173173
expect.assertions(1)
174174

175175
type States = {
176-
state: 'FOO'
176+
current: 'FOO'
177177
} | {
178-
state: 'BAR'
178+
current: 'BAR'
179179
}
180180

181181
const state = statemachine<States>({
182182
FOO: ['BAR'],
183183
BAR: ['FOO']
184184
}, {
185-
state: 'FOO'
185+
current: 'FOO'
186186
})
187187
const transition: Action = async ({ state }) => {
188188
await state.transition('BAR', {}, async () => {
@@ -209,16 +209,16 @@ describe('Statemachine', () => {
209209
expect.assertions(3)
210210

211211
type States = {
212-
state: 'FOO'
212+
current: 'FOO'
213213
} | {
214-
state: 'BAR'
214+
current: 'BAR'
215215
}
216216

217217
const state = statemachine<States>({
218218
FOO: ['BAR'],
219219
BAR: ['FOO']
220220
}, {
221-
state: 'FOO',
221+
current: 'FOO',
222222
})
223223
const transition: Action = async ({ state }) => {
224224
await state.transition('BAR', {}, async () => {
@@ -252,12 +252,12 @@ describe('Statemachine', () => {
252252
test('should make copy of statemachine during tests', () => {
253253

254254
type States = {
255-
state: 'FOO'
255+
current: 'FOO'
256256
obj: {
257257
foo: string
258258
}
259259
} | {
260-
state: 'BAR'
260+
current: 'BAR'
261261
obj: {
262262
foo: string
263263
}
@@ -267,7 +267,7 @@ describe('Statemachine', () => {
267267
FOO: ['BAR'],
268268
BAR: ['FOO']
269269
}, {
270-
state: 'FOO',
270+
current: 'FOO',
271271
obj: {
272272
foo: 'bar'
273273
}
@@ -295,9 +295,9 @@ describe('Statemachine', () => {
295295
test('should have base state', () => {
296296

297297
type States = {
298-
state: 'FOO'
298+
current: 'FOO'
299299
} | {
300-
state: 'BAR'
300+
current: 'BAR'
301301
}
302302

303303
type Base = {
@@ -310,7 +310,7 @@ describe('Statemachine', () => {
310310
FOO: ['BAR'],
311311
BAR: ['FOO']
312312
}, {
313-
state: 'FOO',
313+
current: 'FOO',
314314
}, {
315315
obj: {
316316
foo: 'bar'
@@ -340,10 +340,10 @@ describe('Statemachine', () => {
340340
test('should delete existing state and merge in new state, keep base', () => {
341341

342342
type States = {
343-
state: 'FOO'
343+
current: 'FOO'
344344
foo: string
345345
} | {
346-
state: 'BAR'
346+
current: 'BAR'
347347
bar: number
348348
}
349349

@@ -355,7 +355,7 @@ describe('Statemachine', () => {
355355
FOO: ['BAR'],
356356
BAR: ['FOO']
357357
}, {
358-
state: 'FOO',
358+
current: 'FOO',
359359
foo: 'bar'
360360
}, {
361361
baz: true

packages/node_modules/overmind/src/statemachine.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { deepCopy } from './utils'
44
import { IState } from '.'
55

66
type TStates = {
7-
state: string | number,
7+
current: string | number,
88
}
99

1010
export type Exact<Expected, Actual> =
@@ -27,20 +27,20 @@ type ExactElements<Expected, Actual> = {
2727
};
2828

2929
export type StatemachineTransitions<States extends TStates> = {
30-
[State in States["state"]]: Array<States["state"]>
30+
[State in States["current"]]: Array<States["current"]>
3131
}
3232

3333
export interface MachineMethods<States extends TStates, Base extends IState = {}> {
34-
matches<T extends States["state"]>(...state: T[]): this is Statemachine<Base, States, States extends {
35-
state: T
34+
matches<T extends States["current"]>(...state: T[]): this is Statemachine<Base, States, States extends {
35+
current: T
3636
} ? States : never>;
37-
transition<T extends States["state"], O = void>(
37+
transition<T extends States["current"], O = void>(
3838
state: T,
3939
newState: States extends {
40-
state: T
41-
} ? Exact<Omit<States, 'state'>, {}> extends never ? Omit<States, 'state'> & Partial<Base> : Exact<Partial<Base>, {}> extends never ? Partial<Base> : { [NO_PROP]?: true} : never,
40+
current: T
41+
} ? Exact<Omit<States, 'current'>, {}> extends never ? Omit<States, 'current'> & Partial<Base> : Exact<Partial<Base>, {}> extends never ? Partial<Base> : { [NO_PROP]?: true} : never,
4242
callback?: ((current: Statemachine<Base, States, States extends {
43-
state: T
43+
current: T
4444
} ? States : never>) => O)
4545
): O;
4646
}
@@ -55,23 +55,23 @@ const CURRENT_KEYS = Symbol('CURRENT_KEYS')
5555
const NO_PROP = Symbol('NO_PROP')
5656

5757
export class StateMachine<Base extends IState, States extends TStates, State extends TStates = States, > {
58-
state: State["state"]
59-
private [INITIAL_STATE]: State["state"]
58+
current: State["current"]
59+
private [INITIAL_STATE]: State["current"]
6060
private [STATE]: any
6161
private [BASE]: any
6262
private clone() {
6363
return new StateMachine(this[TRANSITIONS], deepCopy(this[STATE]), deepCopy(this[BASE]))
6464
}
6565
constructor(transitions: StatemachineTransitions<States>, state: States, base: Base) {
6666
this[STATE] = state
67-
this[INITIAL_STATE] = state.state
67+
this[INITIAL_STATE] = state.current
6868
this[TRANSITIONS] = transitions
6969
this[BASE] = base || {}
7070
this[CURRENT_KEYS] = Object.keys(state)
7171
Object.assign(this, state, base)
7272
}
7373
matches(...states) {
74-
if (states.includes(this.state)) {
74+
if (states.includes(this.current)) {
7575
const tree = (this[PROXY_TREE].master.mutationTree || this[PROXY_TREE])
7676

7777
if (tree.enableMutations) {
@@ -89,7 +89,7 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
8989
transition(state, newState, callback) {
9090
const transitions = this[VALUE][TRANSITIONS]
9191

92-
if (transitions[this.state].includes(state)) {
92+
if (transitions[this.current].includes(state)) {
9393
const tree = (this[PROXY_TREE].master.mutationTree || this[PROXY_TREE])
9494
const baseKeys = Object.keys(this[VALUE][BASE])
9595

@@ -101,7 +101,7 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
101101
})
102102

103103
Object.assign(this, newState)
104-
this.state = state
104+
this.current = state
105105
this[VALUE][CURRENT_KEYS] = Object.keys(newState)
106106

107107
let result
@@ -112,9 +112,9 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
112112
tree.blockMutations()
113113

114114
return result
115-
} else if (process.env.NODE_ENV === 'development' && state !== this.state) {
116-
console.warn(`Overmind Statemachine - You tried to transition into "${state}", but it is not a valid transition. The valid transitions are ${JSON.stringify(transitions[this.state])}`)
117-
} else if (process.env.NODE_ENV === 'development' && state === this.state) {
115+
} else if (process.env.NODE_ENV === 'development' && state !== this.current) {
116+
console.warn(`Overmind Statemachine - You tried to transition into "${state}", but it is not a valid transition. The valid transitions are ${JSON.stringify(transitions[this.current])}`)
117+
} else if (process.env.NODE_ENV === 'development' && state === this.current) {
118118
console.warn(`Overmind Statemachine - You tried to transition into "${state}", but you are already in this state. Do a "match" before running this piece of logic or add it as a valid state transition for this state`)
119119
}
120120
}

0 commit comments

Comments
 (0)