UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

65 lines (46 loc) 2.01 kB
import { IllegalStateException } from "../fsm/exceptions/IllegalStateException.js"; import { objectKeyByValue } from "../model/object/objectKeyByValue.js"; import ObservedEnum from "../model/ObservedEnum.js"; import { ProcessState } from "./ProcessState.js"; export class BaseProcess { id = ""; /** * @readonly * @type {ObservedEnum.<ProcessState>} */ __state = new ObservedEnum(ProcessState.New, ProcessState); /** * @returns {ObservedEnum<ProcessState>} */ getState() { return this.__state; } initialize() { const state_current = this.__state.getValue(); if (state_current !== ProcessState.New && state_current !== ProcessState.Finalized) { throw new IllegalStateException(`Expected New or Finalized state, instead got ${objectKeyByValue(ProcessState, state_current)}`); } this.__state.set(ProcessState.Initialized); } finalize() { const state = this.__state; if (state.getValue() === ProcessState.Running) { this.shutdown(); } // note that we don't bind state value, because it is liable to change in "shutdown" if (state.getValue() !== ProcessState.Stopped && state.getValue() !== ProcessState.Initialized) { throw new IllegalStateException(`Expected Initialized or Stopped state, instead got ${objectKeyByValue(ProcessState, state.getValue())}`); } state.set(ProcessState.Finalized); } startup() { const state_current = this.__state.getValue(); if (state_current !== ProcessState.Initialized && state_current !== ProcessState.Stopped) { throw new IllegalStateException(`Expected Initial state, instead got ${objectKeyByValue(ProcessState, state_current)}`); } this.__state.set(ProcessState.Running); } shutdown() { this.__state.set(ProcessState.Stopped); } }