From 3cd66412c4461b36b8fd1dec75dc1d9f31a34eda Mon Sep 17 00:00:00 2001 From: morizon Date: Thu, 26 Dec 2024 14:14:35 +0800 Subject: [PATCH] fix: ext appStorage save object (#6410) --- packages/shared/src/storage/appStorageUtils.ts | 2 +- packages/shared/src/storage/syncStorage.ts | 9 +++++++-- packages/shared/src/utils/assertUtils.ts | 8 ++++---- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/shared/src/storage/appStorageUtils.ts b/packages/shared/src/storage/appStorageUtils.ts index 5d69c760d73..994a906be54 100644 --- a/packages/shared/src/storage/appStorageUtils.ts +++ b/packages/shared/src/storage/appStorageUtils.ts @@ -9,7 +9,7 @@ let _canSaveAsObject: boolean | null = null; function canSaveAsObject(): boolean { if (_canSaveAsObject === null) { _canSaveAsObject = false; - if (platformEnv.isRuntimeBrowser) { + if (platformEnv.isRuntimeBrowser || platformEnv.isExtension) { const isIndexedDB: boolean = ( appStorage as unknown as WebStorage )?.isIndexedDB(); diff --git a/packages/shared/src/storage/syncStorage.ts b/packages/shared/src/storage/syncStorage.ts index 3ad008e5429..6debb3385f0 100644 --- a/packages/shared/src/storage/syncStorage.ts +++ b/packages/shared/src/storage/syncStorage.ts @@ -2,6 +2,7 @@ import { isPlainObject } from 'lodash'; import appGlobals from '../appGlobals'; import platformEnv from '../platformEnv'; +import { ensureRunOnBackground } from '../utils/assertUtils'; import dbPerfMonitor from '../utils/debug/dbPerfMonitor'; import resetUtils from '../utils/resetUtils'; @@ -30,14 +31,18 @@ export const buildAppStorageFactory = ( const setItem: IAppStorage['setItem'] = (key, value, callback) => { resetUtils.checkNotInResetting(); dbPerfMonitor.logAppStorageCall('setItem', key); + ensureRunOnBackground(); return originalSetItem.call(storage, key, value, callback); }; const getItem: IAppStorage['getItem'] = (key, callback) => { dbPerfMonitor.logAppStorageCall('getItem', key); + ensureRunOnBackground(); return originalGetItem.call(storage, key, callback); }; - const removeItem: IAppStorage['removeItem'] = (key, callback) => - originalRemoveItem.call(storage, key, callback); + const removeItem: IAppStorage['removeItem'] = (key, callback) => { + ensureRunOnBackground(); + return originalRemoveItem.call(storage, key, callback); + }; storage.setItem = setItem; storage.getItem = getItem; diff --git a/packages/shared/src/utils/assertUtils.ts b/packages/shared/src/utils/assertUtils.ts index 0d1d9261993..ab34bf8f122 100644 --- a/packages/shared/src/utils/assertUtils.ts +++ b/packages/shared/src/utils/assertUtils.ts @@ -10,10 +10,10 @@ import { isUndefined, } from 'lodash'; +import appGlobals from '../appGlobals'; import errorUtils from '../errors/utils/errorUtils'; import platformEnv from '../platformEnv'; -import appStorage from '../storage/appStorage'; -import { EAppSyncStorageKeys } from '../storage/syncStorage'; +import { EAppSyncStorageKeys } from '../storage/syncStorageKeys'; import { isPromiseObject } from './promiseUtils'; import timerUtils from './timerUtils'; @@ -108,7 +108,7 @@ export function toggleBgApiSerializableChecking(enabled: boolean) { disabled: !enabled, updateAt: Date.now(), }; - appStorage.syncStorage.setObject( + appGlobals.$appStorage?.syncStorage.setObject( EAppSyncStorageKeys.onekey_disable_bg_api_serializable_checking, data, ); @@ -116,7 +116,7 @@ export function toggleBgApiSerializableChecking(enabled: boolean) { export function isBgApiSerializableCheckingDisabled() { try { const data = - appStorage.syncStorage.getObject( + appGlobals.$appStorage?.syncStorage.getObject( EAppSyncStorageKeys.onekey_disable_bg_api_serializable_checking, ); if (!data) {