Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add: getOrThrow / deleteOrThrowを追加 #2055

Merged
merged 9 commits into from
May 13, 2024
7 changes: 3 additions & 4 deletions src/components/Sing/SequencerPhraseIndicator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { computed } from "vue";
import { useStore } from "@/store";
import { PhraseState } from "@/store/type";
import { getOrThrow } from "@/helpers/mapHelper";

const props = defineProps<{
phraseKey: string;
Expand All @@ -19,10 +20,8 @@ const classNames: Record<PhraseState, string> = {
PLAYABLE: "playable",
};
const className = computed(() => {
const phrase = store.state.phrases.get(props.phraseKey);
if (phrase == undefined) {
throw new Error("phrase is undefined.");
}
const phrase = getOrThrow(store.state.phrases, props.phraseKey);

return classNames[phrase.state];
});
</script>
Expand Down
17 changes: 17 additions & 0 deletions src/helpers/mapHelper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** Mapのヘルパー関数 */

/** Mapから値を取得する。指定したキーが存在しない場合は例外を投げる */
export const getOrThrow = <K, V>(map: Map<K, V>, key: K) => {
if (!map.has(key)) {
throw new Error(`Key not found: ${key}`);
}
return map.get(key) as V;
};

/** Mapから値を削除する。指定したキーが存在しない場合は例外を投げる */
export const deleteOrThrow = <K, V>(map: Map<K, V>, key: K) => {
if (!map.has(key)) {
throw new Error(`Key not found: ${key}`);
}
map.delete(key);
};
49 changes: 21 additions & 28 deletions src/store/singing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {
import { getWorkaroundKeyRangeAdjustment } from "@/sing/workaroundKeyRangeAdjustment";
import { createLogger } from "@/domain/frontend/log";
import { noteSchema } from "@/domain/project/schema";
import { getOrThrow } from "@/helpers/mapHelper";

const logger = createLogger("store/singing");

Expand Down Expand Up @@ -607,10 +608,8 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
phraseState,
}: { phraseKey: string; phraseState: PhraseState },
) {
const phrase = state.phrases.get(phraseKey);
if (phrase == undefined) {
throw new Error("phrase is undefined.");
}
const phrase = getOrThrow(state.phrases, phraseKey);

phrase.state = phraseState;
},
},
Expand All @@ -626,10 +625,8 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
singingGuideKey: SingingGuideSourceHash | undefined;
},
) {
const phrase = state.phrases.get(phraseKey);
if (phrase == undefined) {
throw new Error("phrase is undefined.");
}
const phrase = getOrThrow(state.phrases, phraseKey);

phrase.singingGuideKey = singingGuideKey;
},
},
Expand All @@ -645,10 +642,8 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
singingVoiceKey: SingingVoiceSourceHash | undefined;
},
) {
const phrase = state.phrases.get(phraseKey);
if (phrase == undefined) {
throw new Error("phrase is undefined.");
}
const phrase = getOrThrow(state.phrases, phraseKey);

phrase.singingVoiceKey = singingVoiceKey;
},
},
Expand Down Expand Up @@ -1200,12 +1195,11 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
phrase.singingGuideKey != undefined &&
phrase.singingVoiceKey != undefined
) {
let singingGuide = state.singingGuides.get(
let singingGuide = getOrThrow(
state.singingGuides,
phrase.singingGuideKey,
);
if (singingGuide == undefined) {
throw new Error("singingGuide is undefined.");
}

// 歌い方をコピーして、ピッチ編集を適用する
singingGuide = structuredClone(toRaw(singingGuide));
applyPitchEdit(singingGuide, pitchEditData, editFrameRate);
Expand Down Expand Up @@ -1299,10 +1293,10 @@ export const singingStore = createPartialStore<SingingStoreTypes>({

let singingGuide: SingingGuide | undefined;
if (phrase.singingGuideKey != undefined) {
singingGuide = state.singingGuides.get(phrase.singingGuideKey);
if (!singingGuide) {
throw new Error("singingGuide is undefined.");
}
singingGuide = getOrThrow(
state.singingGuides,
phrase.singingGuideKey,
);
} else {
const singingGuideSourceHash =
await calculateSingingGuideSourceHash({
Expand Down Expand Up @@ -2351,16 +2345,15 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
) {
continue;
}
const singingGuide = state.singingGuides.get(
const singingGuide = getOrThrow(
state.singingGuides,
phrase.singingGuideKey,
);
const singingVoice = singingVoices.get(phrase.singingVoiceKey);
if (!singingGuide) {
throw new Error("singingGuide is undefined");
}
if (!singingVoice) {
throw new Error("singingVoice is undefined");
}
const singingVoice = getOrThrow(
singingVoices,
phrase.singingVoiceKey,
);

// TODO: この辺りの処理を共通化する
const audioEvents = await generateAudioEvents(
offlineAudioContext,
Expand Down