Skip to content

Commit 8c63515

Browse files
StateDefinitionsの型のチェックを行えるようにした
1 parent e7f9fd9 commit 8c63515

File tree

2 files changed

+99
-77
lines changed

2 files changed

+99
-77
lines changed

src/sing/stateMachine/sequencerStateMachine.ts

Lines changed: 94 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
State,
1616
SetNextState,
1717
StateMachine,
18+
StateDefinitions,
1819
} from "@/sing/stateMachine/stateMachineBase";
1920
import {
2021
getButton,
@@ -108,66 +109,68 @@ type PartialStore = {
108109

109110
type Context = ComputedRefs & Refs & { readonly store: PartialStore };
110111

111-
type StateDefinitions = [
112-
{
113-
id: "idle";
114-
factoryArgs: undefined;
115-
},
116-
{
117-
id: "addNote";
118-
factoryArgs: {
119-
cursorPosAtStart: PositionOnSequencer;
120-
targetTrackId: TrackId;
121-
};
122-
},
123-
{
124-
id: "moveNote";
125-
factoryArgs: {
126-
cursorPosAtStart: PositionOnSequencer;
127-
targetTrackId: TrackId;
128-
targetNoteIds: Set<NoteId>;
129-
mouseDownNoteId: NoteId;
130-
};
131-
},
132-
{
133-
id: "resizeNoteLeft";
134-
factoryArgs: {
135-
cursorPosAtStart: PositionOnSequencer;
136-
targetTrackId: TrackId;
137-
targetNoteIds: Set<NoteId>;
138-
mouseDownNoteId: NoteId;
139-
};
140-
},
141-
{
142-
id: "resizeNoteRight";
143-
factoryArgs: {
144-
cursorPosAtStart: PositionOnSequencer;
145-
targetTrackId: TrackId;
146-
targetNoteIds: Set<NoteId>;
147-
mouseDownNoteId: NoteId;
148-
};
149-
},
150-
{
151-
id: "selectNotesWithRect";
152-
factoryArgs: {
153-
cursorPosAtStart: PositionOnSequencer;
154-
};
155-
},
156-
{
157-
id: "drawPitch";
158-
factoryArgs: {
159-
cursorPosAtStart: PositionOnSequencer;
160-
targetTrackId: TrackId;
161-
};
162-
},
163-
{
164-
id: "erasePitch";
165-
factoryArgs: {
166-
cursorPosAtStart: PositionOnSequencer;
167-
targetTrackId: TrackId;
168-
};
169-
},
170-
];
112+
type SequencerStateDefinitions = StateDefinitions<
113+
[
114+
{
115+
id: "idle";
116+
factoryArgs: undefined;
117+
},
118+
{
119+
id: "addNote";
120+
factoryArgs: {
121+
cursorPosAtStart: PositionOnSequencer;
122+
targetTrackId: TrackId;
123+
};
124+
},
125+
{
126+
id: "moveNote";
127+
factoryArgs: {
128+
cursorPosAtStart: PositionOnSequencer;
129+
targetTrackId: TrackId;
130+
targetNoteIds: Set<NoteId>;
131+
mouseDownNoteId: NoteId;
132+
};
133+
},
134+
{
135+
id: "resizeNoteLeft";
136+
factoryArgs: {
137+
cursorPosAtStart: PositionOnSequencer;
138+
targetTrackId: TrackId;
139+
targetNoteIds: Set<NoteId>;
140+
mouseDownNoteId: NoteId;
141+
};
142+
},
143+
{
144+
id: "resizeNoteRight";
145+
factoryArgs: {
146+
cursorPosAtStart: PositionOnSequencer;
147+
targetTrackId: TrackId;
148+
targetNoteIds: Set<NoteId>;
149+
mouseDownNoteId: NoteId;
150+
};
151+
},
152+
{
153+
id: "selectNotesWithRect";
154+
factoryArgs: {
155+
cursorPosAtStart: PositionOnSequencer;
156+
};
157+
},
158+
{
159+
id: "drawPitch";
160+
factoryArgs: {
161+
cursorPosAtStart: PositionOnSequencer;
162+
targetTrackId: TrackId;
163+
};
164+
},
165+
{
166+
id: "erasePitch";
167+
factoryArgs: {
168+
cursorPosAtStart: PositionOnSequencer;
169+
targetTrackId: TrackId;
170+
};
171+
},
172+
]
173+
>;
171174

172175
const getGuideLineTicks = (
173176
cursorPos: PositionOnSequencer,
@@ -235,7 +238,7 @@ const executeNotesSelectionProcess = (
235238
}
236239
};
237240

238-
class IdleState implements State<StateDefinitions, Input, Context> {
241+
class IdleState implements State<SequencerStateDefinitions, Input, Context> {
239242
readonly id = "idle";
240243

241244
onEnter() {}
@@ -247,7 +250,7 @@ class IdleState implements State<StateDefinitions, Input, Context> {
247250
}: {
248251
input: Input;
249252
context: Context;
250-
setNextState: SetNextState<StateDefinitions>;
253+
setNextState: SetNextState<SequencerStateDefinitions>;
251254
}) {
252255
const mouseButton = getButton(input.mouseEvent);
253256
const selectedTrackId = context.selectedTrackId.value;
@@ -328,7 +331,7 @@ class IdleState implements State<StateDefinitions, Input, Context> {
328331
onExit() {}
329332
}
330333

331-
class AddNoteState implements State<StateDefinitions, Input, Context> {
334+
class AddNoteState implements State<SequencerStateDefinitions, Input, Context> {
332335
readonly id = "addNote";
333336

334337
private readonly cursorPosAtStart: PositionOnSequencer;
@@ -418,7 +421,7 @@ class AddNoteState implements State<StateDefinitions, Input, Context> {
418421
}: {
419422
input: Input;
420423
context: Context;
421-
setNextState: SetNextState<StateDefinitions>;
424+
setNextState: SetNextState<SequencerStateDefinitions>;
422425
}) {
423426
if (this.innerContext == undefined) {
424427
throw new Error("innerContext is undefined.");
@@ -462,7 +465,9 @@ class AddNoteState implements State<StateDefinitions, Input, Context> {
462465
}
463466
}
464467

465-
class MoveNoteState implements State<StateDefinitions, Input, Context> {
468+
class MoveNoteState
469+
implements State<SequencerStateDefinitions, Input, Context>
470+
{
466471
readonly id = "moveNote";
467472

468473
private readonly cursorPosAtStart: PositionOnSequencer;
@@ -582,7 +587,7 @@ class MoveNoteState implements State<StateDefinitions, Input, Context> {
582587
}: {
583588
input: Input;
584589
context: Context;
585-
setNextState: SetNextState<StateDefinitions>;
590+
setNextState: SetNextState<SequencerStateDefinitions>;
586591
}) {
587592
if (this.innerContext == undefined) {
588593
throw new Error("innerContext is undefined.");
@@ -628,7 +633,9 @@ class MoveNoteState implements State<StateDefinitions, Input, Context> {
628633
}
629634
}
630635

631-
class ResizeNoteLeftState implements State<StateDefinitions, Input, Context> {
636+
class ResizeNoteLeftState
637+
implements State<SequencerStateDefinitions, Input, Context>
638+
{
632639
readonly id = "resizeNoteLeft";
633640

634641
private readonly cursorPosAtStart: PositionOnSequencer;
@@ -743,7 +750,7 @@ class ResizeNoteLeftState implements State<StateDefinitions, Input, Context> {
743750
}: {
744751
input: Input;
745752
context: Context;
746-
setNextState: SetNextState<StateDefinitions>;
753+
setNextState: SetNextState<SequencerStateDefinitions>;
747754
}) {
748755
if (this.innerContext == undefined) {
749756
throw new Error("innerContext is undefined.");
@@ -788,7 +795,9 @@ class ResizeNoteLeftState implements State<StateDefinitions, Input, Context> {
788795
}
789796
}
790797

791-
class ResizeNoteRightState implements State<StateDefinitions, Input, Context> {
798+
class ResizeNoteRightState
799+
implements State<SequencerStateDefinitions, Input, Context>
800+
{
792801
readonly id = "resizeNoteRight";
793802

794803
private readonly cursorPosAtStart: PositionOnSequencer;
@@ -902,7 +911,7 @@ class ResizeNoteRightState implements State<StateDefinitions, Input, Context> {
902911
}: {
903912
input: Input;
904913
context: Context;
905-
setNextState: SetNextState<StateDefinitions>;
914+
setNextState: SetNextState<SequencerStateDefinitions>;
906915
}) {
907916
if (this.innerContext == undefined) {
908917
throw new Error("innerContext is undefined.");
@@ -950,7 +959,7 @@ class ResizeNoteRightState implements State<StateDefinitions, Input, Context> {
950959
}
951960

952961
class SelectNotesWithRectState
953-
implements State<StateDefinitions, Input, Context>
962+
implements State<SequencerStateDefinitions, Input, Context>
954963
{
955964
readonly id = "selectNotesWithRect";
956965

@@ -991,7 +1000,7 @@ class SelectNotesWithRectState
9911000
}: {
9921001
input: Input;
9931002
context: Context;
994-
setNextState: SetNextState<StateDefinitions>;
1003+
setNextState: SetNextState<SequencerStateDefinitions>;
9951004
}) {
9961005
const mouseButton = getButton(input.mouseEvent);
9971006
if (input.targetArea === "SequencerBody") {
@@ -1046,7 +1055,9 @@ class SelectNotesWithRectState
10461055
}
10471056
}
10481057

1049-
class DrawPitchState implements State<StateDefinitions, Input, Context> {
1058+
class DrawPitchState
1059+
implements State<SequencerStateDefinitions, Input, Context>
1060+
{
10501061
readonly id = "drawPitch";
10511062

10521063
private readonly cursorPosAtStart: PositionOnSequencer;
@@ -1177,7 +1188,7 @@ class DrawPitchState implements State<StateDefinitions, Input, Context> {
11771188
}: {
11781189
input: Input;
11791190
context: Context;
1180-
setNextState: SetNextState<StateDefinitions>;
1191+
setNextState: SetNextState<SequencerStateDefinitions>;
11811192
}) {
11821193
if (this.innerContext == undefined) {
11831194
throw new Error("innerContext is undefined.");
@@ -1229,7 +1240,9 @@ class DrawPitchState implements State<StateDefinitions, Input, Context> {
12291240
}
12301241
}
12311242

1232-
class ErasePitchState implements State<StateDefinitions, Input, Context> {
1243+
class ErasePitchState
1244+
implements State<SequencerStateDefinitions, Input, Context>
1245+
{
12331246
readonly id = "erasePitch";
12341247

12351248
private readonly cursorPosAtStart: PositionOnSequencer;
@@ -1313,7 +1326,7 @@ class ErasePitchState implements State<StateDefinitions, Input, Context> {
13131326
}: {
13141327
input: Input;
13151328
context: Context;
1316-
setNextState: SetNextState<StateDefinitions>;
1329+
setNextState: SetNextState<SequencerStateDefinitions>;
13171330
}) {
13181331
if (this.innerContext == undefined) {
13191332
throw new Error("innerContext is undefined.");
@@ -1373,7 +1386,11 @@ export const useSequencerStateMachine = (store: PartialStore) => {
13731386
previewPitchEdit: ref(undefined),
13741387
guideLineTicks: ref(0),
13751388
};
1376-
const stateMachine = new StateMachine<StateDefinitions, Input, Context>(
1389+
const stateMachine = new StateMachine<
1390+
SequencerStateDefinitions,
1391+
Input,
1392+
Context
1393+
>(
13771394
{
13781395
idle: () => new IdleState(),
13791396
addNote: (args) => new AddNoteState(args),

src/sing/stateMachine/stateMachineBase.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ type StateDefinition = {
1616
factoryArgs: Record<string, unknown> | undefined;
1717
};
1818

19+
/**
20+
* ステートの定義のリストを表す型。
21+
*/
22+
export type StateDefinitions<T extends StateDefinition[]> = T;
23+
1924
/**
2025
* ステートのIDを表す型。
2126
*/

0 commit comments

Comments
 (0)