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

vuexのstoreの呼び出しをリテラル引数からDot記法へ #2099

Merged
merged 11 commits into from
May 28, 2024
146 changes: 79 additions & 67 deletions src/store/dictionary.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createPartialStore } from "./vuex";
import { createDotNotationPartialStore as createPartialStore } from "./vuex";
import { UserDictWord, UserDictWordToJSON } from "@/openapi";
import { DictionaryStoreState, DictionaryStoreTypes } from "@/store/type";
import { EngineId } from "@/type/preload";
Expand All @@ -7,10 +7,12 @@ export const dictionaryStoreState: DictionaryStoreState = {};

export const dictionaryStore = createPartialStore<DictionaryStoreTypes>({
LOAD_USER_DICT: {
async action({ dispatch }, { engineId }) {
const engineDict = await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
engineId,
}).then((instance) => instance.invoke("getUserDictWordsUserDictGet")({}));
async action({ actions }, { engineId }) {
const engineDict = await actions
.INSTANTIATE_ENGINE_CONNECTOR({
engineId,
})
.then((instance) => instance.invoke("getUserDictWordsUserDictGet")({}));

// 50音順にソートするために、一旦arrayにする
const dictArray = Object.keys(engineDict).map((k) => {
Expand All @@ -32,10 +34,10 @@ export const dictionaryStore = createPartialStore<DictionaryStoreTypes>({
},

LOAD_ALL_USER_DICT: {
async action({ dispatch, state }) {
async action({ actions, state }) {
const allDict = await Promise.all(
state.engineIds.map((engineId) => {
return dispatch("LOAD_USER_DICT", { engineId });
return actions.LOAD_USER_DICT({ engineId });
}),
);
const mergedDictMap = new Map<string, [string, UserDictWord]>();
Expand All @@ -61,98 +63,108 @@ export const dictionaryStore = createPartialStore<DictionaryStoreTypes>({

ADD_WORD: {
async action(
{ state, dispatch },
{ state, actions },
{ surface, pronunciation, accentType, priority },
) {
// 同じ単語IDで登録するために、1つのエンジンで登録したあと全エンジンに同期する。
const engineId: EngineId | undefined = state.engineIds[0];

if (engineId == undefined)
throw new Error(`No such engine registered: index == 0`);
await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
engineId,
}).then((instance) =>
instance.invoke("addUserDictWordUserDictWordPost")({
surface,
pronunciation,
accentType,
priority,
}),
);
await actions
.INSTANTIATE_ENGINE_CONNECTOR({
engineId,
})
.then((instance) =>
instance.invoke("addUserDictWordUserDictWordPost")({
surface,
pronunciation,
accentType,
priority,
}),
);

await dispatch("SYNC_ALL_USER_DICT");
await actions.SYNC_ALL_USER_DICT();
},
},

REWRITE_WORD: {
async action(
{ state, dispatch },
{ state, actions },
{ wordUuid, surface, pronunciation, accentType, priority },
) {
if (state.engineIds.length === 0)
throw new Error(`At least one engine must be registered`);
for (const engineId of state.engineIds) {
await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
engineId,
}).then((instance) =>
instance.invoke("rewriteUserDictWordUserDictWordWordUuidPut")({
wordUuid,
surface,
pronunciation,
accentType,
priority,
}),
);
await actions
.INSTANTIATE_ENGINE_CONNECTOR({
engineId,
})
.then((instance) =>
instance.invoke("rewriteUserDictWordUserDictWordWordUuidPut")({
wordUuid,
surface,
pronunciation,
accentType,
priority,
}),
);
}
},
},

DELETE_WORD: {
async action({ state, dispatch }, { wordUuid }) {
async action({ state, actions }, { wordUuid }) {
if (state.engineIds.length === 0)
throw new Error(`At least one engine must be registered`);
for (const engineId of state.engineIds) {
await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
engineId,
}).then((instance) =>
instance.invoke("deleteUserDictWordUserDictWordWordUuidDelete")({
wordUuid,
}),
);
await actions
.INSTANTIATE_ENGINE_CONNECTOR({
engineId,
})
.then((instance) =>
instance.invoke("deleteUserDictWordUserDictWordWordUuidDelete")({
wordUuid,
}),
);
}
},
},

SYNC_ALL_USER_DICT: {
async action({ dispatch, state }) {
const mergedDict = await dispatch("LOAD_ALL_USER_DICT");
async action({ actions, state }) {
const mergedDict = await actions.LOAD_ALL_USER_DICT();
for (const engineId of state.engineIds) {
// エンジンの辞書のIDリストを取得する。
const dictIdSet = await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
engineId,
}).then(
async (instance) =>
new Set(
Object.keys(
await instance.invoke("getUserDictWordsUserDictGet")({}),
),
),
);
if (Object.keys(mergedDict).some((id) => !dictIdSet.has(id))) {
await dispatch("INSTANTIATE_ENGINE_CONNECTOR", {
const dictIdSet = await actions
.INSTANTIATE_ENGINE_CONNECTOR({
engineId,
}).then((instance) =>
// マージした辞書をエンジンにインポートする。
instance.invoke("importUserDictWordsImportUserDictPost")({
override: true,
requestBody: Object.fromEntries(
Object.entries(mergedDict).map(([k, v]) => [
k,
UserDictWordToJSON(v),
]),
})
.then(
async (instance) =>
new Set(
Object.keys(
await instance.invoke("getUserDictWordsUserDictGet")({}),
),
),
}),
);
if (Object.keys(mergedDict).some((id) => !dictIdSet.has(id))) {
await actions
.INSTANTIATE_ENGINE_CONNECTOR({
engineId,
})
.then((instance) =>
// マージした辞書をエンジンにインポートする。
instance.invoke("importUserDictWordsImportUserDictPost")({
override: true,
requestBody: Object.fromEntries(
Object.entries(mergedDict).map(([k, v]) => [
k,
UserDictWordToJSON(v),
]),
),
}),
);
}
const removedDictIdSet = new Set(dictIdSet);
// マージされた辞書にあるIDを削除する。
Expand All @@ -163,8 +175,9 @@ export const dictionaryStore = createPartialStore<DictionaryStoreTypes>({
}
}

await dispatch("INSTANTIATE_ENGINE_CONNECTOR", { engineId }).then(
(instance) => {
await actions
.INSTANTIATE_ENGINE_CONNECTOR({ engineId })
.then((instance) => {
// マージ処理で削除された項目をエンジンから削除する。
Promise.all(
[...removedDictIdSet].map((id) =>
Expand All @@ -175,8 +188,7 @@ export const dictionaryStore = createPartialStore<DictionaryStoreTypes>({
),
),
);
},
);
});
}
},
},
Expand Down
Loading
Loading