Skip to content

Commit

Permalink
Do not crash when stopping already stopped interpreter. Fixes #1697
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed Dec 9, 2020
1 parent a5e4e78 commit fe5ebd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/core/src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,11 @@ export class Interpreter<
this.doneListeners.delete(listener);
}

if (!this.initialized) {
// Interpreter already stopped; do nothing
return this;
}

this.state.configuration.forEach((stateNode) => {
for (const action of stateNode.definition.exit) {
this.exec(action, this.state);
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/interpreter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,19 @@ Event: {\\"type\\":\\"SOME_EVENT\\"}"
done();
}, 10);
});

it('stopping a not-started interpreter should not crash', () => {
const service = interpret(
createMachine({
initial: 'a',
states: { a: {} }
})
);

expect(() => {
service.stop();
}).not.toThrow();
});
});

describe('off()', () => {
Expand Down

0 comments on commit fe5ebd3

Please sign in to comment.