From dccb762a4e136a55706f3e36b2522fcf3f328a4e Mon Sep 17 00:00:00 2001 From: Erik Onarheim Date: Sun, 24 Nov 2024 21:28:06 -0600 Subject: [PATCH] fix: [#3277] tweak StateMachine types --- .../Resources/Sound/WebAudioInstance.ts | 12 +++---- src/engine/Util/StateMachine.ts | 31 +++++++++---------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/engine/Resources/Sound/WebAudioInstance.ts b/src/engine/Resources/Sound/WebAudioInstance.ts index b294cc42a..e4b236fbe 100644 --- a/src/engine/Resources/Sound/WebAudioInstance.ts +++ b/src/engine/Resources/Sound/WebAudioInstance.ts @@ -14,7 +14,7 @@ interface SoundState { * @see https://developer.mozilla.org/en-US/docs/Web/API/Web_Audio_API */ export class WebAudioInstance implements Audio { - private _instance: AudioBufferSourceNode; + private _instance!: AudioBufferSourceNode; private _audioContext: AudioContext = AudioContextFactory.create(); private _volumeNode = this._audioContext.createGain(); @@ -24,7 +24,7 @@ export class WebAudioInstance implements Audio { start: 'STOPPED', states: { PLAYING: { - onEnter: ({ data }: { from: string; data: SoundState }) => { + onEnter: ({ data }) => { // Buffer nodes are single use this._createNewBufferSource(); this._handleEnd(); @@ -47,7 +47,7 @@ export class WebAudioInstance implements Audio { this._instance.onended = null; // disconnect the wired on-end handler this._instance.disconnect(); this._instance.stop(0); - this._instance = null; + this._instance = null as any; }, transitions: ['STOPPED', 'PAUSED', 'SEEK'] }, @@ -59,7 +59,7 @@ export class WebAudioInstance implements Audio { transitions: ['*'] }, STOPPED: { - onEnter: ({ data }: { from: string; data: SoundState }) => { + onEnter: ({ data }) => { data.pausedAt = 0; data.startedAt = 0; this._playingFuture.resolve(true); @@ -67,7 +67,7 @@ export class WebAudioInstance implements Audio { transitions: ['PLAYING', 'PAUSED', 'SEEK'] }, PAUSED: { - onEnter: ({ data }: { data: SoundState }) => { + onEnter: ({ data }) => { // Playback rate will be a scale factor of how fast/slow the audio is being played // default is 1.0 // we need to invert it to get the time scale @@ -80,7 +80,7 @@ export class WebAudioInstance implements Audio { { startedAt: 0, pausedAt: 0 - } as SoundState + } satisfies SoundState ); private _createNewBufferSource() { diff --git a/src/engine/Util/StateMachine.ts b/src/engine/Util/StateMachine.ts index 6d29ac0e4..4a698a3e4 100644 --- a/src/engine/Util/StateMachine.ts +++ b/src/engine/Util/StateMachine.ts @@ -1,37 +1,37 @@ -export interface State { +export interface State { name?: string; transitions: string[]; - onEnter?: (context: { from: string; eventData?: any; data: any }) => boolean | void; + onEnter?: (context: { from: string; eventData?: any; data: TData }) => boolean | void; onState?: () => any; - onExit?: (context: { to: string; data: any }) => boolean | void; - onUpdate?: (data: any, elapsedMs: number) => any; + onExit?: (context: { to: string; data: TData }) => boolean | void; + onUpdate?: (data: TData, elapsedMs: number) => any; } -export interface StateMachineDescription { +export interface StateMachineDescription { start: string; - states: { [name: string]: State }; + states: { [name: string]: State }; } export type PossibleStates = TMachine extends StateMachineDescription ? Extract : never; -export interface StateMachineState { - data: any; +export interface StateMachineState { + data: TData; currentState: string; } export class StateMachine { - public startState: State; - private _currentState: State; - public get currentState(): State { + public startState: State; + private _currentState: State; + public get currentState(): State { return this._currentState; } - public set currentState(state: State) { + public set currentState(state: State) { this._currentState = state; } - public states = new Map(); + public states = new Map>(); public data: TData; - static create( + static create, TData>( machineDescription: TMachine, data?: TData ): StateMachine, TData> { @@ -81,7 +81,6 @@ export class StateMachine { return false; } } - // console.log(`${this.currentState.name} => ${potentialNewState.name} (${eventData})`); this.currentState = potentialNewState; if (this.currentState?.onState) { this.currentState.onState(); @@ -108,7 +107,7 @@ export class StateMachine { } restore(saveKey: string) { - const state: StateMachineState = JSON.parse(localStorage.getItem(saveKey)); + const state: StateMachineState = JSON.parse(localStorage.getItem(saveKey)); this.currentState = this.states.get(state.currentState); this.data = state.data; }