2 changed files with 46 additions and 3 deletions
@ -1,13 +1,30 @@ |
|||||
export type SceneState = "idle" | "loading"; |
export const SCENE_STATES = { |
||||
|
IDLE: "idle", |
||||
|
LOADING: "loading", |
||||
|
} as const; |
||||
|
|
||||
|
export type SceneState = (typeof SCENE_STATES)[keyof typeof SCENE_STATES]; |
||||
|
|
||||
|
const SCENE_STATE_TRANSITIONS: Readonly<Record<SceneState, readonly SceneState[]>> = { |
||||
|
[SCENE_STATES.IDLE]: [SCENE_STATES.LOADING], |
||||
|
[SCENE_STATES.LOADING]: [SCENE_STATES.IDLE], |
||||
|
}; |
||||
|
|
||||
export class SceneStateMachine { |
export class SceneStateMachine { |
||||
private state: SceneState = "idle"; |
private state: SceneState = SCENE_STATES.IDLE; |
||||
|
|
||||
getState(): SceneState { |
getState(): SceneState { |
||||
return this.state; |
return this.state; |
||||
} |
} |
||||
|
|
||||
transitionTo(nextState: SceneState): void { |
transitionTo(nextState: SceneState): void { |
||||
|
const allowedNextStates = SCENE_STATE_TRANSITIONS[this.state]; |
||||
|
if (!allowedNextStates.includes(nextState)) { |
||||
|
throw new Error( |
||||
|
`Invalid scene transition: "${this.state}" -> "${nextState}"` |
||||
|
); |
||||
|
} |
||||
|
|
||||
this.state = nextState; |
this.state = nextState; |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue