Skip to content

Commit

Permalink
Add: getOrThrow / deleteOrThrowを追加 (#2055)
Browse files Browse the repository at this point in the history
* Add: StrictMapを追加

* Change: phrasesをmustGetベースに

* Change: 型定義の場所を変える

* Change: mustGet -> getOrThrow

* Change: prototype汚染をやめる

* Apply suggestions from code review

---------

Co-authored-by: Hiroshiba <hihokaruta@gmail.com>
  • Loading branch information
sevenc-nanashi and Hiroshiba committed May 13, 2024
1 parent a406005 commit 595d108
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
7 changes: 3 additions & 4 deletions src/components/Sing/SequencerPhraseIndicator.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<script setup lang="ts">
import { computed } from "vue";
import { useStore } from "@/store";
import { getOrThrow } from "@/helpers/mapHelper";
import { PhraseSourceHash, PhraseState } from "@/store/type";
const props = defineProps<{
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 @@ -88,6 +88,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 @@ -632,10 +633,8 @@ export const singingStore = createPartialStore<SingingStoreTypes>({
phraseState,
}: { phraseKey: PhraseSourceHash; 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 @@ -651,10 +650,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 @@ -670,10 +667,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 @@ -1359,12 +1354,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 @@ -1471,10 +1465,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 @@ -2479,16 +2473,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

0 comments on commit 595d108

Please sign in to comment.