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

[RFC] createInjectionToken with support of multi: true #95

Closed
wanoo21 opened this issue Oct 5, 2023 · 4 comments · Fixed by #98
Closed

[RFC] createInjectionToken with support of multi: true #95

wanoo21 opened this issue Oct 5, 2023 · 4 comments · Fixed by #98
Assignees
Labels
enhancement New feature or request

Comments

@wanoo21
Copy link
Contributor

wanoo21 commented Oct 5, 2023

I started to rewrite some of my libraries and added ngxtension.

I'm using createInjectionToken quite often and I've noticed I can't create a multi-token - multi: true.

I checked the source code and I don't see to be a way to mark it as multiple.

image

Maybe there's a workaround I can't find out?

@nartc
Copy link
Collaborator

nartc commented Oct 5, 2023

That's right. I'll take a look at this.

@nartc
Copy link
Collaborator

nartc commented Oct 5, 2023

@wanoo21 added a fix but I'd like to point this out.

https://github.com/nartc/ngxtension-platform/pull/98/files#diff-28c9dd83d9c535288ff51d6470317daabbcffec42e587608c98810c47c71cddcR111

You can see here that provideFn() is called twice in the providers array. The first call is to make sure the provided factory is run. If not, then the value returned will only include the 2nd call provideFn(2).

@nartc nartc closed this as completed in #98 Oct 5, 2023
@wanoo21
Copy link
Contributor Author

wanoo21 commented Oct 6, 2023

Cool, thank you, nice addition!
I'd like to provide some other suggestions.

How about adding support for the Generic type and marking the factory as optional?

So basically this:
export const TOKEN = new InjectionToken<MyType>();

becomes:
export const [injectFn, provideFn] = createInjectionToken<MyType>();

There's a problem though, I can't make it multi without providing the factory 🤷🏻‍♂️

Also, considering the code above, would this work?
provideFn(() => { const foo = inject(FOO); if (foo) return 'something' return 'something else' })

Instead of a simple value, to provide a factory!

This is how I manage it now:
image

@nartc
Copy link
Collaborator

nartc commented Oct 6, 2023

@wanoo21 open a new issue on that please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants