-
Notifications
You must be signed in to change notification settings - Fork 20
增加支持客户端的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() { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The loadEventCenter method lacks protection against concurrent calls. If multiple methods are called simultaneously before the eventCenter is loaded, this could result in multiple import attempts. Consider adding a loading flag or using a singleton promise to prevent race conditions. Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
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) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The parameter 'method' should have a type annotation. Consider adding ': string' to specify the expected parameter type.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
return (...args) => { | ||||||
if (this.ready) { | ||||||
return this.eventCenter[method](...args) | ||||||
} | ||||||
|
||||||
return new Promise((resolve, reject) => { | ||||||
this.pendingCalls.push({ method, args, resolve, reject }) | ||||||
const loadEventCenter = promisify( | ||||||
limited.async('loadEventCenter', this.loadEventCenter) | ||||||
) | ||||||
loadEventCenter() // 触发加载 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
}) | ||||||
} | ||||||
} | ||||||
|
||||||
// 代理方法 | ||||||
on = this.createProxyMethod('on') | ||||||
off = this.createProxyMethod('off') | ||||||
trigger = this.createProxyMethod('trigger') | ||||||
once = this.createProxyMethod('once') | ||||||
} | ||||||
|
||||||
// 导出代理类和单例实例 | ||||||
export const eventCenter = EventCenterProxy.getInstance() |
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.
Copilot uses AI. Check for mistakes.