From 7732ea2dfea75948715f79ab2d47d5df75799808 Mon Sep 17 00:00:00 2001 From: Alexander Drozdov Date: Sat, 25 Sep 2021 16:02:59 +0300 Subject: [PATCH] refactor config --- src/assets/data/index.ts | 14 ++++---- src/web/Config.ts | 34 ++++++++++++------- src/web/background/Leagues.ts | 6 ++-- src/web/client-log/client-log.ts | 6 ++-- src/web/i18n.ts | 4 +-- src/web/map-check/MapCheck.vue | 6 ++-- src/web/map-check/MapStatButton.vue | 6 ++-- src/web/other-services/wiki.ts | 4 +-- src/web/overlay/OverlayWindow.vue | 14 ++++---- src/web/price-check/BackgroundInfo.vue | 4 +-- src/web/price-check/CheckedItem.vue | 24 ++++++++----- src/web/price-check/PriceCheckWindow.vue | 4 +-- .../price-check/filters/FilterModifier.vue | 4 +-- .../filters/create-item-filters.ts | 25 ++++++++------ .../filters/create-stat-filters.ts | 13 +++---- .../price-check/trade/RateLimiterState.vue | 4 +-- src/web/price-check/trade/TradeBulk.vue | 6 ++-- src/web/price-check/trade/TradeListing.vue | 10 +++--- src/web/price-check/trade/common.ts | 12 +++---- src/web/settings/SettingsWindow.vue | 4 +-- src/web/settings/hotkeys.vue | 8 ++--- src/web/settings/maps.vue | 4 +-- src/web/settings/price-check.vue | 4 +-- src/web/settings/utils.ts | 31 +++++++++++++++++ 24 files changed, 149 insertions(+), 102 deletions(-) create mode 100644 src/web/settings/utils.ts diff --git a/src/assets/data/index.ts b/src/assets/data/index.ts index 52198260..6215485d 100644 --- a/src/assets/data/index.ts +++ b/src/assets/data/index.ts @@ -3,7 +3,7 @@ import type { TranslationDict } from '@/assets/data/en/client_strings' import type { ClientLogDict } from '@/assets/data/en/client_log' import type { BaseType, DropEntry, Stat, StatMatcher, UniqueItem } from './interfaces' -import { Config } from '@/web/Config' +import { AppConfig } from '@/web/Config' import { nameToDetailsId } from '@/web/price-check/trends/getDetailsId' export * from './interfaces' @@ -31,17 +31,19 @@ export let MAP_IMGS: Map export const ITEM_DROP = new Map() ;(function initData () { /* eslint-disable no-lone-blocks */ - { - CLIENT_STRINGS = (require(`./${Config.store.language}/client_strings`).default) - CLIENTLOG_STRINGS = (require(`./${Config.store.language}/client_log`).default) + const { language } = AppConfig() - const itemNames: Array<[string, string]> = (require(`./${Config.store.language}/item-names.json`)) + { + CLIENT_STRINGS = (require(`./${language}/client_strings`).default) + CLIENTLOG_STRINGS = (require(`./${language}/client_log`).default) + + const itemNames: Array<[string, string]> = (require(`./${language}/item-names.json`)) TRANSLATED_ITEM_NAME_BY_REF = new Map(itemNames) ITEM_NAME_REF_BY_TRANSLATED = new Map(itemNames.map(_ => [_[1], _[0]])) } { - STATS = (require(`./${Config.store.language}/stats.json`)) + STATS = (require(`./${language}/stats.json`)) for (const entry of STATS) { for (const condition of entry.stat.matchers) { STAT_BY_MATCH_STR.set(condition.string, { matcher: condition, stat: entry }) diff --git a/src/web/Config.ts b/src/web/Config.ts index 5632d4d9..a74dd770 100644 --- a/src/web/Config.ts +++ b/src/web/Config.ts @@ -1,22 +1,30 @@ -import { reactive } from 'vue' +import { reactive as deepReactive, shallowRef } from 'vue' import { MainProcess } from '@/ipc/main-process-bindings' -import type { Config as ConfigType } from '@/ipc/types' +import type { Config } from '@/ipc/types' import type { Widget } from './overlay/interfaces' -export function getWidgetConfig (type: string) { - return Config.store.widgets.find(w => w.wmType === type) as T | undefined -} +const _config = shallowRef(null) -class ConfigService { - store: ConfigType - - constructor () { - this.store = reactive(MainProcess.getConfig()) +export function AppConfig (): Config +export function AppConfig (type: string): T | undefined +export function AppConfig (type?: string) { + if (!_config.value) { + _config.value = deepReactive(MainProcess.getConfig()) } - saveConfig () { - MainProcess.saveConfig(JSON.parse(JSON.stringify(this.store))) + if (!type) { + return _config.value + } else { + return _config.value.widgets.find(w => w.wmType === type) } } -export const Config = new ConfigService() +export function updateConfig (updates: Config) { + if (_config.value) { + _config.value = deepReactive(JSON.parse(JSON.stringify(updates))) + } +} + +export function saveConfig () { + MainProcess.saveConfig(JSON.parse(JSON.stringify(AppConfig()))) +} diff --git a/src/web/background/Leagues.ts b/src/web/background/Leagues.ts index 6d05f1f7..637a7b03 100644 --- a/src/web/background/Leagues.ts +++ b/src/web/background/Leagues.ts @@ -1,5 +1,5 @@ import { computed, ref } from 'vue' -import { Config } from '@/web/Config' +import { AppConfig } from '@/web/Config' export const isLoading = ref(false) export const error = ref(null) @@ -8,11 +8,11 @@ export const tradeLeagues = ref>([]) export const selected = computed({ get () { return (tradeLeagues.value.length) - ? Config.store.leagueId + ? AppConfig().leagueId : undefined }, set (id) { - Config.store.leagueId = id + AppConfig().leagueId = id } }) diff --git a/src/web/client-log/client-log.ts b/src/web/client-log/client-log.ts index 5a8ab664..452534e2 100644 --- a/src/web/client-log/client-log.ts +++ b/src/web/client-log/client-log.ts @@ -1,6 +1,6 @@ /* eslint-disable camelcase */ import { CLIENTLOG_STRINGS as _$, ITEM_NAME_REF_BY_TRANSLATED, TRADE_TAGS } from '@/assets/data' -import { Config } from '@/web/Config' +import { AppConfig } from '@/web/Config' const enum MessageChannel { SYSTEM = 'system', @@ -64,7 +64,7 @@ export function handleLine (line: string) { entry.body = match.groups!.body if (entry.сhannel === MessageChannel.WHISPER_FROM) { - if ((match = entry.body.match(TRADE_WHISPER[Config.store.language]))) { + if ((match = entry.body.match(TRADE_WHISPER[AppConfig().language]))) { const [pAmount, pTag] = [ match.groups!.price.split(' ', 1).toString(), match.groups!.price.split(' ').slice(1).join(' ') @@ -98,7 +98,7 @@ export function handleLine (line: string) { } // console.log(entry) - } else if ((match = entry.body.match(TRADE_BULK_WHISPER[Config.store.language]))) { + } else if ((match = entry.body.match(TRADE_BULK_WHISPER[AppConfig().language]))) { const [pAmount, pName] = [ match.groups!.price.split(' ', 1).toString(), match.groups!.price.split(' ').slice(1).join(' ') diff --git a/src/web/i18n.ts b/src/web/i18n.ts index fce6c865..a79edcd7 100644 --- a/src/web/i18n.ts +++ b/src/web/i18n.ts @@ -1,9 +1,9 @@ import { createI18n, I18nAdditionalOptions, ComposerOptions } from 'vue-i18n' -import { Config } from './Config' +import { AppConfig } from './Config' export default createI18n({ legacy: false, - locale: Config.store.language, + locale: AppConfig().language, fallbackLocale: 'en', fallbackFormat: true, fallbackWarn: false, diff --git a/src/web/map-check/MapCheck.vue b/src/web/map-check/MapCheck.vue index fe36d0b5..fbe50c7f 100644 --- a/src/web/map-check/MapCheck.vue +++ b/src/web/map-check/MapCheck.vue @@ -25,7 +25,7 @@ import MapStatButton from './MapStatButton.vue' import { prepareMapStats } from './prepare-map-stats' import { TRANSLATED_ITEM_NAME_BY_REF, MAP_IMGS, STAT_BY_MATCH_STR } from '@/assets/data' import { WidgetManager, ItemCheckWidget } from '../overlay/interfaces' -import { Config } from '@/web/Config' +import { AppConfig } from '@/web/Config' export default defineComponent({ components: { @@ -41,9 +41,7 @@ export default defineComponent({ const wm = inject('wm')! const { t } = useI18n() - const config = computed(() => { - return Config.store.widgets.find(widget => widget.wmType === 'item-check') as ItemCheckWidget - }) + const config = computed(() => AppConfig('item-check')!) const mapName = computed(() => { const { item } = props diff --git a/src/web/map-check/MapStatButton.vue b/src/web/map-check/MapStatButton.vue index ae0ab186..ab5d8cb6 100644 --- a/src/web/map-check/MapStatButton.vue +++ b/src/web/map-check/MapStatButton.vue @@ -26,7 +26,7 @@