From 555024d73c4bb108dcc6de60069f1927b54d82a6 Mon Sep 17 00:00:00 2001 From: zhenyuWang <13641039885@163.com> Date: Wed, 4 Sep 2024 20:03:49 +0800 Subject: [PATCH] =?UTF-8?q?refactor(UniApp):=20=E4=BC=98=E5=8C=96=20eventB?= =?UTF-8?q?us?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../uni-app-plus/src/x/framework/app/index.ts | 22 ++++-------------- packages/uni-h5/src/framework/setup/app.ts | 23 ++++--------------- 2 files changed, 8 insertions(+), 37 deletions(-) diff --git a/packages/uni-app-plus/src/x/framework/app/index.ts b/packages/uni-app-plus/src/x/framework/app/index.ts index 5145bcdbfd..522c6bb99a 100644 --- a/packages/uni-app-plus/src/x/framework/app/index.ts +++ b/packages/uni-app-plus/src/x/framework/app/index.ts @@ -16,32 +16,18 @@ import { initService } from './initService' import { setNativeApp } from './app' import { initComponentInstance } from './initComponentInstance' import type { - EventBus, NavigateToOptions, SwitchTabOptions, } from '@dcloudio/uni-app-x/types/uni' -import { UniEventBus } from '@dcloudio/uni-api' +import type { UniApp } from '@dcloudio/uni-app-x/types/app' +import { EventBus } from '@dcloudio/uni-api' let appCtx: ComponentPublicInstance const defaultApp = { globalData: {}, } -class UniApp implements EventBus { - private $eventBus = new UniEventBus() - on = (eventName: string, callback: Function) => { - this.$eventBus.on(eventName, callback) - } - once = (eventName: string, callback: Function) => { - this.$eventBus.once(eventName, callback) - } - off = (eventName?: string, callback?: Function | null) => { - this.$eventBus.off(eventName, callback) - } - emit = (eventName: string, ...args: any[]) => { - this.$eventBus.emit(eventName, ...args) - } - +class UniAppImpl extends EventBus implements UniApp { get vm() { return appCtx } @@ -53,7 +39,7 @@ class UniApp implements EventBus { } } -let $uniApp = new UniApp() +let $uniApp = new UniAppImpl() export const entryPageState = { isReady: false, diff --git a/packages/uni-h5/src/framework/setup/app.ts b/packages/uni-h5/src/framework/setup/app.ts index bff74246ab..1c4b5fa355 100644 --- a/packages/uni-h5/src/framework/setup/app.ts +++ b/packages/uni-h5/src/framework/setup/app.ts @@ -7,28 +7,13 @@ import { initService, initView, } from '@dcloudio/uni-core' -import { UniEventBus } from '@dcloudio/uni-api' -import type { EventBus } from '@dcloudio/uni-app-x/types/uni' +import { EventBus } from '@dcloudio/uni-api' +import type { UniApp } from '@dcloudio/uni-app-x/types/app' let appVm: ComponentPublicInstance -// @ts-expect-error let $uniApp: UniApp if (__X__) { - class UniApp implements EventBus { - private $eventBus = new UniEventBus() - on = (eventName: string, callback: Function) => { - this.$eventBus.on(eventName, callback) - } - once = (eventName: string, callback: Function) => { - this.$eventBus.once(eventName, callback) - } - off = (eventName?: string, callback?: Function | null) => { - this.$eventBus.off(eventName, callback) - } - emit = (eventName: string, ...args: any[]) => { - this.$eventBus.emit(eventName, ...args) - } - + class UniAppImpl extends EventBus implements UniApp { get vm() { return appVm } @@ -39,7 +24,7 @@ if (__X__) { return appVm?.globalData || {} } } - $uniApp = new UniApp() + $uniApp = new UniAppImpl() } export function getApp() {