-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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: taro同步方法支持缓存,并且支持外部注册和扩展 #15599
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
packages/taro-platform-harmony-hybrid/src/api/apis/NativeApiSyncCacheProxy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
import { NativeApi } from './NativeApi' | ||
|
||
export class NativeDataCache { | ||
private cache = new Map<string, any>() | ||
|
||
keys (): string[] { | ||
return Array.from(this.cache.keys()) | ||
} | ||
|
||
set (key: string, value: any) { | ||
this.cache.set(key, value) | ||
} | ||
|
||
get (key: string) { | ||
return this.cache.get(key) | ||
} | ||
|
||
delete (key: string): boolean { | ||
return this.cache.delete(key) | ||
} | ||
|
||
has (key: string): boolean { | ||
return this.cache.has(key) | ||
} | ||
} | ||
|
||
/** | ||
* 系统数据更新监听器 | ||
*/ | ||
export interface NativeDataChangeListener { | ||
/** | ||
* 更新 | ||
* @param methodName 要更新的方法名 | ||
* @param methodArgs 要更新的方法参数,如果是空参,直接传[] | ||
*/ | ||
change: (methodName: string, methodArgs: any[]) => void | ||
/** | ||
* 注册 | ||
* @param methodName 要注册的方法名 | ||
*/ | ||
register: (methodName: string) => void | ||
/** | ||
* 解注册 | ||
* @param methodName 要解注册的方法名 | ||
*/ | ||
unregister:(methodName: string) => void | ||
|
||
} | ||
|
||
/** | ||
* 同步数据缓存ProxyHandler | ||
*/ | ||
export class SyncCacheProxyHandler { | ||
private readonly nativeApi: NativeApi | ||
private listener: NativeDataChangeListener | null = null | ||
private readonly cache: NativeDataCache | ||
private enableMethodNames: Set<string> = new Set<string>() | ||
|
||
constructor (nativeApi: NativeApi) { | ||
this.nativeApi = nativeApi | ||
this.cache = new NativeDataCache() | ||
// 绑定类的this到self变量 | ||
const self = this | ||
this.on({ | ||
register (methodName: string): void { | ||
self.enableMethodNames.add(methodName) | ||
}, | ||
unregister (methodName: string): void { | ||
self.enableMethodNames.delete(methodName) | ||
}, | ||
change (methodName: string, methodArgs: any[]): void { | ||
if (self.enableMethodNames.has(methodName)) { | ||
self.updateNativeData(self.cache, methodName, methodArgs) | ||
} | ||
} | ||
}) | ||
} | ||
|
||
/** | ||
* 监听Native数据变化 | ||
* @param listener 监听器 | ||
*/ | ||
on (listener: NativeDataChangeListener) { | ||
this.listener = listener | ||
this.nativeApi.registerNativeListener(this.listener) | ||
} | ||
|
||
/** | ||
* 更新Native数据 | ||
* @param cache 缓存数据 | ||
* @param methodName 要更新的方法名 | ||
* @param methodArgs 要更新的方法参数 | ||
*/ | ||
updateNativeData (cache: NativeDataCache, methodName: string, methodArgs: any[]) { | ||
const cacheKey = this.generateCacheKey(methodName, methodArgs) | ||
// 删除该key对应的数据 | ||
cache.delete(cacheKey) | ||
// 获取到methodName对应的方法 | ||
const fun = (this.nativeApi as any)[methodName] as (...args: any[]) => any | ||
// 方法存在,可以安全调用,并传入参数 | ||
if (typeof fun === 'function') { | ||
const result = fun(...methodArgs) | ||
// 结果存入缓存 | ||
if (result) { | ||
this.cache.set(cacheKey, result) | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* 生成cache的存储key | ||
* @param methodName 方法名 | ||
* @param methodArgs 方法参数 | ||
*/ | ||
generateCacheKey (methodName: string, methodArgs: any[]): string { | ||
return `${methodName}_${JSON.stringify(methodArgs)}` | ||
} | ||
|
||
get (target: NativeApi, propKey: string | symbol, receiver: any) { | ||
const origMethod = Reflect.get(target, propKey, receiver) | ||
const methodName = `${String(propKey)}` | ||
if (typeof origMethod === 'function' && this.enableMethodNames.has(methodName)) { | ||
return (...args: any[]) => { | ||
const cacheKey = this.generateCacheKey(`${String(propKey)}`, args) | ||
if (this.cache.has(cacheKey)) { | ||
return this.cache.get(cacheKey) | ||
} else { | ||
const result = origMethod.apply(target, args) | ||
if (result) { | ||
// 有效值才存入,null或者undefined存入cache没有意义 | ||
this.cache.set(cacheKey, result) | ||
} | ||
return result | ||
} | ||
} | ||
} | ||
return origMethod | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改为支持数组,减少通信次数