Skip to content

Commit 388159f

Browse files
feat(overmind): reworked statemachine API once more
1 parent a7d73de commit 388159f

File tree

8 files changed

+1168
-1135
lines changed

8 files changed

+1168
-1135
lines changed

.eslintrc.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"overrides": [
2525
{
2626
"files": ["*.ts", "*.tsx"],
27-
"parser": "typescript-eslint-parser",
28-
"plugins": ["typescript"],
27+
"parser": "@typescript-eslint/parser",
28+
"plugins": ["@typescript-eslint/eslint-plugin"],
2929
"rules": {
3030
"import/export": "off",
3131
"no-dupe-class-members": "off",

package-lock.json

Lines changed: 1112 additions & 901 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,22 @@
8585
"concurrently": "3.6.1",
8686
"cz-customizable": "^5.2.0",
8787
"cz-customizable-ghooks": "^1.5.0",
88-
"eslint": "^5.8.0",
89-
"eslint-config-prettier": "^3.1.0",
90-
"eslint-config-standard": "^12.0.0",
91-
"eslint-config-standard-jsx": "^6.0.2",
92-
"eslint-plugin-import": "^2.14.0",
93-
"eslint-plugin-node": "^8.0.0",
94-
"eslint-plugin-prettier": "^3.0.0",
95-
"eslint-plugin-promise": "^4.0.1",
96-
"eslint-plugin-react": "^7.11.1",
97-
"eslint-plugin-standard": "^4.0.0",
98-
"eslint-plugin-typescript": "^0.14.0",
88+
"eslint": "^7.10.0",
89+
"eslint-config-prettier": "^6.12.0",
90+
"eslint-config-standard": "^14.1.1",
91+
"eslint-config-standard-jsx": "^8.1.0",
92+
"eslint-plugin-import": "^2.22.1",
93+
"eslint-plugin-node": "^11.1.0",
94+
"eslint-plugin-prettier": "^3.1.4",
95+
"eslint-plugin-promise": "^4.2.1",
96+
"eslint-plugin-react": "^7.21.2",
97+
"eslint-plugin-standard": "^4.0.1",
98+
"@typescript-eslint/eslint-plugin": "^4.3.0",
9999
"html-webpack-plugin": "3.2.0",
100100
"husky": "^0.14.3",
101101
"jest": "24.9.0",
102102
"jsdom": "11.12.0",
103-
"lint-staged": "^7.2.0",
103+
"lint-staged": "^10.4.0",
104104
"parcel-bundler": "1.11.0",
105105
"prettier": "1.15.2",
106106
"raw-loader": "0.5.1",
@@ -114,8 +114,8 @@
114114
"ts-loader": "4.4.2",
115115
"tslib": "1.10.0",
116116
"tslint": "5.12.1",
117-
"typescript": "^3.7.5",
118-
"typescript-eslint-parser": "^21.0.1",
117+
"typescript": "^4.0.2",
118+
"@typescript-eslint/parser": "^4.3.0",
119119
"url-loader": "1.0.1",
120120
"vue": "3.0.0-rc.10",
121121
"webpack": "4.35.0",

packages/node_modules/overmind/src/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import {
88
IS_PROXY,
99
ITrackStateTree,
1010
PATH,
11+
PROXY_TREE,
1112
ProxyStateTree,
1213
TTree,
1314
VALUE,
14-
PROXY_TREE,
1515
} from 'proxy-state-tree'
1616

1717
import { Derived, IS_DERIVED, IS_DERIVED_CONSTRUCTOR } from './derived'
@@ -192,7 +192,6 @@ export class Overmind<ThisConfig extends IConfiguration>
192192
private mode: DefaultMode | TestMode | SSRMode
193193
private reydrateMutationsForHotReloading: IMutation[] = []
194194
private originalConfiguration
195-
private isStrict = false
196195
initialized: Promise<any>
197196
eventHub: EventEmitter<Events>
198197
devtools: Devtools
@@ -211,7 +210,6 @@ export class Overmind<ThisConfig extends IConfiguration>
211210
const devEnv = options.devEnv || 'development'
212211

213212
this.delimiter = options.delimiter || '.'
214-
this.isStrict = Boolean(options.strict)
215213

216214
if (
217215
(!process.env.NODE_ENV || process.env.NODE_ENV === devEnv) &&
@@ -578,9 +576,6 @@ export class Overmind<ThisConfig extends IConfiguration>
578576
})
579577
} else {
580578
const mutationTree = execution.getMutationTree()
581-
if (this.isStrict) {
582-
mutationTree.blockMutations()
583-
}
584579
const returnValue = action(
585580
this.createContext(execution, mutationTree),
586581
value
@@ -603,9 +598,7 @@ export class Overmind<ThisConfig extends IConfiguration>
603598
this.eventHub.emit(EventType.OPERATOR_START, execution)
604599

605600
const mutationTree = execution.getMutationTree()
606-
if (this.isStrict) {
607-
mutationTree.blockMutations()
608-
}
601+
609602
mutationTree.onMutation((mutation) => {
610603
this.eventHub.emit(EventType.MUTATIONS, {
611604
...execution,

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 1 addition & 2 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 } from './types'
8+
import { IAction, IOperator } from './types'
99

1010
export type SubType<Base, Condition> = Pick<
1111
Base,
@@ -24,7 +24,6 @@ export type Options = {
2424
devtools?: string | boolean
2525
logProxies?: boolean
2626
hotReloading?: boolean
27-
strict?: boolean
2827
}
2928

3029
export type DefaultMode = {

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

Lines changed: 18 additions & 156 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@ describe('Statemachine', () => {
2323

2424
const overmind = createOvermindMock(config)
2525

26-
expect(overmind.state.current).toBe('FOO')
26+
expect(overmind.state.FOO).toBeDefined()
27+
expect(overmind.state.BAR).toBe(null)
2728
})
2829

30+
2931
test('should transition state', () => {
3032
type States = {
3133
FOO: {},
@@ -53,7 +55,7 @@ describe('Statemachine', () => {
5355

5456

5557
overmind.actions.transition()
56-
expect(overmind.state.current).toBe('BAR')
58+
expect(overmind.state.BAR).toBeDefined()
5759
})
5860

5961
test('should ignore transition to invalid state', () => {
@@ -82,7 +84,7 @@ describe('Statemachine', () => {
8284

8385
const overmind = createOvermindMock(config)
8486
overmind.actions.transition()
85-
expect(overmind.state.current).toBe('FOO')
87+
expect(overmind.state.FOO).toBeDefined()
8688
})
8789

8890
test('should run entry and exit transition', async () => {
@@ -99,9 +101,9 @@ describe('Statemachine', () => {
99101

100102
const transition: Action = async ({ state }) => {
101103
if (state.transition('BAR', {})) {
102-
expect(state.current).toBe('BAR')
104+
expect(state.BAR).toBeDefined()
103105
if (state.transition('FOO', {})) {
104-
expect(state.current).toBe('FOO')
106+
expect(state.FOO).toBeDefined()
105107
}
106108
}
107109
}
@@ -118,7 +120,7 @@ describe('Statemachine', () => {
118120
const overmind = createOvermindMock(config)
119121

120122
await overmind.actions.transition()
121-
expect(overmind.state.current).toBe('FOO')
123+
expect(overmind.state.FOO).toBeDefined()
122124
})
123125

124126
test('should flush changes to transitions', () => {
@@ -148,90 +150,11 @@ describe('Statemachine', () => {
148150
interface Action extends IAction<typeof config, void, void> {}
149151

150152
const overmind = createOvermind(config)
151-
overmind.reaction((state) => state.current, (value) => {
152-
expect(value).toBe('BAR')
153+
overmind.reaction((state) => state.BAR, (value) => {
154+
expect(value).toEqual({})
153155
})
154156
overmind.actions.transition()
155157
})
156-
157-
test('should error when mutating async in transitions', async () => {
158-
expect.assertions(1)
159-
160-
type States = {
161-
FOO: {},
162-
BAR: {}
163-
}
164-
165-
const state = statemachine<States>({
166-
FOO: ['BAR'],
167-
BAR: ['FOO']
168-
}).create('FOO', {})
169-
170-
const transition: Action = async ({ state }) => {
171-
if (state.transition('BAR', {})) {
172-
await Promise.resolve()
173-
expect(state[PROXY_TREE].master.mutationTree.isBlocking).toBe(true)
174-
}
175-
}
176-
177-
const config = {
178-
state,
179-
actions: {
180-
transition
181-
}
182-
}
183-
184-
interface Action extends IAction<typeof config, void, Promise<void>> {}
185-
186-
const overmind = createOvermind(config)
187-
188-
return overmind.actions.transition()
189-
})
190-
191-
test('should only enable mutations with matches', async () => {
192-
expect.assertions(4)
193-
194-
type States = {
195-
FOO: {},
196-
BAR: {}
197-
}
198-
199-
const state = statemachine<States>({
200-
FOO: ['BAR'],
201-
BAR: ['FOO']
202-
}).create('FOO', {})
203-
204-
const transition: Action = async ({ state }) => {
205-
if (state.transition('BAR', {})) {
206-
await Promise.resolve()
207-
expect(state[PROXY_TREE].master.mutationTree.isBlocking).toBe(true)
208-
if (state.matches('BAR')) {
209-
expect(state[PROXY_TREE].master.mutationTree.isBlocking).toBe(false)
210-
await Promise.resolve()
211-
.then(() => {
212-
expect(state[PROXY_TREE].master.mutationTree.isBlocking).toBe(true)
213-
})
214-
if (state.transition('FOO', {})) {
215-
expect(state[PROXY_TREE].master.mutationTree.isBlocking).toBe(false)
216-
}
217-
}
218-
}
219-
}
220-
221-
222-
const config = {
223-
state,
224-
actions: {
225-
transition
226-
}
227-
}
228-
229-
interface Action extends IAction<typeof config, void, Promise<void>> {}
230-
231-
const overmind = createOvermind(config)
232-
233-
return overmind.actions.transition()
234-
})
235158

236159
test('should make copy of statemachine during tests', () => {
237160

@@ -270,12 +193,13 @@ describe('Statemachine', () => {
270193

271194
// @ts-ignore
272195
overmind.actions.changeFoo()
273-
expect(overmind.state.FOO.obj.foo).toBe('bar2')
196+
197+
expect(overmind.state.FOO?.obj.foo).toBe('bar2')
274198

275199

276200
const overmind2 = createOvermindMock(config)
277201

278-
expect(overmind2.state.FOO.obj.foo).toBe('bar')
202+
expect(overmind2.state.FOO?.obj.foo).toBe('bar')
279203
})
280204

281205
test('should have base state', () => {
@@ -349,11 +273,11 @@ describe('Statemachine', () => {
349273
if (state.transition('BAR', {
350274
bar: 123
351275
})) {
352-
expect(state.BAR.bar).toBe(123)
276+
expect(state.BAR?.bar).toBe(123)
353277
await Promise.resolve()
354278
if (state.transition('FOO', { foo: '123'})) {
355279
expect(state.BAR).toBe(null)
356-
expect(state.FOO.foo).toBe('123')
280+
expect(state.FOO?.foo).toBe('123')
357281
await Promise.resolve()
358282
}
359283
}
@@ -372,68 +296,6 @@ describe('Statemachine', () => {
372296

373297
overmind.actions.transition()
374298
})
375-
376-
test('should not allow mutations in strict mode', () => {
377-
type States = {
378-
FOO: {},
379-
BAR: {}
380-
}
381-
382-
const state = statemachine<States>({
383-
FOO: ['BAR'],
384-
BAR: ['FOO']
385-
}).create('FOO', {})
386-
const transition: Action = ({ state }) => {
387-
state.current = 'BAR'
388-
}
389-
390-
const config = {
391-
state,
392-
actions: {
393-
transition
394-
}
395-
}
396-
397-
interface Action extends IAction<typeof config, void, void> {}
398-
399-
const overmind = createOvermindMock(config)
400-
// @ts-ignore
401-
overmind.isStrict = true
402-
403-
404-
expect(() => overmind.actions.transition()).toThrow()
405-
})
406-
407-
test('should allow mutations using transition', () => {
408-
type States = {
409-
FOO: {}
410-
BAR: {}
411-
}
412-
413-
const state = statemachine<States>({
414-
FOO: ['BAR'],
415-
BAR: ['FOO']
416-
}).create('FOO', {})
417-
const transition: Action = ({ state }) => {
418-
state.transition('BAR', {})
419-
}
420-
421-
const config = {
422-
state,
423-
actions: {
424-
transition
425-
}
426-
}
427-
428-
interface Action extends IAction<typeof config, void, void> {}
429-
430-
const overmind = createOvermindMock(config)
431-
// @ts-ignore
432-
overmind.isStrict = true
433-
434-
435-
expect(() => overmind.actions.transition()).not.toThrow()
436-
})
437299

438300
test('should dispose nested and base machines', async () => {
439301
expect.assertions(3)
@@ -470,15 +332,15 @@ describe('Statemachine', () => {
470332
})
471333

472334
const transition: Action = ({ state }) => {
473-
if (state.matches('FOO')) {
335+
if (state.FOO) {
474336
const ref = state.FOO
475337
if (state.transition('BAR', {})) {
476338
expect(state.FOO).toBe(null)
477339
ref.transition('BAR', {})
478340
ref.base.transition('BAR', {})
479341

480-
expect(ref.current).toBe('FOO')
481-
expect(ref.base.current).toBe('FOO')
342+
expect(ref.FOO).toBeDefined()
343+
expect(ref.base.FOO).toBeDefined()
482344
}
483345
}
484346
}

0 commit comments

Comments
 (0)