-
Notifications
You must be signed in to change notification settings - Fork 62
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
Adding examples for “versionchange” and “blocked”. #133
Changes from 1 commit
990e8e9
ba42a98
7d37689
c5bc0a9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,7 +247,7 @@ db.close(); | |
|
||
In the future, the database may have grown to contain other object | ||
stores and indexes. The following example shows one way to handle | ||
opening an older version of the database. | ||
migrating from an older version of the database. | ||
|
||
<pre class=lang-javascript> | ||
var request = indexedDB.open("library", 3); // Request version 3. | ||
|
@@ -278,6 +278,63 @@ request.onsuccess = function() { | |
}; | ||
</pre> | ||
|
||
The database cannot upgrade while other tabs are still using it. To avoid blocking | ||
other tabs, you should use the "versionchange" event to close the connection to | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. other tabs → other tabs trying to upgrade the database |
||
the database. | ||
|
||
<pre class=lang-javascript> | ||
db.onversionchange = function() { | ||
// First, save any unsaved data: | ||
saveUnsavedData().then(function() { | ||
// Now you need to close the database. | ||
// If the document isn't being actively used, it may be appropriate to reload: | ||
if (!document.hasFocus()) { | ||
location.reload(); | ||
// Reloading will close the database, and also reload with the new JavaScript | ||
// and database definitions. | ||
} else { | ||
// Alternatively you may close the database and display a message to the | ||
// user. You need to ensure this isn't a disruptive user experience. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to list the other alternative, which is to notify the user that another tab wants to upgrade, so the user should finish their work and close (or reload) this one? (That's probably getting too long.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that would be good, to be honest. I guess you could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No need for BroadcastChannel - the receiver of "versionchange" could pop up a modeless notification saying e.g. "There's a new version of this web app available! Just click reload whenever you're ready." There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah - ok. I gotta get my head out of service worker land and message passing. I'm still not totally clear where .onblocked comes into play... if at all? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The open request trying to do the upgrade gets "blocked" if there are any other connections that don't close as soon as they receive "versionchange". So one approach might be: const open = indexedDB.open(name, version);
open.onerror = e => notify(`Something went wrong: ${e.message}`);
open.onblocked = e => notify('Close the other tabs and then click OK');
open.onupgradeneeded = e => {
if (e.oldVersion === 0) { ... }
if (e.oldVersion === 1) { ... }
...
};
open.onsuccess = e => {
const db = open.result;
db.onversionchange = e => notify('Another tab wants to upgrade. Close this one.');
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But another might be: const open = indexedDB.open(name, version);
open.onerror = e => notify(`Something went wrong: ${e.message}`);
open.onblocked = e => notify('THIS SHOULD NEVER HAPPEN!');
open.onupgradeneeded = e => {
if (e.oldVersion === 0) { ... }
if (e.oldVersion === 1) { ... }
...
};
open.onsuccess = e => {
const db = open.result;
db.onversionchange = e => {
notify('Getting out of the way because another tab wants to upgrade.');
db.close();
};
}; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In reality, you want something more subtle and user friendly like Jake has, such as trying to transparently reload if possible and if not then pestering the user the minimal amount. |
||
db.close(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll make a reference to the onclose event. |
||
displayMessage("Disconnected from database, please refresh to reconnect."); | ||
} | ||
}); | ||
}; | ||
|
||
function saveUnsavedData() { | ||
// How you do this depends on your app. | ||
} | ||
|
||
function displayMessage() { | ||
// Show some sort of alert to the user. | ||
} | ||
</pre> | ||
|
||
Your new tabs can use the "blocked" event to detect if older tabs are preventing | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your new tabs → Pages |
||
them from upgrading the database. | ||
|
||
<pre class=lang-javascript> | ||
var request = indexedDB.open("library", 4); // Request version 4. | ||
|
||
request.onupgradeneeded = function(event) { | ||
// ... | ||
}; | ||
|
||
request.onblocked = function() { | ||
displayMessage("Please close other tabs to this site."); | ||
}; | ||
|
||
request.onsuccess = function() { | ||
hideMessage(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably have |
||
}; | ||
|
||
function hideMessage() { | ||
// Hide a previously displayed message. | ||
} | ||
</pre> | ||
|
||
The user will only see the above message if another tab fails to disconnect from | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe clarify (based on previous questions that have been raised in this area) If all recipients of "versionchange" close the connection synchronously within the handler then "blocked" will not be fired. But if they don't close synchronously (i.e. they ignored it, or they do async work like starting a final transaction to save state) then the "blocked" event will be seen. Once all connections finally close then "success" will be seen - that is, the blocked state is not permanent. That's why it's important to hide the message when "success" comes in. |
||
the database. Ideally the user won't see this! | ||
</aside> | ||
|
||
<!-- ============================================================ --> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should use → may listen for