Skip to content

Commit

Permalink
feat(overmind): strict mode for statemachines
Browse files Browse the repository at this point in the history
  • Loading branch information
christianalfoni committed Jan 23, 2021
1 parent d9910bc commit f217ac7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
9 changes: 8 additions & 1 deletion packages/node_modules/overmind/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ export class Overmind<ThisConfig extends IConfiguration>
private mode: DefaultMode | TestMode | SSRMode
private reydrateMutationsForHotReloading: IMutation[] = []
private originalConfiguration
private isStrict = false
initialized: Promise<any>
eventHub: EventEmitter<Events>
devtools: Devtools
Expand All @@ -210,6 +211,7 @@ export class Overmind<ThisConfig extends IConfiguration>
const devEnv = options.devEnv || 'development'

this.delimiter = options.delimiter || '.'
this.isStrict = Boolean(options.strict)

if (
(!process.env.NODE_ENV || process.env.NODE_ENV === devEnv) &&
Expand Down Expand Up @@ -576,6 +578,9 @@ export class Overmind<ThisConfig extends IConfiguration>
})
} else {
const mutationTree = execution.getMutationTree()
if (this.isStrict) {
mutationTree.blockMutations()
}
const returnValue = action(
this.createContext(execution, mutationTree),
value
Expand All @@ -598,7 +603,9 @@ export class Overmind<ThisConfig extends IConfiguration>
this.eventHub.emit(EventType.OPERATOR_START, execution)

const mutationTree = execution.getMutationTree()

if (this.isStrict) {
mutationTree.blockMutations()
}
mutationTree.onMutation((mutation) => {
this.eventHub.emit(EventType.MUTATIONS, {
...execution,
Expand Down
1 change: 1 addition & 0 deletions packages/node_modules/overmind/src/internalTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type Options = {
devtools?: string | boolean
logProxies?: boolean
hotReloading?: boolean
strict?: boolean
}

export type DefaultMode = {
Expand Down
39 changes: 39 additions & 0 deletions packages/node_modules/overmind/src/statemachine.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,45 @@ describe('Statemachine', () => {
expect(overmind.state.current).toBe('BAR')
})

test('should block mutations in strict mode', () => {
type States = {
current: 'FOO'
foo: string
} | {
current: 'BAR'
}

type Events = {
type: 'TOGGLE',
}

const state = statemachine<States, Events>({
TOGGLE: (state) => state.current === 'FOO' ? 'BAR' : 'FOO'
}).create({
current: 'FOO',
foo: 'bar'
})
const transition: Action = ({ state }) => {
const fooState = state.matches('FOO')
if (fooState) {
fooState.foo = 'bar2'
}
}

const config = {
state,
actions: {
transition
}
}

interface Action extends IAction<typeof config, void, void> {}

const overmind = createOvermind(config, {devtools: false, strict: true, devEnv: 'test'})

expect(() => overmind.actions.transition()).toThrow()
})


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

Expand Down
11 changes: 7 additions & 4 deletions packages/node_modules/overmind/src/statemachine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PATH, VALUE } from 'proxy-state-tree'
import { PATH, PROXY_TREE, VALUE } from 'proxy-state-tree'

import { deepCopy } from './utils'
import { IState } from '.'
Expand Down Expand Up @@ -66,13 +66,16 @@ export class StateMachine<State extends TState, Events extends TEvents> {
return this
}

const existingState = this.current
const tree = (this[PROXY_TREE].master.mutationTree || this[PROXY_TREE])
const transition = this[VALUE][TRANSITIONS][type]

tree.enableMutations()
const result = transition(this, data)

this.current = result || existingState

if (result) {
this.current = result
}
tree.blockMutations()

return this
}
Expand Down

0 comments on commit f217ac7

Please sign in to comment.