perf: Improve the global loading display

This commit is contained in:
vben
2024-06-02 23:50:58 +08:00
parent e650a0b863
commit 77d40dc763
14 changed files with 111 additions and 57 deletions

View File

@@ -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);
}
/**