From 718f4e020e794ea9fd3aeaa52f996496e8ae9d64 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Fedor?= Date: Mon, 16 Sep 2024 17:50:28 +0200 Subject: [PATCH] Extract constants to named constants, update sync events on connection change --- .../Modules/IsThereAnyDeal/ITADApi.ts | 21 ++++++++++++------- .../Modules/Options/ITADOptions.svelte | 5 ++++- .../Options/Settings/ITADConnection.svelte | 6 +++++- 3 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/js/Background/Modules/IsThereAnyDeal/ITADApi.ts b/src/js/Background/Modules/IsThereAnyDeal/ITADApi.ts index 50be36508..38cd8d89b 100644 --- a/src/js/Background/Modules/IsThereAnyDeal/ITADApi.ts +++ b/src/js/Background/Modules/IsThereAnyDeal/ITADApi.ts @@ -26,6 +26,13 @@ import {__userNote_syncErrorEmpty, __userNote_syncErrorLength, __userNote_syncEr import SyncedStorageAdapter from "@Content/Modules/UserNotes/Adapters/SyncedStorageAdapter"; const MaxNoteLength = 250; +const SyncEventsLimit = 40; +const StoreListTTL = 84600; +const PushGamesInterval = 5*60; +const PullWaitlistInterval = 15*60; +const PullCollectionInterval = 15*60; +const PullNotesInterval = 2*60; + const RequiredScopes = [ "wait_read", "wait_write", @@ -167,7 +174,7 @@ export default class ITADApi extends Api implements MessageHandlerInterface { if (await IndexedDB.isStoreExpired("storeList")) { let result = await this.fetchStoreList(); await IndexedDB.replaceAll("storeList", result.map(store => [store.id, store])); - await IndexedDB.setStoreExpiry("storeList", 7*86400); + await IndexedDB.setStoreExpiry("storeList", StoreListTTL); return result; } else { return await IndexedDB.db.getAll("storeList"); @@ -202,7 +209,7 @@ export default class ITADApi extends Api implements MessageHandlerInterface { private async recordSyncEvent(section: string, type: "push"|"pull", count: number): Promise { const events = await this.getSyncEvents(); events.unshift({section, type, timestamp: TimeUtils.now(), count}); - await LocalStorage.set("syncEvents", events.slice(0, 20)); + await LocalStorage.set("syncEvents", events.slice(0, SyncEventsLimit)); } private async removeFromWaitlist(appids: number[]) { @@ -308,7 +315,7 @@ export default class ITADApi extends Api implements MessageHandlerInterface { } else { const lastImport = await this.getLastImport(); - if (lastImport.to && TimeUtils.isInFuture(lastImport.to + 60)) { + if (lastImport.to && TimeUtils.isInFuture(lastImport.to + PushGamesInterval)) { return; } } @@ -371,7 +378,7 @@ export default class ITADApi extends Api implements MessageHandlerInterface { if (force || await IndexedDB.isStoreExpired("waitlist")) { const data = await this.fetchWaitlist(); await IndexedDB.replaceAll("waitlist", data ? [...data.entries()] : []); - await IndexedDB.setStoreExpiry("waitlist", 15*60); + await IndexedDB.setStoreExpiry("waitlist", PullWaitlistInterval); await this.recordLastImport(); await this.recordSyncEvent("waitlist", "pull", data?.size ?? 0); } @@ -386,7 +393,7 @@ export default class ITADApi extends Api implements MessageHandlerInterface { if (force || await IndexedDB.isStoreExpired("collection")) { const data = await this.fetchCollection(); await IndexedDB.replaceAll("collection", data ? [...data.entries()] : []); - await IndexedDB.setStoreExpiry("collection", 15*60); + await IndexedDB.setStoreExpiry("collection", PullCollectionInterval); await this.recordLastImport(); await this.recordSyncEvent("collection", "pull", data?.size ?? 0); } @@ -422,8 +429,6 @@ export default class ITADApi extends Api implements MessageHandlerInterface { private async pullNotes(force: boolean): Promise { await SettingsStore.load(); - const TTL = 2*60; - if (!force && !(await IndexedDB.isStoreExpired("notes"))) { return 0; } @@ -483,7 +488,7 @@ export default class ITADApi extends Api implements MessageHandlerInterface { } } - await IndexedDB.setStoreExpiry("notes", TTL); + await IndexedDB.setStoreExpiry("notes", PullNotesInterval); await this.recordLastImport(); await this.recordSyncEvent("notes", "pull", result.size); return result.size; diff --git a/src/js/Options/Modules/Options/ITADOptions.svelte b/src/js/Options/Modules/Options/ITADOptions.svelte index e9048f58b..b1d44ab43 100644 --- a/src/js/Options/Modules/Options/ITADOptions.svelte +++ b/src/js/Options/Modules/Options/ITADOptions.svelte @@ -44,7 +44,10 @@
- +
diff --git a/src/js/Options/Modules/Options/Settings/ITADConnection.svelte b/src/js/Options/Modules/Options/Settings/ITADConnection.svelte index bd3ea89c8..92378377a 100644 --- a/src/js/Options/Modules/Options/Settings/ITADConnection.svelte +++ b/src/js/Options/Modules/Options/Settings/ITADConnection.svelte @@ -11,13 +11,15 @@ __status } from "@Strings/_strings"; import {L} from "@Core/Localization/Localization"; - import {onMount} from "svelte"; + import {createEventDispatcher, onMount} from "svelte"; import ITADApiFacade from "@Content/Modules/Facades/ITADApiFacade"; import ITADSyncStatus from "@Content/Modules/Widgets/ITADSync/ITADSyncStatus.svelte"; import ESyncStatus from "@Core/Sync/ESyncStatus"; import {type Writable} from "svelte/store"; import type {SettingsSchema} from "@Options/Data/_types"; + const dispatch = createEventDispatcher<{connection: void}>(); + export let settings: Writable; export let isConnected: boolean = false; @@ -30,6 +32,7 @@ promise = (async () => { await ITADApiFacade.authorize(); isConnected = true; + dispatch("connection"); })(); statusComponent.updateLastImport(); } @@ -38,6 +41,7 @@ promise = (async () => { await ITADApiFacade.disconnect(); isConnected = false; + dispatch("connection"); })(); statusComponent.updateLastImport(); }