Skip to content

Commit

Permalink
リファクタリング:コマンドのunixMillisecをUUID4にする (#2174)
Browse files Browse the repository at this point in the history
* stash

* Fix: コマンドのunixMillisecをUUID4にする

* ミス

* ミス
  • Loading branch information
Hiroshiba authored Jul 23, 2024
1 parent 085bbc0 commit b8a6b03
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 43 deletions.
33 changes: 10 additions & 23 deletions src/store/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
MutationsBase,
MutationTree,
} from "@/store/vuex";
import { EditorType } from "@/type/preload";
import { CommandId, EditorType } from "@/type/preload";

enablePatches();
enableMapSet();
Expand Down Expand Up @@ -67,7 +67,7 @@ const recordPatches =
(draft: S) => recipe(draft, payload),
);
return {
unixMillisec: new Date().getTime(),
id: CommandId(crypto.randomUUID()),
redoPatches: doPatches,
undoPatches: undoPatches,
};
Expand Down Expand Up @@ -133,30 +133,17 @@ export const commandStore = createPartialStore<CommandStoreTypes>({
},
},

LAST_COMMAND_UNIX_MILLISEC: {
LAST_COMMAND_IDS: {
getter(state) {
const getLastCommandUnixMillisec = (
commands: Command[],
): number | null => {
const lastCommand = commands[commands.length - 1];
// 型的にはundefinedにはならないが、lengthが0の場合はundefinedになる
return lastCommand ? lastCommand.unixMillisec : null;
const getLastCommandId = (commands: Command[]): CommandId | null => {
if (commands.length == 0) return null;
else return commands[commands.length - 1].id;
};

const lastTalkCommandTime = getLastCommandUnixMillisec(
state.undoCommands["talk"],
);
const lastSongCommandTime = getLastCommandUnixMillisec(
state.undoCommands["song"],
);

if (lastTalkCommandTime != null && lastSongCommandTime != null) {
return Math.max(lastTalkCommandTime, lastSongCommandTime);
} else if (lastTalkCommandTime != null) {
return lastTalkCommandTime;
} else {
return lastSongCommandTime;
}
return {
talk: getLastCommandId(state.undoCommands["talk"]),
song: getLastCommandId(state.undoCommands["song"]),
};
},
},

Expand Down
39 changes: 27 additions & 12 deletions src/store/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ import {
createDefaultTimeSignature,
DEFAULT_TPQN,
} from "@/sing/domain";
import { EditorType } from "@/type/preload";
import { IsEqual } from "@/type/utility";

export const projectStoreState: ProjectStoreState = {
savedLastCommandUnixMillisec: null,
savedLastCommandIds: { talk: null, song: null },
};

const applyTalkProjectToStore = async (
Expand Down Expand Up @@ -132,7 +134,7 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
await context.dispatch("CLEAR_PITCH_EDIT_DATA");

context.commit("SET_PROJECT_FILEPATH", { filePath: undefined });
context.commit("SET_SAVED_LAST_COMMAND_UNIX_MILLISEC", null);
context.commit("RESET_SAVED_LAST_COMMAND_IDS");
context.commit("CLEAR_COMMANDS");
},
),
Expand Down Expand Up @@ -211,7 +213,7 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
await applySongProjectToStore(dispatch, parsedProjectData.song);

commit("SET_PROJECT_FILEPATH", { filePath });
commit("SET_SAVED_LAST_COMMAND_UNIX_MILLISEC", null);
commit("RESET_SAVED_LAST_COMMAND_IDS");
commit("CLEAR_COMMANDS");
return true;
} catch (err) {
Expand Down Expand Up @@ -314,8 +316,8 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({
.then(getValueOrThrow);
context.commit("SET_PROJECT_FILEPATH", { filePath });
context.commit(
"SET_SAVED_LAST_COMMAND_UNIX_MILLISEC",
context.getters.LAST_COMMAND_UNIX_MILLISEC,
"SET_SAVED_LAST_COMMAND_IDS",
context.getters.LAST_COMMAND_IDS,
);
return true;
} catch (err) {
Expand Down Expand Up @@ -372,16 +374,29 @@ export const projectStore = createPartialStore<ProjectStoreTypes>({

IS_EDITED: {
getter(state, getters) {
return (
getters.LAST_COMMAND_UNIX_MILLISEC !==
state.savedLastCommandUnixMillisec
);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: IsEqual<
typeof state.savedLastCommandIds,
typeof getters.LAST_COMMAND_IDS
> = true;
return Object.keys(state.savedLastCommandIds).some((_editor) => {
const editor = _editor as EditorType;
return (
state.savedLastCommandIds[editor] !== getters.LAST_COMMAND_IDS[editor]
);
});
},
},

SET_SAVED_LAST_COMMAND_IDS: {
mutation(state, commandIds) {
state.savedLastCommandIds = commandIds;
},
},

SET_SAVED_LAST_COMMAND_UNIX_MILLISEC: {
mutation(state, unixMillisec) {
state.savedLastCommandUnixMillisec = unixMillisec;
RESET_SAVED_LAST_COMMAND_IDS: {
mutation(state) {
state.savedLastCommandIds = { talk: null, song: null };
},
},
});
4 changes: 2 additions & 2 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1708,7 +1708,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
await dispatch("SET_TIME_SIGNATURES", { timeSignatures });
await dispatch("SET_NOTES", { notes });

commit("SET_SAVED_LAST_COMMAND_UNIX_MILLISEC", null);
commit("RESET_SAVED_LAST_COMMAND_IDS");
commit("CLEAR_COMMANDS");
dispatch("RENDER");
},
Expand Down Expand Up @@ -1751,7 +1751,7 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
startFrame: 0,
});

commit("SET_SAVED_LAST_COMMAND_UNIX_MILLISEC", null);
commit("RESET_SAVED_LAST_COMMAND_IDS");
commit("CLEAR_COMMANDS");
dispatch("RENDER");
},
Expand Down
17 changes: 11 additions & 6 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import {
RootMiscSettingType,
EditorType,
NoteId,
CommandId,
} from "@/type/preload";
import { IEngineConnectorFactory } from "@/infrastructures/EngineConnector";
import {
Expand Down Expand Up @@ -94,7 +95,7 @@ export type FetchAudioResult = {
};

export type Command = {
unixMillisec: number;
id: CommandId;
undoPatches: Patch[];
redoPatches: Patch[];
};
Expand Down Expand Up @@ -1233,8 +1234,8 @@ export type CommandStoreTypes = {
action(payload: { editor: EditorType }): void;
};

LAST_COMMAND_UNIX_MILLISEC: {
getter: number | null;
LAST_COMMAND_IDS: {
getter: Record<EditorType, CommandId | null>;
};

CLEAR_COMMANDS: {
Expand Down Expand Up @@ -1469,7 +1470,7 @@ export type IndexStoreTypes = {

export type ProjectStoreState = {
projectFilePath?: string;
savedLastCommandUnixMillisec: number | null;
savedLastCommandIds: Record<EditorType, CommandId | null>;
};

export type ProjectStoreTypes = {
Expand Down Expand Up @@ -1511,8 +1512,12 @@ export type ProjectStoreTypes = {
getter: boolean;
};

SET_SAVED_LAST_COMMAND_UNIX_MILLISEC: {
mutation: number | null;
SET_SAVED_LAST_COMMAND_IDS: {
mutation: Record<EditorType, CommandId | null>;
};

RESET_SAVED_LAST_COMMAND_IDS: {
mutation: void;
};
};

Expand Down
4 changes: 4 additions & 0 deletions src/type/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export const noteIdSchema = z.string().brand<"NoteId">();
export type NoteId = z.infer<typeof noteIdSchema>;
export const NoteId = (id: string): NoteId => noteIdSchema.parse(id);

export const commandIdSchema = z.string().brand<"CommandId">();
export type CommandId = z.infer<typeof commandIdSchema>;
export const CommandId = (id: string): CommandId => commandIdSchema.parse(id);

// 共通のアクション名
export const actionPostfixSelectNthCharacter = "番目のキャラクターを選択";

Expand Down

0 comments on commit b8a6b03

Please sign in to comment.