Skip to content

Commit

Permalink
Extract constants to named constants, update sync events on connectio…
Browse files Browse the repository at this point in the history
…n change
  • Loading branch information
tfedor committed Sep 16, 2024
1 parent a8bdb7c commit 718f4e0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
21 changes: 13 additions & 8 deletions src/js/Background/Modules/IsThereAnyDeal/ITADApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -202,7 +209,7 @@ export default class ITADApi extends Api implements MessageHandlerInterface {
private async recordSyncEvent(section: string, type: "push"|"pull", count: number): Promise<void> {
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[]) {
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -422,8 +429,6 @@ export default class ITADApi extends Api implements MessageHandlerInterface {
private async pullNotes(force: boolean): Promise<number> {
await SettingsStore.load();

const TTL = 2*60;

if (!force && !(await IndexedDB.isStoreExpired("notes"))) {
return 0;
}
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion src/js/Options/Modules/Options/ITADOptions.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@
<div>
<Section title="IsThereAnyDeal">
<OptionGroup>
<ITADConnection {settings} bind:isConnected on:syncEvent={loadSyncEvents} />
<ITADConnection {settings} bind:isConnected
on:syncEvent={loadSyncEvents}
on:connection={loadSyncEvents}
/>
</OptionGroup>
</Section>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SettingsSchema>;
export let isConnected: boolean = false;
Expand All @@ -30,6 +32,7 @@
promise = (async () => {
await ITADApiFacade.authorize();
isConnected = true;
dispatch("connection");
})();
statusComponent.updateLastImport();
}
Expand All @@ -38,6 +41,7 @@
promise = (async () => {
await ITADApiFacade.disconnect();
isConnected = false;
dispatch("connection");
})();
statusComponent.updateLastImport();
}
Expand Down

0 comments on commit 718f4e0

Please sign in to comment.