Skip to content

Commit

Permalink
Merge pull request #16264 from kongxiaojun/f-taro-storage-cache-opt
Browse files Browse the repository at this point in the history
【Harmony-Hybrid】优化Taro Storage缓存,解决跨Web容器修改Storage后缓存不更新
  • Loading branch information
qican777 authored Sep 9, 2024
2 parents eefb302 + 59e05be commit 213971f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
13 changes: 6 additions & 7 deletions examples/mini-program-example/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,20 @@ export default function Index() {
</Button>
<Button
onClick={()=>{
nativeApi.harmonyNavigateTo({
indexHtmlPath: '/spa/new/index.html',
taroPath: 'pages/performance/index/index'
Taro.navigateTo({
url: 'pages/harmony-hybrid/mix-router/home/index'
})
}}>
多实例(多SPA跳转):性能列表页
多实例及混合路由测试页
</Button>
<Button
onClick={()=>{
nativeApi.harmonyNavigateTo({
nativeApi.navigateToTaroHybrid({
indexHtmlPath: '/spa/new/index.html',
taroPath: 'pages/api/index/index'
taroPath: '/pages/index/index'
})
}}>
单实例(单SPA):接口列表页
单实例(单SPA):首页
</Button>
</View>
)
Expand Down
13 changes: 7 additions & 6 deletions examples/mini-program-example/src/util/nativeApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// @ts-ignore
const decorator = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)
const sync = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)

// @proxyClassSign('')
class NativeApi {
// @ts-ignore
@decorator
harmonyNavigateTo(options: any) {
return options
}
@sync
navigateToTaroHybrid(_options: any) {}


@sync
navigateToNative(_options: any){}
}

const nativeApi = new NativeApi()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import {timeLog} from "./NativeApiLog";
import { syncApiCache } from './harmony-native/ApiCache'
import { asyncAndNotRelease, asyncAndRelease, syncAndRelease } from './harmony-native/ApiDecorator'
import { asyncAndNotRelease, asyncAndRelease, syncAndNotRelease, syncAndRelease } from './harmony-native/ApiDecorator'
import { storageCacheAndSyncProxy } from './harmony-native/StorageCacheAndSyncProxy'
import { NativeDataChangeListener, SyncCacheProxyHandler } from './NativeApiSyncCacheProxy'

Expand Down Expand Up @@ -627,6 +627,12 @@ export class NativeApi {
exitMiniProgram (option?: any): any {
return option
}

@(syncAndNotRelease)
onStorageStatusChange (_options: any): void {}

@(syncAndNotRelease)
offStorageStatusChange (_options: any): void {}
}

export interface Status {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export const syncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)

// @ts-ignore
// const syncAndNotRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: false }) || (target => target)
export const syncAndNotRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: false }) || (target => target)

// @ts-ignore
export const asyncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: true, autoRelease: true }) || (target => target)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const TYPE_STORAGE_UPDATE_EVENT_CLEAR = 'TYPE_STORAGE_UPDATE_EVENT_CLEAR'
class ProxyHandler {
private cacheMap: Map<any, any>
private pageHasShowed: boolean = false
Expand Down Expand Up @@ -28,6 +29,14 @@ class ProxyHandler {
})
}, 2000)
})

native.onStorageStatusChange((type: string, key: string) => {
if (type === TYPE_STORAGE_UPDATE_EVENT_CLEAR) {
this.cacheMap.clear()
} else {
this.cacheMap.delete(key)
}
})
}

get (target, propKey, receiver) {
Expand Down

0 comments on commit 213971f

Please sign in to comment.