Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(transloco): support nested array of scopes #650

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

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

@stackblitz
Copy link

stackblitz bot commented Apr 26, 2023

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

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

@@ -7,7 +7,7 @@
"declarationMap": true,
"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