Skip to content

Commit

Permalink
fix: initializeModuleGuard fix and delete timer warnings (#91)
Browse files Browse the repository at this point in the history
* fix: performance, add timers, minor fixes

* fix: no restricted syntax lint fix

* fix: adding .nx to gitignore

* fix: add lang$ in userServiceMock

* fix: initializeModuleGuard fix and delete timer warnings

* fix: lint error fix

---------

Co-authored-by: kim.tran <[email protected]>
  • Loading branch information
KimFFVII and kim.tran authored Jan 29, 2024
1 parent 98e77e7 commit a1dfa9f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@ import { TranslateLoader } from '@ngx-translate/core'
import { first, mergeMap, Observable, tap } from 'rxjs'

export class AsyncTranslateLoader implements TranslateLoader {
static lastTimerId = 0
timerId = AsyncTranslateLoader.lastTimerId++

constructor(private translateLoader$: Observable<TranslateLoader>) {}

getTranslation(lang: string): Observable<any> {
return this.translateLoader$.pipe(
tap(() => console.time('AsyncTranslateLoader')),
tap(() => console.time('AsyncTranslateLoader_' + this.timerId)),
first(),
mergeMap((translateLoader) => translateLoader.getTranslation(lang)),
tap(() => console.timeEnd('AsyncTranslateLoader'))
tap(() => console.timeEnd('AsyncTranslateLoader_' + this.timerId))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import { AppStateService } from '../../services/app-state.service'
import { AsyncTranslateLoader } from './async-translate-loader.utils'
import { TranslateCombinedLoader } from './translate.combined.loader'

let lastTranslateLoaderTimerId = 0

export function createTranslateLoader(http: HttpClient, appStateService: AppStateService): TranslateLoader {
console.time('createTranslateLoader')
const timerId = lastTranslateLoaderTimerId++
console.time('createTranslateLoader_' + timerId)
return new AsyncTranslateLoader(
combineLatest([appStateService.currentMfe$.asObservable(), appStateService.globalLoading$.asObservable()]).pipe(
filter(([, isLoading]) => !isLoading),
Expand All @@ -26,7 +29,7 @@ export function createTranslateLoader(http: HttpClient, appStateService: AppStat
new TranslateHttpLoader(http, Location.joinWithSlash(currentMfe.remoteBaseUrl, `assets/i18n/`), '.json')
)
}),
tap(() => console.timeEnd('createTranslateLoader'))
tap(() => console.timeEnd('createTranslateLoader_' + timerId))
)
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@angular/core'
import { ActivatedRouteSnapshot, CanActivate, RouterStateSnapshot, UrlTree } from '@angular/router'
import { TranslateService } from '@ngx-translate/core'
import { filter, forkJoin, from, isObservable, map, mergeMap, Observable, of, tap } from 'rxjs'
import { filter, from, isObservable, map, mergeMap, Observable, of, tap, zip } from 'rxjs'
import { AppStateService } from './app-state.service'
import { ConfigurationService } from './configuration.service'
import { UserService } from './user.service'
Expand All @@ -22,16 +22,15 @@ export class InitializeModuleGuard implements CanActivate {
_state: RouterStateSnapshot
): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
console.time('InitializeModuleGuard')
return forkJoin([
return zip([
this.loadTranslations(),
this.configService.isInitialized,
this.appStateService.currentPortal$.isInitialized,
this.appStateService.globalLoading$.pipe(
filter((g) => !g),
map(() => true)
),
from(this.configService.isInitialized),
from(this.appStateService.currentPortal$.isInitialized),
this.appStateService.globalLoading$.pipe(filter((g) => !g)),
]).pipe(
tap(() => console.timeEnd('InitializeModuleGuard')),
tap(() => {
console.timeEnd('InitializeModuleGuard')
}),
map(() => true)
)
}
Expand Down

0 comments on commit a1dfa9f

Please sign in to comment.