feat: enhance settings persistence with platform provider integration

- Updated `persistSettings` to accept a `PlatformProvider`, enabling dynamic loading and saving of production settings.
- Refactored settings loading to handle null cases and invalid JSON gracefully.
- Introduced a new `browserSettingsIo` module for managing settings storage in the browser.
- Added comprehensive tests for `persistSettings` and `browserSettingsIo` to ensure correct functionality and error handling.
- Integrated the updated settings persistence into `AppRoot` for improved platform-specific behavior.
This commit is contained in:
鲁树人 committed 2026-11-01 10:54:13 +09:00
1 parent cc688b85bd
commit cb5531e9e4
7 files changed
+262 -24

No files matched your search

+19 -5
View File
@@ -8,6 +8,7 @@ import type {
NativeImportResult,
PlatformCapabilities,
} from '~/platform/types';
import { createBrowserSettingsIo, DEFAULT_SETTINGS_STORAGE_KEY, type SettingsStorage } from './browserSettingsIo';
import { detectBrowserDownloadTypes } from './detectDownloadCapabilities';
function notImplemented(capability: string): Promise<never> {
@@ -18,20 +19,33 @@ function notSupported(capability: string): Promise<never> {
return Promise.reject(new NotSupportedMethod(capability));
}
export interface BrowserProviderOptions {
/** Defaults to `localStorage`. Injected in tests. */
settingsStorage?: SettingsStorage;
/** Defaults to `um-react-settings`. */
settingsStorageKey?: string;
}
/**
* In-browser `PlatformProvider`.
* Capabilities follow runtime File System Access / save-picker probing (no DirectWrite).
* crypto / settings / download currently throw `NotImplementedError` (not yet delegated to
* the existing worker / localStorage / download helpers).
* `settings` persists production settings via localStorage (or injected storage).
* crypto / download currently throw `NotImplementedError` (not yet delegated to
* the existing worker / download helpers).
* Native and ADB key import throw `NotSupportedMethod` (`nativeKeyAccess` empty, `androidKeyAccess` None).
*/
export function createBrowserProvider(): PlatformProvider {
export function createBrowserProvider(options: BrowserProviderOptions = {}): PlatformProvider {
const capabilities: PlatformCapabilities = Object.freeze({
downloadTypes: Object.freeze(detectBrowserDownloadTypes()),
androidKeyAccess: AndroidImportType.None,
nativeKeyAccess: Object.freeze([] as NativeImportType[]),
});
const settingsIo = createBrowserSettingsIo(
options.settingsStorage ?? localStorage,
options.settingsStorageKey ?? DEFAULT_SETTINGS_STORAGE_KEY,
);
return {
id: 'browser',
capabilities,
@@ -42,8 +56,8 @@ export function createBrowserProvider(): PlatformProvider {
},
settings: {
load: () => notImplemented('settings.load'),
save: () => notImplemented('settings.save'),
load: () => settingsIo.load(),
save: (settings) => settingsIo.save(settings),
},
download: {