From 28f9ff64033f52ed9b91e2d7c0ae1f387e3c90f2 Mon Sep 17 00:00:00 2001 From: front-refined <229298746@qq.com> Date: Fri, 24 May 2024 10:31:54 +0800 Subject: [PATCH] feat: Event center switch of an earlier version --- docs/zh-cn/data.md | 11 +++++++++ src/interact/event_center.ts | 29 +++++++++++++++++++----- src/interact/index.ts | 44 ++++++++++++++++++++++-------------- typings/global.d.ts | 1 + 4 files changed, 62 insertions(+), 23 deletions(-) diff --git a/docs/zh-cn/data.md b/docs/zh-cn/data.md index 7371c3368..759ccc38f 100644 --- a/docs/zh-cn/data.md +++ b/docs/zh-cn/data.md @@ -5,6 +5,17 @@ 同时我们也提供了全局通信,方便跨应用之间的数据通信。 +> [!NOTE] +> 如果需要保留 0.x 的数据通信行为,我们提供了全局配置 +> ```js +> import microApp from '@micro-zoe/micro-app' +> microApp.start({ +> 'event-center-legacy': true, // 全局配置 0.x 数据通信行为,默认为false +> }) +> ``` +> [0.x 数据通信文档](https://micro-zoe.github.io/micro-app/0.x/#/zh-cn/data) + + ## 一、子应用获取来自主应用的数据 有两种方式获取来自主应用的数据: diff --git a/src/interact/event_center.ts b/src/interact/event_center.ts index 0061df629..e47660a28 100755 --- a/src/interact/event_center.ts +++ b/src/interact/event_center.ts @@ -1,6 +1,7 @@ /* eslint-disable no-cond-assign */ import { CallableFunctionForInteract, AppName } from '@micro-app/types' import { logError, isFunction, isPlainObject, assign, defer } from '../libs/utils' +import microApp from '../micro_app' export default class EventCenter { public eventList = new Map, - nextStep: CallableFunction, + nextStep?: CallableFunction, force?: boolean, dispatchDataEvent?: CallableFunction, ): void { + const eventCenterLegacy = microApp.options['event-center-legacy'] if (this.isLegalName(name)) { if (!isPlainObject(data)) { return logError('event-center: data must be object') @@ -179,8 +182,19 @@ export default class EventCenter { let eventInfo = this.eventList.get(name) if (eventInfo) { - eventInfo.tempData = assign({}, eventInfo.tempData || eventInfo.data, data) - !eventInfo.force && (eventInfo.force = !!force) + if (!eventCenterLegacy) { + eventInfo.tempData = assign({}, eventInfo.tempData || eventInfo.data, data) + !eventInfo.force && (eventInfo.force = !!force) + } else { + // keep 0.x behavior + // Update when the data is not equal + if (eventInfo.data !== data) { + eventInfo.data = data + for (const f of eventInfo.callbacks) { + f(data) + } + } + } } else { eventInfo = { data: data, @@ -190,10 +204,13 @@ export default class EventCenter { /** * When sent data to parent, eventInfo probably does not exist, because parent may listen to datachange */ - eventInfo.force = true + !eventCenterLegacy && (eventInfo.force = true) + } + + if (!eventCenterLegacy && nextStep) { + // add to queue, event eventInfo is null + this.enqueue(name, nextStep, dispatchDataEvent) } - // add to queue, event eventInfo is null - this.enqueue(name, nextStep, dispatchDataEvent) } } diff --git a/src/interact/index.ts b/src/interact/index.ts index 834a9bec5..63765fc1e 100644 --- a/src/interact/index.ts +++ b/src/interact/index.ts @@ -10,6 +10,7 @@ import { logError, getRootContainer, } from '../libs/utils' +import microApp from '../micro_app' const eventCenter = new EventCenter() @@ -231,23 +232,32 @@ export class EventCenterForMicroApp extends EventCenterForGlobal { dispatch (data: Record, nextStep?: CallableFunction, force?: boolean): void { removeDomScope() - eventCenter.dispatch( - createEventName(this.appName, false), - data, - (resArr: unknown[]) => isFunction(nextStep) && nextStep(resArr), - force, - () => { - const app = appInstanceMap.get(this.appName) - if (app?.container && isPlainObject(data)) { - const event = new CustomEvent('datachange', { - detail: { - data: eventCenter.getData(createEventName(this.appName, false)) - } - }) - - getRootContainer(app.container).dispatchEvent(event) - } - }) + const dispatchDataEvent = () => { + const app = appInstanceMap.get(this.appName) + if (app?.container && isPlainObject(data)) { + const event = new CustomEvent('datachange', { + detail: { + data: eventCenter.getData(createEventName(this.appName, false)) + } + }) + + getRootContainer(app.container).dispatchEvent(event) + } + } + + if (!microApp.options['event-center-legacy']) { + eventCenter.dispatch( + createEventName(this.appName, false), + data, + (resArr: unknown[]) => isFunction(nextStep) && nextStep(resArr), + force, + () => { + dispatchDataEvent() + }) + } else { + eventCenter.dispatch(createEventName(this.appName, false), data) + dispatchDataEvent() + } } forceDispatch (data: Record, nextStep?: CallableFunction): void { diff --git a/typings/global.d.ts b/typings/global.d.ts index 8be122fdf..91d00c45d 100644 --- a/typings/global.d.ts +++ b/typings/global.d.ts @@ -338,6 +338,7 @@ declare module '@micro-app/types' { 'disable-sandbox'?: boolean 'disable-memory-router'?: boolean 'disable-patch-request'?: boolean + 'event-center-legacy'?: boolean 'keep-router-state'?: boolean 'hidden-router'?: boolean 'keep-alive'?: boolean