Skip to content

Commit 8a50bfa

Browse files
fix(overmind): properly dispose machines
1 parent e758380 commit 8a50bfa

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,8 @@ describe('Statemachine', () => {
456456

457457
expect(() => overmind.actions.transition()).not.toThrow()
458458
})
459-
test('should dispose nested machines', async () => {
459+
test('should dispose nested and base machines', async () => {
460+
expect.assertions(3)
460461
type States = {
461462
current: 'FOO'
462463
} | {
@@ -470,13 +471,28 @@ describe('Statemachine', () => {
470471
current: 'BAR'
471472
}
472473

473-
const nested = statemachine<States>({
474+
type BaseStates = {
475+
current: 'FOO'
476+
} | {
477+
current: 'BAR',
478+
}
479+
480+
const base = statemachine<BaseStates>({
474481
FOO: ['BAR'],
475482
BAR: ['FOO']
476483
}, {
477484
current: 'FOO'
478485
})
479486

487+
const nested = statemachine<States, { base: typeof base }>({
488+
FOO: ['BAR'],
489+
BAR: ['FOO']
490+
}, {
491+
current: 'FOO'
492+
}, {
493+
base
494+
})
495+
480496
const state = statemachine<ParentStates>({
481497
FOO: ['BAR'],
482498
BAR: ['FOO']
@@ -488,8 +504,14 @@ describe('Statemachine', () => {
488504
state.matches('FOO', (fooState) => {
489505
const ref = fooState.nested
490506
state.transition('BAR', {})
507+
491508
expect(fooState.nested).toBe(undefined)
509+
ref.transition('BAR', {})
510+
ref.base.transition('BAR', {})
511+
ref.base
512+
492513
expect(ref.current).toBe('FOO')
514+
expect(ref.base.current).toBe('FOO')
493515
})
494516
}
495517

packages/node_modules/overmind/src/statemachine.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export class StateMachine<Base extends IState, States extends TStates, State ext
6565
return new StateMachine(this[TRANSITIONS], deepCopy(this[STATE]), deepCopy(this[BASE]))
6666
}
6767
private dispose() {
68-
this
68+
Object.keys(this[VALUE]).forEach((key) => {
69+
if (this[VALUE][key] instanceof StateMachine) {
70+
this[key].dispose()
71+
}
72+
})
73+
this[VALUE][IS_DISPOSED] = true
6974
}
7075
constructor(transitions: StatemachineTransitions<States>, state: States, base: Base) {
7176
this[STATE] = state

0 commit comments

Comments
 (0)