Skip to content

Commit

Permalink
解决Stroage的sync的Bug:没有同步更新js侧的缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
58liuyang committed May 27, 2024
1 parent 24aa13e commit 0af706a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 18 deletions.
61 changes: 49 additions & 12 deletions packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: ''}

Check failure on line 853 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

A space is required before '}'

Check failure on line 853 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

A space is required before '}'

Check failure on line 853 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

A space is required before '}'

Check failure on line 853 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

A space is required before '}'
} else {
const status = this.asyncToSyncProxy.getStorageSync({ key })
if (status.done && status.errorMsg === '') {
Expand All @@ -860,25 +860,62 @@ class CacheStorageProxy {
}
}
}
if(prop === 'getStorage') {

Check failure on line 863 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Expected space(s) after "if"

Check failure on line 863 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Expected space(s) after "if"

Check failure on line 863 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Expected space(s) after "if"

Check failure on line 863 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Expected space(s) after "if"
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)})

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

A space is required after '{'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Missing space before value for key 'errMsg'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

A space is required before '}'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

A space is required after '{'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Missing space before value for key 'errMsg'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

A space is required before '}'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

A space is required after '{'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space before value for key 'errMsg'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

A space is required before '}'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

A space is required after '{'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space before value for key 'errMsg'

Check failure on line 869 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

A space is required before '}'
} else {
this.nativeApi['getStorage']({

Check failure on line 871 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

["getStorage"] is better written in dot notation

Check failure on line 871 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

["getStorage"] is better written in dot notation

Check failure on line 871 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

["getStorage"] is better written in dot notation

Check failure on line 871 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

["getStorage"] is better written in dot notation
key: key,
fail: fail,
success: (res)=> {

Check failure on line 874 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Missing space before =>

Check failure on line 874 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Missing space before =>

Check failure on line 874 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space before =>

Check failure on line 874 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space before =>
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']({

Check failure on line 887 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

["setStorage"] is better written in dot notation

Check failure on line 887 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

["setStorage"] is better written in dot notation

Check failure on line 887 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

["setStorage"] is better written in dot notation

Check failure on line 887 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

["setStorage"] is better written in dot notation
key: key,
data: data,
fail: ()=>{},

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Missing space before =>

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (macos-11)

Missing space after =>

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Missing space before =>

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (windows-latest)

Missing space after =>

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space before =>

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space after =>

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space before =>

Check failure on line 890 in packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts

View workflow job for this annotation

GitHub Actions / Testing on Node.js 16.x (ubuntu-latest)

Missing space after =>
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[]) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/**
Expand Down Expand Up @@ -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)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 */
}
Expand Down

0 comments on commit 0af706a

Please sign in to comment.