Skip to content

Commit

Permalink
fix: fixed translation cache loading
Browse files Browse the repository at this point in the history
  • Loading branch information
markuczy committed Jul 11, 2024
1 parent bf0cc8c commit 28ba600
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,19 @@ class TranslationCacheTopic extends SyncableTopic<Record<string, any>> {

@Injectable({ providedIn: 'root' })
export class TranslationCacheService implements OnDestroy {
private id: string = ''

Check failure on line 16 in libs/angular-accelerator/src/lib/services/translation-cache.service.ts

View workflow job for this annotation

GitHub Actions / main

Type string trivially inferred from a string literal, remove type annotation
private translationTopic$ = new TranslationCacheTopic()
private translations$ = new BehaviorSubject<any>({})
constructor() {
this.translationTopic$
.pipe(
filter((message) => message['id'] === this.id),
withLatestFrom(this.translations$),
map(([topicTranslations, translations]) => {
let foundValueOthersDoNotKnow = false
const newTranslations = { ...translations }
Object.keys(topicTranslations).forEach((k) => {
if (!topicTranslations[k] && translations[k]) {
if (!topicTranslations[k] && translations[k] && k !== this.id) {
foundValueOthersDoNotKnow = true
}
newTranslations[k] ??= topicTranslations[k]
Expand All @@ -43,19 +45,23 @@ export class TranslationCacheService implements OnDestroy {
this.translationTopic$.destroy()
}

setId(id: string) {
this.id = 'translation-'.concat(id)
}

getTranslationFile(url: string, cacheMissFunction: () => Observable<any>): Observable<any> {
if (this.translations$.value[url]) {
return of(this.translations$.value[url])
}
this.translationTopic$.publish({ ...this.translations$.value, [url]: null })
this.translationTopic$.publish({ ...this.translations$.value, [url]: null, id: this.id })
return race(
this.translations$.pipe(
filter((t) => t[url]),
map((t) => t[url])
),
cacheMissFunction().pipe(
tap((t) => {
this.translationTopic$.publish({ ...this.translations$.value, [url]: t })
this.translationTopic$.publish({ ...this.translations$.value, [url]: t, id: this.id })
})
)
).pipe(first())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Location } from '@angular/common'
import { HttpClient } from '@angular/common/http'
import { inject } from '@angular/core'
import { inject, InjectionToken } from '@angular/core'
import { TranslateLoader } from '@ngx-translate/core'
import { map, ReplaySubject, tap } from 'rxjs'
import { TranslationCacheService } from '../services/translation-cache.service'
Expand All @@ -10,14 +10,18 @@ import { TranslateCombinedLoader } from './translate.combined.loader'

let lastTranslateLoaderTimerId = 0

export const REMOTE_COMPONENT_ID = new InjectionToken<string>('REMOTE_COMPONENT_ID')

export function createRemoteComponentTranslateLoader(
http: HttpClient,
baseUrlReplaySubject$: ReplaySubject<string>,
remoteComponentId: string,
translationCacheService?: TranslationCacheService
): TranslateLoader {
const ts = translationCacheService ?? inject(TranslationCacheService)
ts.setId(remoteComponentId)
const timerId = lastTranslateLoaderTimerId++

console.time('createRemoteComponentTranslateLoader_' + timerId)
return new AsyncTranslateLoader(
baseUrlReplaySubject$.pipe(
Expand Down
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 {
ApplicationRef,

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

View workflow job for this annotation

GitHub Actions / main

'ApplicationRef' is defined but never used
EnvironmentProviders,
Injector,
NgModuleRef,
Expand Down Expand Up @@ -75,11 +76,11 @@ function adaptRemoteComponentRoutes(injector: Injector) {
return
}

if(!router.config.find((val) => val.path === '**')) {
if (!router.config.find((val) => val.path === '**')) {
router.resetConfig(
router.config.concat({
path: '**',
children: []
children: [],
})
)
}
Expand Down Expand Up @@ -140,11 +141,15 @@ function connectMicroFrontendRouter(injector: Injector, warn: boolean = true) {
function connectRouter(router: Router): void {
const initialUrl = `${location.pathname.substring(getLocation().deploymentPath.length)}${location.search}`
router.navigateByUrl(initialUrl)
const observer = new EventsTopic();
observer.pipe(filter((e) => e.type === 'navigated')).subscribe(() => {
const routerUrl = `${location.pathname.substring(
getLocation().deploymentPath.length
)}${location.search}`;
router.navigateByUrl(routerUrl);
});
let lastUrl = initialUrl

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

View workflow job for this annotation

GitHub Actions / main

'lastUrl' is assigned a value but never used

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

View workflow job for this annotation

GitHub Actions / main

'lastUrl' is never reassigned. Use 'const' instead
// const observer = new EventsTopic()
// observer.pipe(filter((e) => e.type === 'navigated')).subscribe(() => {
// const routerUrl = `${location.pathname.substring(
// getLocation().deploymentPath.length
// )}${location.search}`;
// if (routerUrl !== lastUrl) {
// lastUrl = routerUrl;
// router.navigateByUrl(routerUrl);
// }
// });
}

0 comments on commit 28ba600

Please sign in to comment.