diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts index ca93a0acdee4..f0497e6897ed 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts @@ -850,7 +850,7 @@ class CacheStorageProxy { return (...args: any[]) => { const key = args[0].key if (this.cacheMap.has(key)) { - return this.cacheMap.get(key) + return { done: true, data: this.cacheMap.get(key), errorMsg: ''} } else { const status = this.asyncToSyncProxy.getStorageSync({ key }) if (status.done && status.errorMsg === '') { @@ -860,25 +860,62 @@ class CacheStorageProxy { } } } + if(prop === 'getStorage') { + return (...args: any[]) => { + const key = args[0].key + const fail = args[0].fail + const success = args[0].success + if (this.cacheMap.has(key)) { + success({errMsg:'ok', data: this.cacheMap.get(key)}) + } else { + this.nativeApi['getStorage']({ + key: key, + fail: fail, + success: (res)=> { + this.cacheMap.set(key, res.data) + success(res) + } + }) + } + } + } if (prop === 'setStorageSync') { return (...args: any[]) => { const { key, data } = args[0] - const status = this.asyncToSyncProxy.setStorageSync({ key, data }) - if (status.done && status.errorMsg === '') { - status.data = data - this.cacheMap.set(key, status) - } - return status + // 先更新js缓存,同异步原生,TODO 考虑失败的情况 + this.cacheMap.set(key, data) + this.nativeApi['setStorage']({ + key: key, + data: data, + fail: ()=>{}, + success: () => {} + }) + } + } + if(prop === 'setStorage') { + return (...args: any[]) => { + const key = args[0].key + const data = args[0].data + this.cacheMap.set(key, data) + // @ts-ignore + this.nativeApi['setStorage']({key: key, data: data}) } } if (prop === 'removeStorageSync') { return (...args: any[]) => { const { key } = args[0] - const status = this.asyncToSyncProxy.removeStorageSync({ key }) - if (status.done && status.errorMsg === '') { - this.cacheMap.delete(key) - } - return status + // 先更新缓存,再同步原生 + this.cacheMap.delete(key) + this.nativeApi['removeStorage']({key: key}) + } + } + if (prop === 'removeStorage') { + return (...args: any[]) => { + const { key } = args[0] + // 先更新缓存,再同步原生 + this.cacheMap.delete(key) + // @ts-ignore + this.nativeApi['removeStorage']({key: key}) } } return (...args: any[]) => { diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/storage/index.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/storage/index.ts index b58fa3dd93e0..8ea3d3f43100 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/storage/index.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/storage/index.ts @@ -22,8 +22,8 @@ export const createCacheManager = /* @__PURE__ */ temporarilyNotSupport('createC * @__object [key, data] */ export const setStorageSync: typeof Taro.setStorageSync = (key, data = '') => { - const status = native.setStorageSync({ key, data: JSON.stringify(handleData(data)) }) - displayExecRes(status, setStorageSync.name) + native.setStorageSync({ key, data: JSON.stringify(handleData(data)) }) + // displayExecRes(status, setStorageSync.name) } /** @@ -71,8 +71,8 @@ export const revokeBufferURL = /* @__PURE__ */ temporarilyNotSupport('revokeBuff * @canUse removeStorageSync */ export const removeStorageSync: typeof Taro.removeStorageSync = (key: string) => { - const status = native.removeStorageSync({ key }) - displayExecRes(status, removeStorageSync.name) + native.removeStorageSync({ key }) + // displayExecRes(status, removeStorageSync.name) } /** diff --git a/packages/taro-platform-harmony-hybrid/src/api/apis/storage/util.ts b/packages/taro-platform-harmony-hybrid/src/api/apis/storage/util.ts index 03d40aef2878..d91392116b56 100644 --- a/packages/taro-platform-harmony-hybrid/src/api/apis/storage/util.ts +++ b/packages/taro-platform-harmony-hybrid/src/api/apis/storage/util.ts @@ -1,8 +1,8 @@ import { Status } from '../NativeApi' export function displayExecRes (status: Status, method: string) { - if (!status.done) { - console.error({ errMsg: `${method} execution fail: ` + status.errorMsg }) + if (!status?.done) { + console.error({ errMsg: `${method} execution fail: ` + status?.errorMsg }) } else { /* empty */ }