Skip to content
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: Event center switch of an earlier version #1230

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/zh-cn/data.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


## 一、子应用获取来自主应用的数据

有两种方式获取来自主应用的数据:
Expand Down
29 changes: 23 additions & 6 deletions src/interact/event_center.ts
Original file line number Diff line number Diff line change
@@ -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<string, {
Expand Down Expand Up @@ -123,6 +124,7 @@ export default class EventCenter {
autoTrigger &&
Object.keys(eventInfo.data).length &&
(
microApp.options['event-center-legacy'] ||
!this.queue.includes(name) ||
this.isEqual(eventInfo.data, eventInfo.tempData)
)
Expand Down Expand Up @@ -168,19 +170,31 @@ export default class EventCenter {
public dispatch (
name: string,
data: Record<PropertyKey, unknown>,
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')
}

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,
Expand All @@ -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)
}
}

Expand Down
44 changes: 27 additions & 17 deletions src/interact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
logError,
getRootContainer,
} from '../libs/utils'
import microApp from '../micro_app'

const eventCenter = new EventCenter()

Expand Down Expand Up @@ -231,23 +232,32 @@ export class EventCenterForMicroApp extends EventCenterForGlobal {
dispatch (data: Record<PropertyKey, unknown>, 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<PropertyKey, unknown>, nextStep?: CallableFunction): void {
Expand Down
1 change: 1 addition & 0 deletions typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down