Skip to content

Instantly share code, notes, and snippets.

@adamloving
Created February 1, 2011 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamloving/805497 to your computer and use it in GitHub Desktop.
Save adamloving/805497 to your computer and use it in GitHub Desktop.
A class to monitor background updates to a HTML based iPhone application
/*
A class to monitor background updates to a HTML based iPhone application.
Note that this needs to be used in conjunction with an HTML5 Cache Manifest file
This originated from some sample code in this book:
http://www.amazon.com/Building-iPhone-Apps-HTML-JavaScript/dp/0596805780
*/
MyCompany.app.CacheManager = function() {
return {
cacheStatusValues: {
0: 'uncached', 1: 'idle', 2: 'checking', 3: 'downloading', 4: 'updateready', 5:'obsolete'
},
isUpdating: false,
init: function() {
// The 4 events we are concerned with are:
// 'downloading' - fired at the beginning of a needed update
// 'progress' - fired during the download of a needed update
// 'cached' - fired at the end of the very first download
// 'updateready' - fired at the end of all other successful updates,
// 'noupdate' - for some unknown reason, this needs to be checked as well
window.applicationCache.addEventListener('downloading', this.onDownloading, false);
window.applicationCache.addEventListener('progress', this.onDownloading, false);
window.applicationCache.addEventListener('updateready', this.onUpdateReady, false);
window.applicationCache.addEventListener('cached', this.onUpdateReady, false);
window.applicationCache.addEventListener('noupdate', this.onNoUpdate, false);
window.applicationCache.addEventListener('checking', this.onCacheEvent, false);
window.applicationCache.addEventListener('error', this.onCacheEvent, false);
window.applicationCache.addEventListener('obsolete', this.onCacheEvent, false);
// window.applicationCache.update();
},
onNoUpdate: function(e) {
if (window.applicationCache.status == window.applicationCache.UPDATEREADY) {
MyCompany.app.CacheManager.isUpdating = true;
MyCompany.app.CacheManager.reload();
} else {
MyCompany.app.CacheManager.onCacheEvent(e);
}
},
onDownloading: function() {
konsole.log('Downloading update...');
if (MyCompany.app.ApplicationController.panel && !MyCompany.app.CacheManager.isUpdating) {
MyCompany.app.ApplicationController.onDownloadingUpdate();
}
MyCompany.app.CacheManager.isUpdating = true;
},
onUpdateReady: function(e) {
konsole.log('Update has been downloaded.');
MyCompany.app.CacheManager.isUpdating = true;
MyCompany.app.CacheManager.reload();
},
reload: function() {
try {
window.applicationCache.swapCache();
konsole.log('Swapped/updated the application cache.');
} catch (err) {
konsole.error('could not swap cache');
}
window.location.reload();
},
onCacheEvent: function(e) {
var online, status, type, message;
online = (navigator.onLine) ? 'yes' : 'no';
status = MyCompany.app.CacheManager.cacheStatusValues[window.applicationCache.status];
type = e.type;
message = 'Cache: online(' + online + ')' +
' event(' + type + ')' +
' status(' + status + ')';
if (type == 'error' && navigator.onLine) {
message+= ' There was an unknown error, check your Cache Manifest.';
}
console.log(message);
}
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment