Skip to content

Commit

Permalink
feat(state-machine): add destroy method for state cleanup
Browse files Browse the repository at this point in the history
 - when called, it exits any current state and cleans up.
  • Loading branch information
justindujardin committed Feb 9, 2024
1 parent 1653ad8 commit b0a262f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/app/core/state-machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface IResumeCallback {
/** A state change description */
export interface IStateChange<T extends string> {
from: State<T> | null;
to: State<T>;
to: State<T> | null;
}

// Implementation
Expand All @@ -33,6 +33,19 @@ export class StateMachine<StateNames extends string> {
private _previousState: State<StateNames> | null = null;
private _pendingStates: [State<StateNames>, (result: boolean) => void][] = [];

/** Destroy the state machine, and exit any current state. */
async destroy() {
const state = this._currentState;
this._currentState = null;
this._pendingStates = [];
this._previousState = null;
this._transitioning = false;
if (state) {
this.onExitState$.emit({ from: state, to: null });
await state.exit(this);
}
}

addState(state: State<StateNames>): void {
this.states.push(state);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export class CombatGuardBehavior extends CombatActionBehavior {
'defeat',
'escape',
];
if (exitStates.includes(to.name)) {
if (to && exitStates.includes(to.name)) {
const model: CombatantTypes = this.from?.model as CombatantTypes;
assertTrue(model, 'invalid guard behavior model');
this.store.dispatch(
Expand Down

0 comments on commit b0a262f

Please sign in to comment.