-
Notifications
You must be signed in to change notification settings - Fork 304
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
Changes from 4 commits
c54a39d
4434723
0b92e68
02f77cd
61f194d
eb21c3b
f23d548
33c97fa
35764fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Mapの拡張。 | ||
// | ||
// - Mapを継承したStrictMapはimmerにより通常のMapになってしまう。 | ||
// - implements Map<K, V>のカスタムクラスはstructuredCloneにより{ map: Map<K, V> }になってしまう。 | ||
// そのため、Mapを直接拡張する。 | ||
|
||
const addProperty = (key: string, value: (...args: never[]) => unknown) => { | ||
// @ts-expect-error 意図的にPrototype汚染をしている。 | ||
Map.prototype[key] = value; | ||
}; | ||
|
||
addProperty( | ||
"getOrThrow", | ||
function (this: Map<unknown, unknown>, key: unknown): unknown { | ||
if (!this.has(key)) { | ||
throw new Error(`Key not found: ${key}`); | ||
} | ||
return this.get(key); | ||
}, | ||
); | ||
|
||
addProperty( | ||
"deleteOrThrow", | ||
function (this: Map<unknown, unknown>, key: unknown): void { | ||
if (!this.has(key)) { | ||
throw new Error(`Key not found: ${key}`); | ||
} | ||
this.delete(key); | ||
}, | ||
); | ||
|
||
declare global { | ||
interface Map<K, V> { | ||
getOrThrow(key: K): V; | ||
deleteOrThrow(key: K): void; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. これが独自のものだと気づきやすいように、 |
||
|
||
export {}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -607,10 +607,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 = state.phrases.getOrThrow(phraseKey); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 念のために将来revertするかもと考えて、このコミットでの変更は最小限にしておくのはどうでしょう・・・? |
||
phrase.state = phraseState; | ||
}, | ||
}, | ||
|
@@ -626,10 +624,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 = state.phrases.getOrThrow(phraseKey); | ||
|
||
phrase.singingGuideKey = singingGuideKey; | ||
}, | ||
}, | ||
|
@@ -645,10 +641,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 = state.phrases.getOrThrow(phraseKey); | ||
|
||
phrase.singingVoiceKey = singingVoiceKey; | ||
}, | ||
}, | ||
|
@@ -1200,12 +1194,10 @@ export const singingStore = createPartialStore<SingingStoreTypes>({ | |
phrase.singingGuideKey != undefined && | ||
phrase.singingVoiceKey != undefined | ||
) { | ||
let singingGuide = state.singingGuides.get( | ||
let singingGuide = state.singingGuides.getOrThrow( | ||
phrase.singingGuideKey, | ||
); | ||
if (singingGuide == undefined) { | ||
throw new Error("singingGuide is undefined."); | ||
} | ||
|
||
// 歌い方をコピーして、ピッチ編集を適用する | ||
singingGuide = structuredClone(toRaw(singingGuide)); | ||
applyPitchEdit(singingGuide, pitchEditData, editFrameRate); | ||
|
@@ -1299,10 +1291,9 @@ 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 = state.singingGuides.getOrThrow( | ||
phrase.singingGuideKey, | ||
); | ||
} else { | ||
const singingGuideSourceHash = | ||
await calculateSingingGuideSourceHash({ | ||
|
@@ -2353,16 +2344,13 @@ export const singingStore = createPartialStore<SingingStoreTypes>({ | |
) { | ||
continue; | ||
} | ||
const singingGuide = state.singingGuides.get( | ||
const singingGuide = state.singingGuides.getOrThrow( | ||
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 = singingVoices.getOrThrow( | ||
phrase.singingVoiceKey, | ||
); | ||
|
||
// TODO: この辺りの処理を共通化する | ||
const audioEvents = await generateAudioEvents( | ||
offlineAudioContext, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ちょっと詳しくないのですが、このファイルをimportしたら見えるようにする事って可能だったりしますか・・・?
というのも現状でelectronのメインプロセスから使えないのに見えるようになってて問題だな~~と感じたためです。
(その場合は
addProperty
が何度も実行されることも考えないといけないかも?)