mail/src/js/util/update/update-handler.js

126 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-10-02 16:05:44 -04:00
'use strict';
var ngModule = angular.module('woUtil');
ngModule.service('updateHandler', UpdateHandler);
module.exports = UpdateHandler;
2014-10-02 16:05:44 -04:00
var axe = require('axe-logger'),
cfg = require('../../app-config').config,
updateV1 = require('./update-v1'),
updateV2 = require('./update-v2'),
updateV3 = require('./update-v3'),
updateV4 = require('./update-v4'),
updateV5 = require('./update-v5');
/**
* Handles database migration
*/
2014-12-11 12:12:37 -05:00
function UpdateHandler(appConfigStore, accountStore, auth, dialog) {
this._appConfigStorage = appConfigStore;
2014-11-21 07:33:22 -05:00
this._userStorage = accountStore;
2014-10-02 16:05:44 -04:00
this._updateScripts = [updateV1, updateV2, updateV3, updateV4, updateV5];
this._auth = auth;
this._dialog = dialog;
}
2014-10-02 16:05:44 -04:00
/**
* Executes all the necessary updates
*/
2014-12-11 07:57:40 -05:00
UpdateHandler.prototype.update = function() {
2014-10-02 16:05:44 -04:00
var self = this,
currentVersion = 0,
targetVersion = cfg.dbVersion,
versionDbType = 'dbVersion';
2014-12-11 07:57:40 -05:00
return self._appConfigStorage.listItems(versionDbType, 0, null).then(function(items) {
2014-10-02 16:05:44 -04:00
// parse the database version number
if (items && items.length > 0) {
currentVersion = parseInt(items[0], 10);
2014-03-11 11:06:19 -04:00
}
2014-12-11 07:57:40 -05:00
return self._applyUpdate({
2014-10-02 16:05:44 -04:00
currentVersion: currentVersion,
targetVersion: targetVersion
2014-12-11 07:57:40 -05:00
});
2014-10-02 16:05:44 -04:00
});
};
/**
* Schedules necessary updates and executes thom in order
*/
2014-12-11 07:57:40 -05:00
UpdateHandler.prototype._applyUpdate = function(options) {
var self = this;
2014-12-11 12:12:37 -05:00
return new Promise(function(resolve, reject) {
2014-12-11 07:57:40 -05:00
var scriptOptions,
queue = [];
2014-03-11 11:06:19 -04:00
2014-12-11 07:57:40 -05:00
if (options.currentVersion >= options.targetVersion) {
// the current database version is up to date
resolve();
2014-10-02 16:05:44 -04:00
return;
2014-03-11 11:06:19 -04:00
}
2014-12-11 07:57:40 -05:00
scriptOptions = {
appConfigStorage: self._appConfigStorage,
userStorage: self._userStorage,
auth: self._auth
};
// add all the necessary database updates to the queue
for (var i = options.currentVersion; i < options.targetVersion; i++) {
queue.push(self._updateScripts[i]);
2014-10-02 16:05:44 -04:00
}
2014-03-11 11:06:19 -04:00
2014-12-11 07:57:40 -05:00
// takes the next update from the queue and executes it
function executeNextUpdate() {
if (queue.length < 1) {
// we're done
resolve();
return;
}
// process next update
var script = queue.shift();
script(scriptOptions).then(executeNextUpdate).catch(reject);
}
2014-10-02 16:05:44 -04:00
2014-12-11 07:57:40 -05:00
executeNextUpdate();
});
2014-10-02 16:05:44 -04:00
};
/**
* Check application version and update correspondingly
*/
UpdateHandler.prototype.checkForUpdate = function() {
var self = this;
2014-10-02 16:05:44 -04:00
// Chrome Packaged App
if (typeof window.chrome !== 'undefined' && chrome.runtime && chrome.runtime.onUpdateAvailable) {
// check for Chrome app update and restart
chrome.runtime.onUpdateAvailable.addListener(function(details) {
axe.debug('New Chrome App update... requesting reload.');
// Chrome downloaded a new app version
self._dialog.confirm({
2014-10-02 16:05:44 -04:00
title: 'Update available',
message: 'A new version ' + details.version + ' of the app is available. Restart the app to update?',
positiveBtnStr: 'Restart',
negativeBtnStr: 'Not now',
showNegativeBtn: true,
callback: function(agree) {
if (agree) {
chrome.runtime.reload();
}
}
});
2014-10-02 16:05:44 -04:00
});
chrome.runtime.requestUpdateCheck(function(status) {
if (status === "update_found") {
axe.debug("Update pending...");
} else if (status === "no_update") {
axe.debug("No update found.");
} else if (status === "throttled") {
axe.debug("Checking updates too frequently.");
}
});
}
};