Skip to content

Commit db5c4c0

Browse files
committed
feat(mobile): enhance settings synchronization and event handling
- Updated the settings change event to include a structured payload for better data management. - Refactored the persist and load methods in the SettingSyncQueue to utilize a key-value storage system instead of localStorage. - Added logging for remote settings in development mode to aid in debugging. Signed-off-by: Innei <tukon479@gmail.com>
1 parent 1e9d20e commit db5c4c0

2 files changed

Lines changed: 16 additions & 7 deletions

File tree

apps/mobile/src/atoms/settings/internal/helper.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ export const createSettingAtom = <T extends object>(
119119
const updated = Date.now()
120120

121121
EventBus.dispatch("SETTING_CHANGE_EVENT", {
122-
key: key as "general" | "ui",
123-
payload: value,
122+
key: settingKey as "general" | "ui",
123+
payload: {
124+
[key]: value,
125+
},
124126
})
125127
setSettings({
126128
...getSettings(),

apps/mobile/src/modules/settings/sync-queue.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from "@/src/atoms/settings/general"
1212
import { __uiSettingAtom, getUISettings, uiServerSyncWhiteListKeys } from "@/src/atoms/settings/ui"
1313
import { apiClient } from "@/src/lib/api-fetch"
14+
import { kv } from "@/src/lib/kv"
1415

1516
type SettingMapping = {
1617
appearance: UISettings
@@ -77,6 +78,8 @@ class SettingSyncQueue {
7778
const nextPayload = omit(data.payload, omitKeys, settingWhiteListMap[tab])
7879
if (isEmptyObject(nextPayload)) return
7980
this.enqueue(tab, nextPayload)
81+
82+
this.persist()
8083
})
8184

8285
this.disposers.push(d1)
@@ -90,16 +93,16 @@ class SettingSyncQueue {
9093
}
9194

9295
private readonly storageKey = "setting_sync_queue"
93-
private persist() {
96+
private async persist() {
9497
if (this.queue.length === 0) {
9598
return
9699
}
97-
localStorage.setItem(this.storageKey, JSON.stringify(this.queue))
100+
kv.set(this.storageKey, JSON.stringify(this.queue))
98101
}
99102

100-
private load() {
101-
const queue = localStorage.getItem(this.storageKey)
102-
localStorage.removeItem(this.storageKey)
103+
private async load() {
104+
const queue = await kv.get(this.storageKey)
105+
kv.delete(this.storageKey)
103106
if (!queue) {
104107
return
105108
}
@@ -242,6 +245,10 @@ class SettingSyncQueue {
242245
const remoteSettings = await this.fetchSettingRemote()
243246

244247
if (!remoteSettings) return
248+
if (__DEV__) {
249+
// eslint-disable-next-line no-console
250+
console.log("remote settings:", remoteSettings)
251+
}
245252

246253
if (isEmptyObject(remoteSettings.settings)) return
247254

0 commit comments

Comments
 (0)