Databases are now only usable in a single tab or window.
This is necessary due to PGlite single-user mode limitation to avoid race conditions if the user sends multiple queries from different locations. The postgres wire messages could be mixed resulting in unpredictable behaviours.
Web API features used to make it works:
sessionStorage to identify each tab/window with a unique id.
localStorage to store the locks between tabs and windows.
window.addEventListener('storage', ...) to listen to changes and synchronize each tab/window context state.
window.addEventListener('beforeunload', ...) to clear the lock if the tab or window is closed.
Databases are now only usable in a single tab or window.
This is necessary due to PGlite single-user mode limitation to avoid race conditions if the user sends multiple queries from different locations. The postgres wire messages could be mixed resulting in unpredictable behaviours.
Web API features used to make it works:
sessionStorage
to identify each tab/window with a unique id.localStorage
to store the locks between tabs and windows.window.addEventListener('storage', ...)
to listen to changes and synchronize each tab/window context state.window.addEventListener('beforeunload', ...)
to clear the lock if the tab or window is closed.Demo:
https://github.com/user-attachments/assets/f8df6e3b-f270-4932-acde-fc249a0ec28d