Skip to content

Commit

Permalink
fix: resubscribe on component or module init
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jul 17, 2024
1 parent f2c7081 commit 20ec4bb
Showing 1 changed file with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Subscription, filter } from 'rxjs'
*/

export type AppType = 'shell' | 'microfrontend'
export type EntrypointType = 'microfrontend' | 'component'

export function bootstrapModule<M>(module: Type<M>, appType: AppType, production: boolean): Promise<NgModuleRef<M>> {
return cachePlatform(production)
Expand Down Expand Up @@ -57,27 +58,32 @@ export async function bootstrapRemoteComponent(

cachePlatform(production)
adaptRemoteComponentRoutes(app.injector)
const sub = connectMicroFrontendRouter(app.injector, false)

createEntrypoint(component, elementName, app.injector, sub)
createEntrypoint(component, elementName, app.injector, 'component')
}

export function createAppEntrypoint(component: Type<any>, elementName: string, injector: Injector) {
const sub = connectMicroFrontendRouter(injector)
createEntrypoint(component, elementName, injector, sub)
createEntrypoint(component, elementName, injector, 'microfrontend')
}

function createEntrypoint(
component: Type<any>,
elementName: string,
injector: Injector,
routerSub?: Subscription | null
entrypointType: EntrypointType
) {
const originalNgDestroy = component.prototype.ngOnDestroy?.bind(component)
component.prototype.ngOnDestroy = () => {
routerSub?.unsubscribe()
let sub: Subscription | null
const originalNgInit = component.prototype.ngOnInit
component.prototype.ngOnInit = function () {
sub = connectMicroFrontendRouter(injector, entrypointType === 'microfrontend')
if (originalNgInit !== undefined) {
originalNgInit.call(this)
}
}
const originalNgDestroy = component.prototype.ngOnDestroy
component.prototype.ngOnDestroy = function () {
sub?.unsubscribe()
if (originalNgDestroy !== undefined) {
originalNgDestroy()
originalNgDestroy.call(this)
}
}

Expand Down

0 comments on commit 20ec4bb

Please sign in to comment.