Skip to content

Commit

Permalink
fix: [#3277] tweak StateMachine types
Browse files Browse the repository at this point in the history
  • Loading branch information
eonarheim committed Nov 25, 2024
1 parent b06f3ff commit dccb762
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 22 deletions.
12 changes: 6 additions & 6 deletions src/engine/Resources/Sound/WebAudioInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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();
Expand All @@ -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']
},
Expand All @@ -59,15 +59,15 @@ 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);
},
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
Expand All @@ -80,7 +80,7 @@ export class WebAudioInstance implements Audio {
{
startedAt: 0,
pausedAt: 0
} as SoundState
} satisfies SoundState
);

private _createNewBufferSource() {
Expand Down
31 changes: 15 additions & 16 deletions src/engine/Util/StateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,37 @@
export interface State {
export interface State<TData> {
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<TData = any> {
start: string;
states: { [name: string]: State };
states: { [name: string]: State<TData> };
}

export type PossibleStates<TMachine> = TMachine extends StateMachineDescription ? Extract<keyof TMachine['states'], string> : never;

export interface StateMachineState {
data: any;
export interface StateMachineState<TData> {
data: TData;
currentState: string;
}

export class StateMachine<TPossibleStates extends string, TData> {
public startState: State;
private _currentState: State;
public get currentState(): State {
public startState: State<TData>;
private _currentState: State<TData>;
public get currentState(): State<TData> {
return this._currentState;
}
public set currentState(state: State) {
public set currentState(state: State<TData>) {
this._currentState = state;
}
public states = new Map<string, State>();
public states = new Map<string, State<TData>>();
public data: TData;

static create<TMachine extends StateMachineDescription, TData>(
static create<TMachine extends StateMachineDescription<TData>, TData>(
machineDescription: TMachine,
data?: TData
): StateMachine<PossibleStates<TMachine>, TData> {
Expand Down Expand Up @@ -81,7 +81,6 @@ export class StateMachine<TPossibleStates extends string, TData> {
return false;
}
}
// console.log(`${this.currentState.name} => ${potentialNewState.name} (${eventData})`);
this.currentState = potentialNewState;
if (this.currentState?.onState) {
this.currentState.onState();
Expand All @@ -108,7 +107,7 @@ export class StateMachine<TPossibleStates extends string, TData> {
}

restore(saveKey: string) {
const state: StateMachineState = JSON.parse(localStorage.getItem(saveKey));
const state: StateMachineState<TData> = JSON.parse(localStorage.getItem(saveKey));
this.currentState = this.states.get(state.currentState);
this.data = state.data;
}
Expand Down

0 comments on commit dccb762

Please sign in to comment.