Skip to content

Commit

Permalink
Refactor: 上書き防止処理を関数化し共通化 (#1478)
Browse files Browse the repository at this point in the history
  • Loading branch information
thiramisu authored Aug 5, 2023
1 parent a200d73 commit 37bd6f9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 29 deletions.
40 changes: 15 additions & 25 deletions src/store/audio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ function parseTextFile(
return audioItems;
}

async function changeFileTailToNonExistent(filePath: string) {
const EXTENSION = "wav";
let tail = 1;
const name = filePath.slice(0, filePath.length - 1 - EXTENSION.length);
while (await window.electron.checkFileExists(filePath)) {
filePath = `${name}[${tail}].${EXTENSION}`;
tail += 1;
}
return filePath;
}

export async function writeTextFile(obj: {
filePath: string;
text: string;
Expand Down Expand Up @@ -1336,12 +1347,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}

if (state.savingSetting.avoidOverwrite) {
let tail = 1;
const name = filePath.slice(0, filePath.length - 4);
while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) {
filePath = name + "[" + tail.toString() + "]" + ".wav";
tail += 1;
}
filePath = await changeFileTailToNonExistent(filePath);
}

let blob = await dispatch("GET_AUDIO_CACHE", { audioKey });
Expand Down Expand Up @@ -1489,12 +1495,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}

if (state.savingSetting.avoidOverwrite) {
let tail = 1;
const name = filePath.slice(0, filePath.length - 4);
while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) {
filePath = name + "[" + tail.toString() + "]" + ".wav";
tail += 1;
}
filePath = await changeFileTailToNonExistent(filePath);
}

const encodedBlobs: string[] = [];
Expand Down Expand Up @@ -1613,7 +1614,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
CONNECT_AND_EXPORT_TEXT: {
action: createUILockAction(
async (
{ state, dispatch, getters },
{ state, getters },
{ filePath }: { filePath?: string }
): Promise<SaveResultObject> => {
const defaultFileName = buildProjectFileName(state, "txt");
Expand All @@ -1634,12 +1635,7 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}

if (state.savingSetting.avoidOverwrite) {
let tail = 1;
const name = filePath.slice(0, filePath.length - 4);
while (await dispatch("CHECK_FILE_EXISTS", { file: filePath })) {
filePath = name + "[" + tail.toString() + "]" + ".wav";
tail += 1;
}
filePath = await changeFileTailToNonExistent(filePath);
}

const characters = new Map<string, string>();
Expand Down Expand Up @@ -1864,12 +1860,6 @@ export const audioStore = createPartialStore<AudioStoreTypes>({
}
},
},

CHECK_FILE_EXISTS: {
action(_, { file }: { file: string }) {
return window.electron.checkFileExists(file);
},
},
});

export const audioCommandStoreState: AudioCommandStoreState = {};
Expand Down
4 changes: 0 additions & 4 deletions src/store/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,6 @@ export type AudioStoreTypes = {
STOP_CONTINUOUSLY_AUDIO: {
action(): void;
};

CHECK_FILE_EXISTS: {
action(payload: { file: string }): Promise<boolean>;
};
};

/*
Expand Down

0 comments on commit 37bd6f9

Please sign in to comment.