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

Sharing a single provider instance between multiple Injection Tokens #2470

Open
darkbasic opened this issue Nov 4, 2023 · 0 comments
Open

Comments

@darkbasic
Copy link
Contributor

When I rearchitected Accounts.js 1.0 I wanted to preserve the ability to use different databases to store the user data and the sessions (whenever the user decides to use sessions instead of JSON Web Tokens).
This is done by passing a database type parameter to the function in charge to create the module.
This way depending on the parameter the module will provide either DatabaseInterfaceUserToken, DatabaseInterfaceSessionsToken or both (not providing both of them means that another module will have to provide the other provider).
The problem is that when you want to provide both of them from the same module you cannot actually do so because useClass will instantiate two instances of the same provider.
I would like to implement the possibility of sharing the same instance of a class between multiple injectors:

{
  provide: DatabaseInterfaceUserToken,
  useClass: Mongo,
},
{
  provide: DatabaseInterfaceSessionsToken,
  useInstance: DatabaseInterfaceUserToken,
},

The current workaround is that whenever the same module wants to provide both of them we provide DatabaseInterfaceSessionsToken using { useValue: undefined } and every time we need to access the DatabaseInterfaceSessionsToken we check if it is defined and we use DatabaseInterfaceUserToken instead whenever it is undefined. This is possible because DatabaseInterfaceUserToken must be always provided.

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

No branches or pull requests

1 participant