Skip to content

Sharing a single provider instance between multiple Injection Tokens #2470

Description

@darkbasic

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.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions