perf: Improve the global loading display
This commit is contained in:
@@ -21,6 +21,8 @@ import { defaultPreferences } from './config';
|
||||
import type { Preferences } from './types';
|
||||
|
||||
const STORAGE_KEY = 'preferences';
|
||||
const STORAGE_KEY_LOCALE = `${STORAGE_KEY}-locale`;
|
||||
const STORAGE_KEY_THEME = `${STORAGE_KEY}-theme`;
|
||||
|
||||
interface initialOptions {
|
||||
namespace: string;
|
||||
@@ -36,7 +38,7 @@ function isDarkTheme(theme: string) {
|
||||
}
|
||||
|
||||
class PreferenceManager {
|
||||
private cache: StorageManager<Preferences> | null = null;
|
||||
private cache: StorageManager | null = null;
|
||||
private flattenedState: Flatten<Preferences>;
|
||||
private initialPreferences: Preferences = defaultPreferences;
|
||||
private isInitialized: boolean = false;
|
||||
@@ -60,6 +62,8 @@ class PreferenceManager {
|
||||
*/
|
||||
private _savePreferences(preference: Preferences) {
|
||||
this.cache?.setItem(STORAGE_KEY, preference);
|
||||
this.cache?.setItem(STORAGE_KEY_LOCALE, preference.app.locale);
|
||||
this.cache?.setItem(STORAGE_KEY_THEME, preference.app.themeMode);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -89,7 +93,7 @@ class PreferenceManager {
|
||||
* 从缓存中加载偏好设置。如果缓存中没有找到对应的偏好设置,则返回默认偏好设置。
|
||||
*/
|
||||
private loadCachedPreferences() {
|
||||
return this.cache?.getItem(STORAGE_KEY);
|
||||
return this.cache?.getItem<Preferences>(STORAGE_KEY);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -231,8 +235,8 @@ class PreferenceManager {
|
||||
|
||||
/**
|
||||
* 覆盖偏好设置
|
||||
* @param overrides - 要覆盖的偏好设置
|
||||
* @param namespace - 命名空间
|
||||
* overrides 要覆盖的偏好设置
|
||||
* namespace 命名空间
|
||||
*/
|
||||
public async initPreferences({ namespace, overrides }: initialOptions) {
|
||||
// 是否初始化过
|
||||
@@ -273,6 +277,8 @@ class PreferenceManager {
|
||||
this.savePreferences(this.state);
|
||||
// 从存储中移除偏好设置项
|
||||
this.cache?.removeItem(STORAGE_KEY);
|
||||
this.cache?.removeItem(STORAGE_KEY_THEME);
|
||||
this.cache?.removeItem(STORAGE_KEY_LOCALE);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -10,7 +10,7 @@ interface StorageItem<T> {
|
||||
value: T;
|
||||
}
|
||||
|
||||
class StorageManager<T> {
|
||||
class StorageManager {
|
||||
private prefix: string;
|
||||
private storage: Storage;
|
||||
|
||||
@@ -67,7 +67,7 @@ class StorageManager<T> {
|
||||
* @param defaultValue 当项不存在或已过期时返回的默认值
|
||||
* @returns 值,如果项已过期或解析错误则返回默认值
|
||||
*/
|
||||
getItem(key: string, defaultValue: T | null = null): T | null {
|
||||
getItem<T>(key: string, defaultValue: T | null = null): T | null {
|
||||
const fullKey = this.getFullKey(key);
|
||||
const itemStr = this.storage.getItem(fullKey);
|
||||
if (!itemStr) {
|
||||
@@ -103,7 +103,7 @@ class StorageManager<T> {
|
||||
* @param value 值
|
||||
* @param ttl 存活时间(毫秒)
|
||||
*/
|
||||
setItem(key: string, value: T, ttl?: number): void {
|
||||
setItem<T>(key: string, value: T, ttl?: number): void {
|
||||
const fullKey = this.getFullKey(key);
|
||||
const expiry = ttl ? Date.now() + ttl : undefined;
|
||||
const item: StorageItem<T> = { expiry, value };
|
||||
|
Reference in New Issue
Block a user