refactor(RouterProvider, chromeStorage, useContextMenuData): 更新存储获取逻辑以移除默认值参数
- 在 RouterProvider 和 useContextMenuData 中更新对 storageUtil.get 的调用,移除默认值参数,简化逻辑。 - 修改 chromeStorage 中的 get 方法签名,确保在没有默认值时返回可选类型,并添加相应的单元测试以验证类型推断。
This commit is contained in:
@@ -193,7 +193,7 @@ export function RouterProvider({
|
||||
|
||||
// Check storage for pending context menu data
|
||||
storageUtil
|
||||
.get('contextMenu/pendingData', undefined)
|
||||
.get('contextMenu/pendingData')
|
||||
.then((pendingData) => {
|
||||
if (
|
||||
pendingData &&
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { describe, expect, it, expectTypeOf } from 'vitest';
|
||||
import { storageUtil } from '@/utils/chromeStorage';
|
||||
|
||||
describe('chromeStorage', () => {
|
||||
@@ -75,6 +75,20 @@ describe('chromeStorage', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('get 类型签名', () => {
|
||||
it('无默认值时应推断为可选返回类型', () => {
|
||||
const getWithoutDefault = () => storageUtil.get('app/theme');
|
||||
expectTypeOf<ReturnType<typeof getWithoutDefault>>().toEqualTypeOf<
|
||||
Promise<string | undefined>
|
||||
>();
|
||||
});
|
||||
|
||||
it('有默认值时应推断为确定返回类型', () => {
|
||||
const getWithDefault = () => storageUtil.get('app/theme', 'light');
|
||||
expectTypeOf<ReturnType<typeof getWithDefault>>().toEqualTypeOf<Promise<string>>();
|
||||
});
|
||||
});
|
||||
|
||||
describe('set', () => {
|
||||
it('应该成功设置字符串值', async () => {
|
||||
(chrome.storage.local.set as any).mockResolvedValue(undefined);
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { StorageSchema } from '@/types/storage';
|
||||
|
||||
class StorageUtils {
|
||||
async get<K extends keyof StorageSchema>(key: K): Promise<StorageSchema[K]>;
|
||||
|
||||
async get<K extends keyof StorageSchema>(
|
||||
key: K,
|
||||
defaultValue?: StorageSchema[K],
|
||||
): Promise<StorageSchema[K] | undefined>;
|
||||
defaultValue: StorageSchema[K],
|
||||
): Promise<StorageSchema[K]>;
|
||||
|
||||
async get<K extends keyof StorageSchema>(key: K): Promise<StorageSchema[K] | undefined>;
|
||||
|
||||
/**
|
||||
* 获取值
|
||||
@@ -19,7 +19,10 @@ class StorageUtils {
|
||||
defaultValue?: StorageSchema[K],
|
||||
): Promise<StorageSchema[K] | undefined> {
|
||||
const result = await chrome.storage.local.get([key]);
|
||||
return (result[key] ?? defaultValue) as StorageSchema[K] | undefined;
|
||||
if (defaultValue !== undefined) {
|
||||
return (result[key] ?? defaultValue) as StorageSchema[K];
|
||||
}
|
||||
return result[key] as StorageSchema[K] | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -26,7 +26,7 @@ export function useContextMenuData({ featureKey, onData }: UseContextMenuDataOpt
|
||||
useEffect(() => {
|
||||
const checkAndConsumeData = async () => {
|
||||
try {
|
||||
const data = await storageUtil.get(STORAGE_KEY, undefined);
|
||||
const data = await storageUtil.get(STORAGE_KEY);
|
||||
|
||||
if (!data) return;
|
||||
|
||||
@@ -55,7 +55,7 @@ export function useContextMenuData({ featureKey, onData }: UseContextMenuDataOpt
|
||||
if (newData && newData.featureKey === featureKey) {
|
||||
void (async () => {
|
||||
try {
|
||||
const data = await storageUtil.get(STORAGE_KEY, undefined);
|
||||
const data = await storageUtil.get(STORAGE_KEY);
|
||||
if (!data) return;
|
||||
if (data.featureKey !== featureKey) return;
|
||||
if (Date.now() - data.timestamp > CONTEXT_MENU_DATA_EXPIRY_MS) {
|
||||
|
||||
Reference in New Issue
Block a user