-
Notifications
You must be signed in to change notification settings - Fork 21
增加支持客户端的eventCenter代理类,解决从@tarojs/taro导入的eventCenter为空的问题 #47
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | ||||||
| /** | ||||||
| * Taro事件中心代理类 | ||||||
| * 实现客户端懒加载@tarojs/runtime中的eventCenter | ||||||
| * 解决从@tarojs/taro导入的eventCenter为空的问题 | ||||||
| */ | ||||||
| import promisify from 'mpromisify' | ||||||
| import { limited } from '../_util' | ||||||
|
|
||||||
| // 单例实例 | ||||||
| let instance: EventCenterProxy | ||||||
|
|
||||||
| interface EventCenterProxy { | ||||||
| ready: boolean | ||||||
| eventCenter: any | ||||||
| pendingCalls: any[] | ||||||
| } | ||||||
|
|
||||||
| class EventCenterProxy { | ||||||
| constructor() { | ||||||
| if (instance) { | ||||||
| return instance | ||||||
| } | ||||||
|
|
||||||
| this.ready = false | ||||||
| this.eventCenter = null | ||||||
| this.pendingCalls = [] | ||||||
| // eslint-disable-next-line @typescript-eslint/no-this-alias | ||||||
| instance = this | ||||||
| } | ||||||
|
|
||||||
| // 获取单例实例 | ||||||
| static getInstance() { | ||||||
| if (!instance) { | ||||||
| instance = new EventCenterProxy() | ||||||
| } | ||||||
| return instance | ||||||
| } | ||||||
|
|
||||||
| // 加载真实eventCenter | ||||||
| async loadEventCenter() { | ||||||
|
||||||
| try { | ||||||
| const { eventCenter } = await import('@tarojs/runtime') | ||||||
| this.eventCenter = eventCenter | ||||||
| this.ready = true | ||||||
|
|
||||||
| // 执行所有待处理的调用 | ||||||
| this.pendingCalls.forEach(({ method, args, resolve }) => { | ||||||
| resolve(this.eventCenter[method](...args)) | ||||||
| }) | ||||||
| this.pendingCalls = [] | ||||||
| } catch (error) { | ||||||
| console.error('加载eventCenter失败:', error) | ||||||
| // 失败时也执行待处理调用,避免阻塞 | ||||||
| this.pendingCalls.forEach(({ reject }) => reject(error)) | ||||||
| this.pendingCalls = [] | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| // 创建代理方法 | ||||||
| createProxyMethod(method) { | ||||||
|
||||||
| createProxyMethod(method) { | |
| createProxyMethod(method: string) { |
Copilot
AI
Sep 10, 2025
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.
The loadEventCenter promise is created but not awaited or handled. This could lead to unhandled promise rejections if the loading fails. Consider using 'loadEventCenter().catch(() => {})' or properly handling the promise.
| loadEventCenter() // 触发加载 | |
| loadEventCenter().catch(() => {}) // 触发加载,避免未处理的promise拒绝 |
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.
The interface uses 'any' types which reduces type safety. Consider defining more specific types for 'eventCenter' and 'pendingCalls' to improve code maintainability and catch potential runtime errors.