| |
| |
| |
|
|
| const SETTINGS_PUSH_DEBOUNCE_MS = 1500; |
| let _settingsPushTimer = null; |
|
|
| |
| |
| |
| |
| async function syncSettings() { |
| const isAuthenticated = window.__bayanAuth && |
| window.__bayanAuth.userId && |
| !window.__bayanAuth.isOfflineMode; |
|
|
| if (!isAuthenticated) return; |
|
|
| const settings = await loadSettings(); |
| if (!settings) return; |
|
|
| |
| if (settings.theme && typeof setTheme === 'function') { |
| setTheme(settings.theme); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| function onSettingsChanged(key, value) { |
| if (_settingsPushTimer) clearTimeout(_settingsPushTimer); |
| _settingsPushTimer = setTimeout(async () => { |
| const isAuthenticated = window.__bayanAuth && |
| window.__bayanAuth.userId && |
| !window.__bayanAuth.isOfflineMode; |
| if (!isAuthenticated) return; |
| await saveSettings({ [key]: value }); |
| }, SETTINGS_PUSH_DEBOUNCE_MS); |
| } |
|
|
| |
| |
| |
| function _bindSettingsListeners() { |
| window.addEventListener('bayan:themechange', (e) => { |
| if (e.detail && e.detail.theme) { |
| onSettingsChanged('theme', e.detail.theme); |
| } |
| }); |
|
|
| |
| window.addEventListener('bayan:authchange', () => { |
| syncSettings(); |
| }); |
| } |
|
|
| |
| |
| |
| async function initSettingsSync() { |
| _bindSettingsListeners(); |
| await syncSettings(); |
| } |
|
|