Skip to content

Conversation

@dmitry-stepanenko
Copy link

@dmitry-stepanenko dmitry-stepanenko commented Apr 26, 2023

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

[ ] Bugfix
[x] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Build related changes
[ ] CI related changes
[ ] Documentation content changes
[ ] Other... Please describe:

What is the current behavior?

Right now transloco is able to resolve only 1-level array of scopes. This works fine with straightforward approach when using { provide: TRANSLOCO_SCOPE, useValue: scope, multi: true }. However, it might be needed to pass an array of scopes within single provider.

What is the new behavior?

This change allows to use the following syntax

{ 
  provide: TRANSLOCO_SCOPE,
  useValue: ['scope1', 'scope2']
  multi: true
}

In my scenario there's a big amount of scopes that we're using. They're provided either as component or module providers. And there's a limitation related to Angular's injection hierarchy: it resolves ElementInjectors before ModuleInjectors and if TRANSLOCO_SCOPE is provided on the ElementInjector level, it won't grab module injectors at all. To overcome this I want to grab all parent scopes and provide them explicitly

export function provideLanguageScopes(
  scope: LanguageScope,
  ...otherScopes: LanguageScope[]
): Provider {
  const scopeSet = new Set(otherScopes.concat(scope));
  return {
    provide: TRANSLOCO_SCOPE,
    useFactory: (parentScopes: LanguageScope[] | null) => {
      parentScopes && parentScopes.flat(Infinity).forEach(p => scopeSet.add(p));
      return [...scopeSet.values()];
    },
    multi: true,
    deps: [[new SkipSelf(), new Optional(), TRANSLOCO_SCOPE]],
  };
}

This approach requires transloco to be able to work with nested arrays of scopes. This PR does not introduce any changes to existing behavior, only enhances it so that it is possible to provide arrays under TRANSLOCO_SCOPE

Does this PR introduce a breaking change?

[x] Yes
[ ] No

Setting yes as there's a change to MaybeArray interface, that might not be compatible with all implementations

Other information

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

type ScopeResolverParams = {
inline: string | undefined;
provider: MaybeArray<TranslocoScope>;
provider: TranslocoScope;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this was done unintentionally, scope resolver is not able to process array of scopes. There's no compilation error only because of isScopeObject type guard, that is also not supposed to handle array of scopes

"inlineSources": true,
"types": [],
"lib": ["dom", "es2018"]
"lib": ["dom", "es2019"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be able to use Array.prototype.flat()

@dmitry-stepanenko
Copy link
Author

dmitry-stepanenko commented Apr 28, 2023

@shaharkazaz will you be able to look at this? An issue on our side appears to be quite serious. Appreciated in advance 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant