Skip to content

Commit

Permalink
feat: unsubscribe for module
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jul 16, 2024
1 parent f1b1998 commit 7187445
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { createCustomElement } from '@angular/elements'
import { createApplication, platformBrowser } from '@angular/platform-browser'
import {
EnvironmentInjector,
EnvironmentProviders,
Injector,
NgModuleRef,
Expand Down Expand Up @@ -33,8 +32,6 @@ export function bootstrapModule<M>(module: Type<M>, appType: AppType, production
.then((ref) => {
if (appType === 'shell') {
setShellZone(ref.injector)
} else if (appType === 'microfrontend') {
connectMicroFrontendRouter(ref.injector)
}
return ref
})
Expand Down Expand Up @@ -65,16 +62,23 @@ export async function bootstrapRemoteComponent(
createEntrypoint(component, elementName, app.injector, sub)
}

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

function createEntrypoint(
component: Type<any>,
elementName: string,
injector: EnvironmentInjector,
injector: Injector,
routerSub?: Subscription | null
) {
const originalNgDestroy = component.prototype.ngOnDestroy?.bind(component)
component.prototype.ngOnDestroy = () => {
routerSub?.unsubscribe()
originalNgDestroy()
if (originalNgDestroy !== undefined) {
originalNgDestroy()
}
}

const myRemoteComponentAsWebComponent = createCustomElement(component, {
Expand Down

0 comments on commit 7187445

Please sign in to comment.