Skip to content

Question about createInjectionToken and RxDB #194

Closed Answered by nartc
tutkli asked this question in Q&A
Discussion options

You must be logged in to vote

There are some subtle differences between your createInjectionToken code and the DatabaseService code.

  • The factory that you pass in for createInjectionToken is only INVOKED ONCE and the DB_INSTANCE is undefined initially.
  • The service code exposes a get db() which technically tries to get the latest reference to DB_INSTANCE whenever databaseService.db is accessed.

You can try returning a function that returns DB_INSTANCE with createInjectionToken instead

export const [injectDatabase] = createInjectionToken(() => {
  return () => DB_INSTANCE;
});

@Component({ ... })
export class AppComponent {
  private database = injectDatabase();

  constructor() {
      console.log(this.database()); /…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tutkli
Comment options

Answer selected by tutkli
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants