From 12ab1c5ed442fcb0c24db749d057cf5ea7ae8ddd Mon Sep 17 00:00:00 2001 From: Yan-Fa Li Date: Fri, 1 Nov 2024 15:01:09 -0700 Subject: [PATCH] chore: simplify fetch - use await to flatten code - must await dispatch otherwise there's a race for the background updating correctly --- src/store/modules/app/actions.js | 19 +++++---- src/store/modules/app/mutations.js | 1 - src/store/tester.js | 65 ------------------------------ 3 files changed, 9 insertions(+), 76 deletions(-) delete mode 100644 src/store/tester.js diff --git a/src/store/modules/app/actions.js b/src/store/modules/app/actions.js index b25b875374..6383affd86 100644 --- a/src/store/modules/app/actions.js +++ b/src/store/modules/app/actions.js @@ -77,7 +77,7 @@ const actions = { commit('setKeyboard', keyboard); const oldLayout = state.layout || ''; commit('setLayout', undefined); - dispatch('loadLayouts'); + await dispatch('loadLayouts'); let nextLayout = getPreferredLayout(state.layouts); console.info(getPreferredLayout(state.layouts)); if (oldLayout && !isUndefined(state.layouts[oldLayout])) { @@ -122,16 +122,15 @@ const actions = { }); return p; } - await fetch(`${backend_keyboards_url}/${state.keyboard}/info.json`).then( - async (resp) => { - if (resp.ok) { - const data = await resp.json(); - commit('setKeyboardMeta', data); - commit('processLayouts', data); - return resp; - } - } + const resp = await fetch( + `${backend_keyboards_url}/${state.keyboard}/info.json` ); + if (resp.ok) { + const data = await resp.json(); + commit('setKeyboardMeta', data); + commit('processLayouts', data); + return resp; + } }, saveConfiguratorSettings({ state }) { localStorageSet( diff --git a/src/store/modules/app/mutations.js b/src/store/modules/app/mutations.js index fdceb291af..bd8dead14d 100644 --- a/src/store/modules/app/mutations.js +++ b/src/store/modules/app/mutations.js @@ -196,7 +196,6 @@ const mutations = { state.snowflakes = !state.snowflakes; }, setKeyboardMeta(state, data) { - debugger; state.keyboardMeta = data?.keyboards ? data.keyboards : {}; } }; diff --git a/src/store/tester.js b/src/store/tester.js deleted file mode 100644 index 40da7f14c8..0000000000 --- a/src/store/tester.js +++ /dev/null @@ -1,65 +0,0 @@ -import { defineStore } from 'pinia'; -import defaults from '../config'; -import codeToPos from './modules/tester/codeToPos'; -import layouts from './modules/tester/layouts'; -import { isUndefined } from 'lodash'; - -function reduceCodeToPos(arr) { - // Create look up table for Browser Code to Layout position - return arr.reduce((acc, code, idx) => { - acc[code] = idx; - return acc; - }, {}); -} - -const codeToPosition = { - ANSI: reduceCodeToPos(codeToPos.ANSI), - ISO: reduceCodeToPos(codeToPos.ISO) -}; - -function getDefaultLayout() { - const userLang = navigator.language || navigator.userLanguage; - let layout = 'ANSI'; - if (userLang.toLowerCase().indexOf('en') < 0) { - layout = 'ISO'; - } - return layout; -} - -export const useStore = defineStore('tester', { - state: () => ({ - defaults, - codeToPosition, - config: Object.assign({}, defaults), - layout: getDefaultLayout(), - keymap: {}, - layouts: { - ISO: layouts.ISO, - ANSI: layouts.ANSI - }, - chatterDetected: false - }), - getters: { - availableLayouts(state) { - return Object.keys(state.layouts.sort()); - }, - getQMKCode(state) { - return (pos) => { - if (isUndefined(pos)) { - return ''; - } - return state.keymap[state.layout][pos].code; - }; - }, - activeKeymap(state) { - return state.keymap[state.layout]; - }, - activeLayoutMeta(state) { - return state.layouts[state.layout]; - }, - codeToPosition(state) { - return state.codeToPosition[state.layout]; - }, - actions: {} - } -});