1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-12-01 13:42:16 -05:00
kaiwa/clientapp/helpers/cache.js
2014-01-06 22:01:17 -08:00

51 lines
1.1 KiB
JavaScript

var WildEmitter = require('wildemitter');
var STATES = [
'uncached',
'idle',
'checking',
'downloading',
'updateReady',
'obsolete'
];
function AppCache() {
WildEmitter.call(this);
var self = this;
this.cache = window.applicationCache;
this.state = STATES[this.cache.status];
this.emit('change', this.state);
function mapevent(name, altName) {
self.cache.addEventListener(name, function (e) {
var newState = STATES[self.cache.status];
if (newState !== self.state) {
self.state = newState;
self.emit('change', newState);
}
self.emit(altName || name, e);
}, false);
}
mapevent('cached');
mapevent('checking');
mapevent('downloading');
mapevent('error');
mapevent('noupdate', 'noUpdate');
mapevent('obsolete');
mapevent('progress');
mapevent('updateready', 'updateReady');
}
AppCache.prototype = Object.create(WildEmitter.prototype, {
constructor: {
value: AppCache
}
});
AppCache.prototype.update = function () {
this.cache.update();
};
module.exports = AppCache;