Skip to content

Commit

Permalink
ツール対応に関係ない変更をrevert
Browse files Browse the repository at this point in the history
  • Loading branch information
sigprogramming committed Jan 31, 2025
1 parent b01d5c3 commit 99c0bbe
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 60 deletions.
19 changes: 6 additions & 13 deletions src/composables/useSequencerStateMachine.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { computed, ref } from "vue";
import {
ComputedRefs,
IdleStateId,
PartialStore,
Refs,
} from "@/sing/sequencerStateMachine/common";
import { getNoteDuration } from "@/sing/domain";
import { createSequencerStateMachine } from "@/sing/sequencerStateMachine";

export const useSequencerStateMachine = (
store: PartialStore,
initialStateId: IdleStateId,
) => {
export const useSequencerStateMachine = (store: PartialStore) => {
const computedRefs: ComputedRefs = {
snapTicks: computed(() =>
getNoteDuration(store.state.sequencerSnapType, store.state.tpqn),
Expand All @@ -29,14 +25,11 @@ export const useSequencerStateMachine = (
previewPitchEdit: ref(undefined),
guideLineTicks: ref(0),
};
const stateMachine = createSequencerStateMachine(
{
...computedRefs,
...refs,
store,
},
initialStateId,
);
const stateMachine = createSequencerStateMachine({
...computedRefs,
...refs,
store,
});
return {
stateMachine,
nowPreviewing: computed(() => refs.nowPreviewing.value),
Expand Down
8 changes: 2 additions & 6 deletions src/sing/sequencerStateMachine/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {
Context,
IdleStateId,
Input,
SequencerStateDefinitions,
} from "@/sing/sequencerStateMachine/common";
Expand All @@ -18,10 +17,7 @@ import { SelectNotesWithRectState } from "@/sing/sequencerStateMachine/states/se
import { DrawPitchState } from "@/sing/sequencerStateMachine/states/drawPitchState";
import { ErasePitchState } from "@/sing/sequencerStateMachine/states/erasePitchState";

export const createSequencerStateMachine = (
context: Context,
initialStateId: IdleStateId,
) => {
export const createSequencerStateMachine = (context: Context) => {
return new StateMachine<SequencerStateDefinitions, Input, Context>(
{
selectNotesToolIdle: () => new SelectNotesToolIdleState(),
Expand All @@ -36,7 +32,7 @@ export const createSequencerStateMachine = (
drawPitch: (args) => new DrawPitchState(args),
erasePitch: (args) => new ErasePitchState(args),
},
new SelectNotesToolIdleState(),
context,
initialStateId,
);
};
51 changes: 10 additions & 41 deletions src/sing/stateMachine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type SetNextState<T extends StateDefinition[]> = <U extends StateId<T>>(

/**
* ステートマシンのステートを表すインターフェース。
*
* @template State このインターフェースを実装するステートの型。
* @template Input ステートが処理する入力の型。
* @template Context ステート間で共有されるコンテキストの型。
Expand All @@ -57,6 +58,7 @@ export interface State<

/**
* 入力を処理し、必要に応じて次のステートを設定する。
*
* @param payload `input`、`context`、`setNextState`関数を含むペイロード。
*/
process(payload: {
Expand All @@ -67,12 +69,14 @@ export interface State<

/**
* ステートに入ったときに呼び出される。
*
* @param context ステート間で共有されるコンテキスト。
*/
onEnter(context: Context): void;

/**
* ステートから出るときに呼び出される。
*
* @param context ステート間で共有されるコンテキスト。
*/
onExit(context: Context): void;
Expand All @@ -87,17 +91,9 @@ type StateFactories<T extends StateDefinition[], Input, Context> = {
) => State<T, Input, Context> & { readonly id: U };
};

/**
* 初期ステートとして設定可能なステートのIDを表す型。
*/
type InitialStateId<T extends StateDefinition[]> = T[number] extends infer U
? U extends { id: string; factoryArgs: undefined }
? U["id"]
: never
: never;

/**
* ステートマシンを表すクラス。
*
* @template State ステートマシンのステートの型。
* @template Input ステートが処理する入力の型。
* @template Context ステート間で共有されるコンテキストの型。
Expand All @@ -115,7 +111,6 @@ export class StateMachine<
private readonly context: Context;

private currentState: State<StateDefinitions, Input, Context>;
private isDisposed = false;

/**
* ステートマシンの現在のステートのID。
Expand All @@ -125,67 +120,41 @@ export class StateMachine<
}

/**
* @param stateFactories ステートのファクトリー関数。
* @param context ステートマシンのコンテキスト。
* @param initialStateId ステートマシンの初期ステートのID。
* @param initialState ステートマシンの初期ステート。
* @param context ステート間で共有されるコンテキスト。
*/
constructor(
stateFactories: StateFactories<StateDefinitions, Input, Context>,
initialState: State<StateDefinitions, Input, Context>,
context: Context,
initialStateId: InitialStateId<StateDefinitions>,
) {
this.stateFactories = stateFactories;
this.context = context;

this.currentState = stateFactories[initialStateId](undefined);
}
this.currentState = initialState;

/**
* ステートを設定する。
* @param id ステートのID。
* @param factoryArgs ステートのファクトリー関数の引数。
*/
setState<T extends StateId<StateDefinitions>>(
id: T,
factoryArgs: FactoryArgs<StateDefinitions, T>,
) {
if (this.isDisposed) {
throw new Error("This state machine is already disposed.");
}
this.currentState.onExit(this.context);
this.currentState = this.stateFactories[id](factoryArgs);
this.currentState.onEnter(this.context);
}

/**
* 現在のステートを使用して入力を処理し、必要に応じてステートの遷移を行う。
*
* @param input 処理する入力。
*/
process(input: Input) {
if (this.isDisposed) {
throw new Error("This state machine is already disposed.");
}

let nextState: State<StateDefinitions, Input, Context> | undefined =
undefined;

this.currentState.process({
input,
context: this.context,
setNextState: (id, factoryArgs) => {
nextState = this.stateFactories[id](factoryArgs);
},
});

if (nextState != undefined) {
this.currentState.onExit(this.context);
this.currentState = nextState;
this.currentState.onEnter(this.context);
}
}

dispose() {
this.currentState.onExit(this.context);
this.isDisposed = true;
}
}

0 comments on commit 99c0bbe

Please sign in to comment.