Skip to content

Commit

Permalink
feat: router unsubscribe for remote components
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jul 16, 2024
1 parent cbc274b commit c2b4ec2
Showing 1 changed file with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createCustomElement } from '@angular/elements'
import { createApplication, platformBrowser } from '@angular/platform-browser'
import {
EnvironmentInjector,
EnvironmentProviders,
Injector,
NgModuleRef,
Expand All @@ -16,7 +17,7 @@ import {
import { Router } from '@angular/router'
import { getLocation } from '@onecx/accelerator'
import { EventsTopic } from '@onecx/integration-interface'
import { filter } from 'rxjs'
import { Subscription, filter } from 'rxjs'

/**
* Implementation inspired by @angular-architects/module-federation-plugin https://github.com/angular-architects/module-federation-plugin/blob/main/libs/mf-tools/src/lib/web-components/bootstrap-utils.ts
Expand Down Expand Up @@ -59,10 +60,25 @@ export async function bootstrapRemoteComponent(

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

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

export function createEntrypoint(
component: Type<any>,
elementName: string,
injector: EnvironmentInjector,
routerSub?: Subscription | null
) {
let originalNgDestroy = component.prototype.ngOnDestroy?.bind(component)

Check failure on line 74 in libs/angular-webcomponents/src/lib/utils/webcomponent-bootstrap.utils.ts

View workflow job for this annotation

GitHub Actions / main

'originalNgDestroy' is never reassigned. Use 'const' instead
component.prototype.ngOnDestroy = () => {
routerSub?.unsubscribe()
originalNgDestroy()
}

const myRemoteComponentAsWebComponent = createCustomElement(component, {
injector: app.injector,
injector: injector,
})

customElements.define(elementName, myRemoteComponentAsWebComponent)
Expand Down Expand Up @@ -124,25 +140,25 @@ function cachePlatform(production: boolean): PlatformRef {
return platform
}

function connectMicroFrontendRouter(injector: Injector, warn: boolean = true) {
function connectMicroFrontendRouter(injector: Injector, warn: boolean = true): Subscription | null {
const router = injector.get(Router)

if (!router) {
if (warn) {
console.warn('No router to connect found')
}
return
return null
}

connectRouter(router)
return connectRouter(router)
}

function connectRouter(router: Router): void {
function connectRouter(router: Router): Subscription {
const initialUrl = `${location.pathname.substring(getLocation().deploymentPath.length)}${location.search}`
router.navigateByUrl(initialUrl)
let lastUrl = initialUrl
const observer = new EventsTopic()
observer.pipe(filter((e) => e.type === 'navigated')).subscribe(() => {
return observer.pipe(filter((e) => e.type === 'navigated')).subscribe(() => {
const routerUrl = `${location.pathname.substring(getLocation().deploymentPath.length)}${location.search}`
if (routerUrl !== lastUrl) {
lastUrl = routerUrl
Expand Down

0 comments on commit c2b4ec2

Please sign in to comment.