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

Prevent race condition on creation of database #134

Open
coolweb opened this issue Feb 14, 2020 · 9 comments
Open

Prevent race condition on creation of database #134

coolweb opened this issue Feb 14, 2020 · 9 comments
Labels
question Further information is requested

Comments

@coolweb
Copy link
Contributor

coolweb commented Feb 14, 2020

The creation of the database is done in your forroot that call the ngx indexed db service constructor which create the db in asynchronous action. If some code try to access the database, the database might not be created.
A solution is to create a separate method to create the database with async return like promise and call this method in an APP_INITILAZER provider in your module.

@assuncaocharles
Copy link
Owner

I am not sure if this would make a big difference, currently, the DB is created on the module initialization. What would change creating a method and calling it? I think to check if the DB is ready will still have to be done.

@assuncaocharles assuncaocharles added the question Further information is requested label Feb 15, 2020
@coolweb
Copy link
Contributor Author

coolweb commented Feb 17, 2020

It make a difference, in our case we want in some scenario delete the database on app starting. If your code is in a method which return a promise and that method is called into an APP_INITIALIZER provider in your module, it'll not create race condition. For the moment while our app is starting, your code start which create the db without store, our code do some stuff and then the rest of your code continue and try to create stores...

@WhatzzUp
Copy link

@coolweb we´r facing the same issue

@assuncaocharles
Copy link
Owner

Alright it makes sense, so I would think about passing a callback into the configuration to be called when it's done.. What do you think?

@coolweb
Copy link
Contributor Author

coolweb commented Feb 21, 2020

A callback'll resolve the problem but I think the standard way in angular is to use the APP_INITIALIZER factory when we need to initialize async stuffs, in the forroot you initialize sync stuff like config values. I use APP_INITIALIZER in some apps for this kind of scenario, some info here https://www.tektutorialshub.com/angular/angular-how-to-use-app-initializer/

@evilstiefel
Copy link

Just FYI since this is still open, to my knowledge, you can't use APP_INITIALIZER when the module is loaded lazily (and there is no MODULE_INITIALIZER equivalent yet), so a callback would be more universal

@jonlighthill
Copy link

Can this be related to #295

@assuncaocharles
Copy link
Owner

@all-contributors please add @coolweb for maintenance

@allcontributors
Copy link
Contributor

@assuncaocharles

I've put up a pull request to add @coolweb! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants