Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: nativeApi同步获取Native数据支持缓存 #15533

Open
wants to merge 3 commits into
base: 3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions examples/mini-program-example/src/pages/index/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@ export default function Index() {
}}>
单实例(单SPA):接口列表页
</Button>

<Button
onClick={()=>{
const time1 = new Date().getTime();
const info = Taro.getSystemSetting();
const time2 = new Date().getTime();
const diff = time2-time1
console.log('getSystemSetting:', info["wifiEnabled"]+":"+diff)
Taro.showToast({
title: JSON.stringify(info["wifiEnabled"]+":"+diff),
})

}}>
测试接口缓存
</Button>
</View>
)
}
140 changes: 33 additions & 107 deletions packages/taro-platform-harmony-hybrid/src/api/apis/NativeApi.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import osChannelApi from './osChannelApi'
import { RequestTask } from './request'

import { HybridProxy } from './NativeApiHybridProxy'
import { NativeDataChangeListener, SyncCacheProxyHandler } from './NativeApiSyncCacheProxy'
// @ts-ignore
const syncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: false, autoRelease: true }) || (target => target)
// @ts-ignore
Expand All @@ -10,8 +9,14 @@ const asyncAndRelease = window.MethodChannel && window.MethodChannel.jsBridgeMod
// @ts-ignore
const asyncAndNotRelease = window.MethodChannel && window.MethodChannel.jsBridgeMode({ isAsync: true, autoRelease: false }) || (target => target)

export let judgeUseAxios = false
class NativeApi {
export class NativeApi {
// @ts-ignore
@(syncAndNotRelease)
// @ts-ignore
// eslint-disable-next-line @typescript-eslint/no-unused-vars
registerNativeListener (listener: NativeDataChangeListener | null): void {
}

// @ts-ignore
@(syncAndRelease)
getWindowInfo (): any {
Expand Down Expand Up @@ -798,114 +803,35 @@ class NativeApi {
}
}

export interface Status {
done: boolean
data: string
errorMsg: string
}
export class ProxyChain {
private target: any

class CacheStorageProxy {
private cacheMap: Map<any, any>
private readonly nativeApi: NativeApi
private readonly asyncToSyncProxy: any

constructor (nativeApi: NativeApi) {
this.nativeApi = nativeApi
this.cacheMap = new Map<string, any>()
this.asyncToSyncProxy = new Proxy(nativeApi, new AsyncToSyncProxy(this.nativeApi))
}

// @ts-ignore
get (target: { [x: string]: any }, prop: string) {
if (prop === 'getStorageSync') {
return (...args: any[]) => {
const key = args[0].key
if (this.cacheMap.has(key)) {
return this.cacheMap.get(key)
} else {
const status = this.asyncToSyncProxy.getStorageSync({ key })
if (status.done && status.errMsg === '') {
this.cacheMap.set(key, status)
}
return status
}
}
}
if (prop === 'setStorageSync') {
return (...args: any[]) => {
const { key, data } = args[0]
const status = this.asyncToSyncProxy.setStorageSync({ key, data })
if (status.done && status.errMsg === '') {
this.cacheMap.set(key, status)
}
return status
}
}
return (...args: any[]) => {
return this.asyncToSyncProxy[prop](...args)
}
constructor (target: object) {
this.target = target
}
}

class AsyncToSyncProxy {
private readonly nativeApi: NativeApi
private readonly STATUS: Status = { done: false, data: '', errorMsg: `search timeout` }
private methods = ['setStorageSync', 'removeStorageSync', 'getStorageSync', 'getStorageInfoSync', 'clearStorageSync']

constructor (nativeApi: NativeApi) {
this.nativeApi = nativeApi
}

get (target: { [x: string]: any }, prop: string) {
if (this.methods.includes(prop)) {
return (...args: any[]) => {
const asyncFunc = prop.substring(0, prop.length - 'Sync'.length)
this.nativeApi[asyncFunc](...args)

let count = 0
while (count < 20000) {
count++
if (count % 2000 === 0) {
const status = this.nativeApi.getExecStatus({ method: prop, key: args[0].key })
if (status.done || status.errorMsg) {
return status
}
}
}
return this.STATUS
}
}
return target[prop]
// 添加一个新的Proxy处理器
addHandler (handler: (target: any) => ProxyHandler<any>): ProxyChain {
const h = handler(this.target)
this.target = new Proxy(this.target, h)
return this
}
}

class HybridProxy {
private readonly useAxios: boolean
private readonly useOsChannel: boolean
private readonly cacheProxy: any
private readonly requestApi = 'request'

constructor (useAxios: boolean, useOsChannel: boolean, nativeApi: NativeApi) {
this.useAxios = useAxios
this.useOsChannel = useOsChannel
this.cacheProxy = new Proxy(nativeApi, new CacheStorageProxy(nativeApi))
}

get (_target: any, prop: string) {
return (...args: any) => {
if (this.useAxios && prop === this.requestApi) {
judgeUseAxios = this.useAxios
// @ts-ignore
return new RequestTask(...args)
}
if (this.useOsChannel && osChannelApi.hasOwnProperty(prop)) {
return osChannelApi[prop](...args)
}
return this.cacheProxy[prop](...args)
}
// 创建并获取最终的Proxy对象
getProxy (): any {
return this.target
}
}

const nativeApi = new NativeApi()
const native = new Proxy(nativeApi, new HybridProxy(false, false, nativeApi)) // 第一个false是默认走jsb,true是走纯js, 第二个false是不走osChannel
/**
* 链式Proxy
* 通过[addHandler]添加ProxyHandler
* 通过[getProxy]获取最终的target
*/
const native = new ProxyChain(new NativeApi())
.addHandler((target) => new SyncCacheProxyHandler(target))
// HybridProxy第一个false是默认走jsb,true是走纯js, 第二个false是不走osChannel
.addHandler((target) => new HybridProxy(false, false, target))
.getProxy()

export default native
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { NativeApi } from './NativeApi'
import osChannelApi from './osChannelApi'
import { RequestTask } from './request'

export let judgeUseAxios = false

export interface Status {
done: boolean
data: string
errorMsg: string
}

class CacheStorageProxy {
private cacheMap: Map<any, any>
private readonly nativeApi: NativeApi
private readonly asyncToSyncProxy: any

constructor (nativeApi: NativeApi) {
this.nativeApi = nativeApi
this.cacheMap = new Map<string, any>()
this.asyncToSyncProxy = new Proxy(nativeApi, new AsyncToSyncProxy(this.nativeApi))
}

// @ts-ignore
get (target: { [x: string]: any }, prop: string) {
if (prop === 'getStorageSync') {
return (...args: any[]) => {
const key = args[0].key
if (this.cacheMap.has(key)) {
return this.cacheMap.get(key)
} else {
const status = this.asyncToSyncProxy.getStorageSync({ key })
if (status.done && status.errMsg === '') {
this.cacheMap.set(key, status)
}
return status
}
}
}
if (prop === 'setStorageSync') {
return (...args: any[]) => {
const { key, data } = args[0]
const status = this.asyncToSyncProxy.setStorageSync({ key, data })
if (status.done && status.errMsg === '') {
this.cacheMap.set(key, status)
}
return status
}
}
return (...args: any[]) => {
return this.asyncToSyncProxy[prop](...args)
}
}
}

class AsyncToSyncProxy {
private readonly nativeApi: NativeApi
private readonly STATUS: Status = { done: false, data: '', errorMsg: `search timeout` }
private methods = ['setStorageSync', 'removeStorageSync', 'getStorageSync', 'getStorageInfoSync', 'clearStorageSync']

constructor (nativeApi: NativeApi) {
this.nativeApi = nativeApi
}

get (target: { [x: string]: any }, prop: string) {
if (this.methods.includes(prop)) {
return (...args: any[]) => {
const asyncFunc = prop.substring(0, prop.length - 'Sync'.length)
this.nativeApi[asyncFunc](...args)

let count = 0
while (count < 20000) {
count++
if (count % 2000 === 0) {
const status = this.nativeApi.getExecStatus({ method: prop, key: args[0].key })
if (status.done || status.errorMsg) {
return status
}
}
}
return this.STATUS
}
}
return target[prop]
}
}

export class HybridProxy {
private readonly useAxios: boolean
private readonly useOsChannel: boolean
private readonly cacheProxy: any
private readonly requestApi = 'request'

constructor (useAxios: boolean, useOsChannel: boolean, nativeApi: NativeApi) {
this.useAxios = useAxios
this.useOsChannel = useOsChannel
this.cacheProxy = new Proxy(nativeApi, new CacheStorageProxy(nativeApi))
}

get (_target: any, prop: string) {
return (...args: any) => {
if (this.useAxios && prop === this.requestApi) {
judgeUseAxios = this.useAxios
// @ts-ignore
return new RequestTask(...args)
}
if (this.useOsChannel && osChannelApi.hasOwnProperty(prop)) {
return osChannelApi[prop](...args)
}
return this.cacheProxy[prop](...args)
}
}
}
Loading