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

Adding examples for “versionchange” and “blocked”. #133

Merged
merged 4 commits into from
Jan 9, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 58 additions & 1 deletion index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Copy link
Member

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

Copy link
Member

Choose a reason for hiding this comment

The 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.
Copy link
Member

Choose a reason for hiding this comment

The 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.)

Copy link
Member

Choose a reason for hiding this comment

The 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 new BroadcastChannel("idb-messages") or something?

Copy link
Member

Choose a reason for hiding this comment

The 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."

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member

Choose a reason for hiding this comment

The 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.');
};

Copy link
Member

Choose a reason for hiding this comment

The 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(); 
  };
};

Copy link
Member

Choose a reason for hiding this comment

The 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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe also db = null so that other page logic can notice the connection is done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your new tabs → Pages
older tabs → tabs with older versions of the code

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should probably have // ... here indicating proceeding with the usual open steps

};

function hideMessage() {
// Hide a previously displayed message.
}
</pre>

The user will only see the above message if another tab fails to disconnect from
Copy link
Member

Choose a reason for hiding this comment

The 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>

<!-- ============================================================ -->
Expand Down