perf: optimize for some details and comments (#4030)

* perf: optimize for some details and comments

* fix: test case

* chore: update ci
This commit is contained in:
Vben
2024-08-04 05:42:59 +08:00
committed by GitHub
parent d3ed6757ac
commit b6415fad2d
20 changed files with 37 additions and 134 deletions

View File

@@ -1,7 +1,8 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { defaultPreferences } from './config';
import { isDarkTheme, PreferenceManager } from './preferences';
import { PreferenceManager } from './preferences';
import { isDarkTheme } from './update-css-variables';
describe('preferences', () => {
let preferenceManager: PreferenceManager;

View File

@@ -19,14 +19,6 @@ const STORAGE_KEY = 'preferences';
const STORAGE_KEY_LOCALE = `${STORAGE_KEY}-locale`;
const STORAGE_KEY_THEME = `${STORAGE_KEY}-theme`;
function isDarkTheme(theme: string) {
let dark = theme === 'dark';
if (theme === 'auto') {
dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
}
return dark;
}
class PreferenceManager {
private cache: null | StorageManager = null;
// private flattenedState: Flatten<Preferences>;
@@ -39,6 +31,7 @@ class PreferenceManager {
constructor() {
this.cache = new StorageManager();
// 避免频繁的操作缓存
this.savePreferences = useDebounceFn(
(preference: Preferences) => this._savePreferences(preference),
150,
@@ -58,7 +51,6 @@ class PreferenceManager {
/**
* 处理更新的键值
* 根据更新的键值执行相应的操作。
*
* @param {DeepPartial<Preferences>} updates - 部分更新的偏好设置
*/
private handleUpdates(updates: DeepPartial<Preferences>) {
@@ -124,7 +116,7 @@ class PreferenceManager {
this.updatePreferences({
theme: { mode: isDark ? 'dark' : 'light' },
});
updateCSSVariables(this.state);
// updateCSSVariables(this.state);
});
}
@@ -232,4 +224,4 @@ class PreferenceManager {
}
const preferencesManager = new PreferenceManager();
export { isDarkTheme, PreferenceManager, preferencesManager };
export { PreferenceManager, preferencesManager };

View File

@@ -115,4 +115,4 @@ function isDarkTheme(theme: string) {
return dark;
}
export { updateCSSVariables };
export { isDarkTheme, updateCSSVariables };

View File

@@ -2,7 +2,8 @@ import { computed } from 'vue';
import { diff } from '@vben-core/shared';
import { isDarkTheme, preferencesManager } from './preferences';
import { preferencesManager } from './preferences';
import { isDarkTheme } from './update-css-variables';
function usePreferences() {
const preferences = preferencesManager.getPreferences();