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

Conversation

jakearchibald
Copy link
Contributor

No description provided.

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

} 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.
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.

}
</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

}
</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.

};

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

@@ -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.

other tabs → other tabs trying to upgrade the database

// 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.

@marcoscaceres
Copy link
Member

this is super helpful! Thanks @jakearchibald!

@jakearchibald
Copy link
Contributor Author

@inexorabletash excellent feedback. I've made some major changes to the original PR. The example is now in its own section with its own ID. I've covered more ways to handle "versionchange", and gone into more detail with "blocked".

</pre>

Another way is to call the database's close method. Other code on your page
should react to the database's "close" event, so it knows the database is no
Copy link
Member

Choose a reason for hiding this comment

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

The close event only fires when the connection closes abnormally, e.g. when the user deletes browsing data or there is corruption or an I/O error. If close() is called explicitly the event does not fire.

Copy link
Member

Choose a reason for hiding this comment

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

And therefore it might be worth adding a note to the onclose definition that it's only fired in abnormal close scenarios. Look for the forced close flag.

Copy link
Member

@inexorabletash inexorabletash left a comment

Choose a reason for hiding this comment

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

Just one nit

@jakearchibald
Copy link
Contributor Author

@inexorabletash updated!

@inexorabletash inexorabletash merged commit 4e3b78a into w3c:gh-pages Jan 9, 2017
@jakearchibald
Copy link
Contributor Author

Cheers!

@inexorabletash
Copy link
Member

Thanks @jakearchibald !

I made some subsequent tweaks (wordsmithing, spelling fix, definition links) - can you take a peek at the live spec and make sure I didn't make it worse?

@jakearchibald jakearchibald deleted the versionchange-example branch January 10, 2017 10:21
@jakearchibald
Copy link
Contributor Author

@inexorabletash looks good to me, thanks!

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

Successfully merging this pull request may close these issues.

None yet

3 participants