Created
February 10, 2013 16:34
-
-
Save slightlyoff/4750136 to your computer and use it in GitHub Desktop.
Upgrading
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ctrl.js | |
// origin: http://example.com | |
// scope: "/*" | |
"use strict"; | |
// Called when this controller is installed as the newest version | |
// for a scope. Perhaps a clearer name for what is now sketched | |
// as "onupdate". Note that this is called BEFORE "onupgrade" is | |
// sent to a previous version's controller. | |
this.oninstall = function(e) { | |
// We start with empty caches for each version of each controller. | |
assert(!this.caches.get("main")); | |
var main = new Cache([ | |
// A resource used in v1. As an optimization, the browser | |
//can keep the copy it downloaded from v1 and avoid the network | |
// hit, assuming it hasn't expired in the HTTP cache sense. | |
"http://cdn.com/js/lib.js", | |
// A resource that's new to v2. This will be fetched now. | |
"/app/2/app.js", | |
// ... | |
]); | |
this.caches.set("main", main); | |
}; | |
// Called when the controller is replaced by a newer | |
// version, but after the new version's oninstall has returned. | |
this.onupgrade = function(e) { | |
// We could free resources here, but in most cases won't need | |
// to. Instead, when the last live page controlled by our | |
// replaced controller is unloaded, the browser can remove | |
// all caches "owned" by the now-removed controller. Assuming | |
// that v3 also defines a "main", its "oninstall" will not be | |
// able to see the "main" cache associated with v2. It can, | |
// however, define a new one and, thanks to the overlapping | |
// resources optimization outlined above, avoid re-fetching most | |
// of the resources it needs. | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment