Skip to content

Commit 5b3e463

Browse files
committed
feat: 完善name属性change和set __MICRO_APP_STATE__能力&修复1533
1 parent 64fe26c commit 5b3e463

File tree

5 files changed

+33
-10
lines changed

5 files changed

+33
-10
lines changed

src/create_app.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,9 @@ export default class CreateApp implements AppInterface {
240240

241241
this.createSandbox()
242242

243-
// place outside of nextAction, as nextAction may execute async
244-
this.setAppState(appStates.BEFORE_MOUNT)
245-
246243
const nextAction = () => {
244+
// place inside of nextAction, make sure iframe ready
245+
this.setAppState(appStates.BEFORE_MOUNT)
247246
/**
248247
* Special scenes:
249248
* 1. mount before prerender exec mount (loading source)

src/interact/event_center.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
/* eslint-disable no-cond-assign */
22
import { CallableFunctionForInteract, AppName } from '@micro-app/types'
33
import { logError, isFunction, isPlainObject, assign, defer } from '../libs/utils'
4+
interface IEventInfo {
5+
data: Record<PropertyKey, unknown>
6+
tempData?: Record<PropertyKey, unknown> | null
7+
force?: boolean
8+
callbacks: Set<CallableFunctionForInteract>
9+
}
410

511
export default class EventCenter {
6-
public eventList = new Map<string, {
7-
data: Record<PropertyKey, unknown>,
8-
tempData?: Record<PropertyKey, unknown> | null,
9-
force?: boolean,
10-
callbacks: Set<CallableFunctionForInteract>,
11-
}>()
12+
public eventList = new Map<string, IEventInfo | undefined>()
1213

1314
// whether the name is legal
1415
private isLegalName (name: string): boolean {

src/interact/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,15 @@ export class EventCenterForBaseApp extends EventCenterForGlobal {
183183
clearDataListener (appName: string): void {
184184
eventCenter.off(createEventName(formatAppName(appName), false))
185185
}
186+
187+
changeEventAppName (newAppName: string, oldAppName: string): void {
188+
const newEventName = createEventName(formatAppName(newAppName), true)
189+
const oldEventName = createEventName(formatAppName(oldAppName), true)
190+
if (eventCenter.eventList.has(oldEventName)) {
191+
eventCenter.eventList.set(newEventName, eventCenter.eventList.get(oldEventName))
192+
eventCenter.eventList.delete(oldEventName)
193+
}
194+
}
186195
}
187196

188197
// Event center for sub app

src/micro_app_element.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export function defineElement (tagName: string): void {
5151
private cacheData: Record<PropertyKey, unknown> | null = null
5252
private connectedCount = 0
5353
private connectStateMap: Map<number, boolean> = new Map()
54-
public appName = '' // app name
54+
private _appName = '' // app name
5555
public appUrl = '' // app url
5656
public ssrUrl = '' // html path in ssr mode
5757
public version = version
@@ -441,6 +441,7 @@ export function defineElement (tagName: string): void {
441441
unmountcb,
442442
})
443443
}
444+
delete this.__MICRO_APP_NAME__
444445
}
445446

446447
// hidden app when disconnectedCallback called with keep-alive
@@ -634,6 +635,17 @@ export function defineElement (tagName: string): void {
634635
return null
635636
}
636637

638+
set appName(value: string) {
639+
if (value !== this._appName) {
640+
microApp.changeEventAppName(value, this._appName)
641+
this._appName = value
642+
}
643+
}
644+
645+
get appName(): string {
646+
return this._appName
647+
}
648+
637649
/**
638650
* get publicPath from a valid address,it can used in micro-app-devtools
639651
*/

src/sandbox/iframe/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ export default class IframeSandbox {
101101
this.microAppWindow = this.iframe!.contentWindow
102102

103103
this.patchIframe(this.microAppWindow, (resolve: CallableFunction) => {
104+
// refresh
105+
this.microAppWindow = this.iframe!.contentWindow
104106
// create new html to iframe
105107
this.createIframeTemplate(this.microAppWindow)
106108
// get escapeProperties from plugins

0 commit comments

Comments
 (0)