From a61036eeb20831e81ba8e1dae41c64b37f0be274 Mon Sep 17 00:00:00 2001 From: Kaitlyn Ekdahl Date: Wed, 13 Jan 2021 16:52:25 -0800 Subject: [PATCH] feat: pipe only loads necessary dependencies When using multiple scopes, Transloco pipe will derive scope from the key and only load dependencies for that scope. Closes #394 --- .../transloco/src/lib/transloco.pipe.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/projects/ngneat/transloco/src/lib/transloco.pipe.ts b/projects/ngneat/transloco/src/lib/transloco.pipe.ts index 293576b1..355938db 100644 --- a/projects/ngneat/transloco/src/lib/transloco.pipe.ts +++ b/projects/ngneat/transloco/src/lib/transloco.pipe.ts @@ -55,11 +55,20 @@ export class TranslocoPipe implements PipeTransform, OnDestroy { active: activeLang }); - return Array.isArray(this.providerScope) - ? forkJoin( - (this.providerScope).map(providerScope => this.resolveScope(lang, providerScope)) - ) - : this.resolveScope(lang, this.providerScope); + let resolvedScope; + + if (Array.isArray(this.providerScope)) { + const keyPrefix = key.split('.')[0]; + const derivedScope = this.providerScope.find(s => + typeof s === 'string' ? s === keyPrefix : s.alias === keyPrefix || s.scope === keyPrefix + ); + + resolvedScope = this.resolveScope(lang, derivedScope); + } else { + resolvedScope = this.resolveScope(lang, this.providerScope); + } + + return resolvedScope; }), listenOrNotOperator(this.listenToLangChange) )