Skip to content

Commit f217ac7

Browse files
feat(overmind): strict mode for statemachines
1 parent d9910bc commit f217ac7

File tree

4 files changed

+55
-5
lines changed

4 files changed

+55
-5
lines changed

packages/node_modules/overmind/src/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ export class Overmind<ThisConfig extends IConfiguration>
192192
private mode: DefaultMode | TestMode | SSRMode
193193
private reydrateMutationsForHotReloading: IMutation[] = []
194194
private originalConfiguration
195+
private isStrict = false
195196
initialized: Promise<any>
196197
eventHub: EventEmitter<Events>
197198
devtools: Devtools
@@ -210,6 +211,7 @@ export class Overmind<ThisConfig extends IConfiguration>
210211
const devEnv = options.devEnv || 'development'
211212

212213
this.delimiter = options.delimiter || '.'
214+
this.isStrict = Boolean(options.strict)
213215

214216
if (
215217
(!process.env.NODE_ENV || process.env.NODE_ENV === devEnv) &&
@@ -576,6 +578,9 @@ export class Overmind<ThisConfig extends IConfiguration>
576578
})
577579
} else {
578580
const mutationTree = execution.getMutationTree()
581+
if (this.isStrict) {
582+
mutationTree.blockMutations()
583+
}
579584
const returnValue = action(
580585
this.createContext(execution, mutationTree),
581586
value
@@ -598,7 +603,9 @@ export class Overmind<ThisConfig extends IConfiguration>
598603
this.eventHub.emit(EventType.OPERATOR_START, execution)
599604

600605
const mutationTree = execution.getMutationTree()
601-
606+
if (this.isStrict) {
607+
mutationTree.blockMutations()
608+
}
602609
mutationTree.onMutation((mutation) => {
603610
this.eventHub.emit(EventType.MUTATIONS, {
604611
...execution,

packages/node_modules/overmind/src/internalTypes.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type Options = {
2424
devtools?: string | boolean
2525
logProxies?: boolean
2626
hotReloading?: boolean
27+
strict?: boolean
2728
}
2829

2930
export type DefaultMode = {

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,45 @@ describe('Statemachine', () => {
6565
expect(overmind.state.current).toBe('BAR')
6666
})
6767

68+
test('should block mutations in strict mode', () => {
69+
type States = {
70+
current: 'FOO'
71+
foo: string
72+
} | {
73+
current: 'BAR'
74+
}
75+
76+
type Events = {
77+
type: 'TOGGLE',
78+
}
79+
80+
const state = statemachine<States, Events>({
81+
TOGGLE: (state) => state.current === 'FOO' ? 'BAR' : 'FOO'
82+
}).create({
83+
current: 'FOO',
84+
foo: 'bar'
85+
})
86+
const transition: Action = ({ state }) => {
87+
const fooState = state.matches('FOO')
88+
if (fooState) {
89+
fooState.foo = 'bar2'
90+
}
91+
}
92+
93+
const config = {
94+
state,
95+
actions: {
96+
transition
97+
}
98+
}
99+
100+
interface Action extends IAction<typeof config, void, void> {}
101+
102+
const overmind = createOvermind(config, {devtools: false, strict: true, devEnv: 'test'})
103+
104+
expect(() => overmind.actions.transition()).toThrow()
105+
})
106+
68107

69108
test('should ignore transition when no state returned', () => {
70109

packages/node_modules/overmind/src/statemachine.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PATH, VALUE } from 'proxy-state-tree'
1+
import { PATH, PROXY_TREE, VALUE } from 'proxy-state-tree'
22

33
import { deepCopy } from './utils'
44
import { IState } from '.'
@@ -66,13 +66,16 @@ export class StateMachine<State extends TState, Events extends TEvents> {
6666
return this
6767
}
6868

69+
const existingState = this.current
70+
const tree = (this[PROXY_TREE].master.mutationTree || this[PROXY_TREE])
6971
const transition = this[VALUE][TRANSITIONS][type]
7072

73+
tree.enableMutations()
7174
const result = transition(this, data)
75+
76+
this.current = result || existingState
7277

73-
if (result) {
74-
this.current = result
75-
}
78+
tree.blockMutations()
7679

7780
return this
7881
}

0 commit comments

Comments
 (0)