mirror of https://github.com/keeweb/keeweb.git
updater model
This commit is contained in:
parent
8f4932610c
commit
94660d9e71
|
@ -62,7 +62,7 @@ ready(() => {
|
|||
function loadConfigs() {
|
||||
return Promise.all([
|
||||
AppSettingsModel.load(),
|
||||
UpdateModel.instance.load(),
|
||||
UpdateModel.load(),
|
||||
RuntimeDataModel.load(),
|
||||
FileInfoCollection.load()
|
||||
]);
|
||||
|
|
|
@ -33,8 +33,8 @@ const Updater = {
|
|||
|
||||
updateInProgress() {
|
||||
return (
|
||||
UpdateModel.instance.get('status') === 'checking' ||
|
||||
['downloading', 'extracting'].indexOf(UpdateModel.instance.get('updateStatus')) >= 0
|
||||
UpdateModel.status === 'checking' ||
|
||||
['downloading', 'extracting'].indexOf(UpdateModel.updateStatus) >= 0
|
||||
);
|
||||
},
|
||||
|
||||
|
@ -58,7 +58,7 @@ const Updater = {
|
|||
return;
|
||||
}
|
||||
let timeDiff = this.MinUpdateTimeout;
|
||||
const lastCheckDate = UpdateModel.instance.get('lastCheckDate');
|
||||
const lastCheckDate = UpdateModel.lastCheckDate;
|
||||
if (lastCheckDate) {
|
||||
timeDiff = Math.min(
|
||||
Math.max(this.UpdateInterval + (lastCheckDate - new Date()), this.MinUpdateTimeout),
|
||||
|
@ -73,7 +73,7 @@ const Updater = {
|
|||
if (!this.enabled || this.updateInProgress()) {
|
||||
return;
|
||||
}
|
||||
UpdateModel.instance.set('status', 'checking');
|
||||
UpdateModel.set({ status: 'checking' });
|
||||
if (!startedByUser) {
|
||||
// additional protection from broken program logic, to ensure that auto-checks are not performed more than once an hour
|
||||
const diffMs = new Date() - this.updateCheckDate;
|
||||
|
@ -96,18 +96,18 @@ const Updater = {
|
|||
logger.info('Update check: ' + (match ? match[0] : 'unknown'));
|
||||
if (!match) {
|
||||
const errMsg = 'No version info found';
|
||||
UpdateModel.instance.set({
|
||||
UpdateModel.set({
|
||||
status: 'error',
|
||||
lastCheckDate: dt,
|
||||
lastCheckError: errMsg
|
||||
});
|
||||
UpdateModel.instance.save();
|
||||
UpdateModel.save();
|
||||
this.scheduleNextCheck();
|
||||
return;
|
||||
}
|
||||
const updateMinVersionMatch = data.match(/#\s*updmin:v([\d+\.\w]+)/);
|
||||
const prevLastVersion = UpdateModel.instance.get('lastVersion');
|
||||
UpdateModel.instance.set({
|
||||
const prevLastVersion = UpdateModel.lastVersion;
|
||||
UpdateModel.set({
|
||||
status: 'ok',
|
||||
lastCheckDate: dt,
|
||||
lastSuccessCheckDate: dt,
|
||||
|
@ -116,14 +116,14 @@ const Updater = {
|
|||
lastCheckError: null,
|
||||
lastCheckUpdMin: updateMinVersionMatch ? updateMinVersionMatch[1] : null
|
||||
});
|
||||
UpdateModel.instance.save();
|
||||
UpdateModel.save();
|
||||
this.scheduleNextCheck();
|
||||
if (!this.canAutoUpdate()) {
|
||||
return;
|
||||
}
|
||||
if (
|
||||
prevLastVersion === UpdateModel.instance.get('lastVersion') &&
|
||||
UpdateModel.instance.get('updateStatus') === 'ready'
|
||||
prevLastVersion === UpdateModel.lastVersion &&
|
||||
UpdateModel.updateStatus === 'ready'
|
||||
) {
|
||||
logger.info('Waiting for the user to apply downloaded update');
|
||||
return;
|
||||
|
@ -131,33 +131,30 @@ const Updater = {
|
|||
if (!startedByUser && this.getAutoUpdateType() === 'install') {
|
||||
this.update(startedByUser);
|
||||
} else if (
|
||||
SemVer.compareVersions(
|
||||
UpdateModel.instance.get('lastVersion'),
|
||||
RuntimeInfo.version
|
||||
) > 0
|
||||
SemVer.compareVersions(UpdateModel.lastVersion, RuntimeInfo.version) > 0
|
||||
) {
|
||||
UpdateModel.instance.set('updateStatus', 'found');
|
||||
UpdateModel.set({ updateStatus: 'found' });
|
||||
}
|
||||
},
|
||||
error: e => {
|
||||
logger.error('Update check error', e);
|
||||
UpdateModel.instance.set({
|
||||
UpdateModel.set({
|
||||
status: 'error',
|
||||
lastCheckDate: new Date(),
|
||||
lastCheckError: 'Error checking last version'
|
||||
});
|
||||
UpdateModel.instance.save();
|
||||
UpdateModel.save();
|
||||
this.scheduleNextCheck();
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
canAutoUpdate() {
|
||||
const minLauncherVersion = UpdateModel.instance.get('lastCheckUpdMin');
|
||||
const minLauncherVersion = UpdateModel.lastCheckUpdMin;
|
||||
if (minLauncherVersion) {
|
||||
const cmp = SemVer.compareVersions(Launcher.version, minLauncherVersion);
|
||||
if (cmp < 0) {
|
||||
UpdateModel.instance.set({ updateStatus: 'ready', updateManual: true });
|
||||
UpdateModel.set({ updateStatus: 'ready', updateManual: true });
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -165,7 +162,7 @@ const Updater = {
|
|||
},
|
||||
|
||||
update(startedByUser, successCallback) {
|
||||
const ver = UpdateModel.instance.get('lastVersion');
|
||||
const ver = UpdateModel.lastVersion;
|
||||
if (!this.enabled) {
|
||||
logger.info('Updater is disabled');
|
||||
return;
|
||||
|
@ -174,24 +171,24 @@ const Updater = {
|
|||
logger.info('You are using the latest version');
|
||||
return;
|
||||
}
|
||||
UpdateModel.instance.set({ updateStatus: 'downloading', updateError: null });
|
||||
UpdateModel.set({ updateStatus: 'downloading', updateError: null });
|
||||
logger.info('Downloading update', ver);
|
||||
Transport.httpGet({
|
||||
url: Links.UpdateDesktop.replace('{ver}', ver),
|
||||
file: 'KeeWeb-' + ver + '.zip',
|
||||
cache: !startedByUser,
|
||||
success: filePath => {
|
||||
UpdateModel.instance.set('updateStatus', 'extracting');
|
||||
UpdateModel.set({ updateStatus: 'extracting' });
|
||||
logger.info('Extracting update file', this.UpdateCheckFiles, filePath);
|
||||
this.extractAppUpdate(filePath, err => {
|
||||
if (err) {
|
||||
logger.error('Error extracting update', err);
|
||||
UpdateModel.instance.set({
|
||||
UpdateModel.set({
|
||||
updateStatus: 'error',
|
||||
updateError: 'Error extracting update'
|
||||
});
|
||||
} else {
|
||||
UpdateModel.instance.set({ updateStatus: 'ready', updateError: null });
|
||||
UpdateModel.set({ updateStatus: 'ready', updateError: null });
|
||||
if (!startedByUser) {
|
||||
Events.emit('update-app');
|
||||
}
|
||||
|
@ -203,7 +200,7 @@ const Updater = {
|
|||
},
|
||||
error(e) {
|
||||
logger.error('Error downloading update', e);
|
||||
UpdateModel.instance.set({
|
||||
UpdateModel.set({
|
||||
updateStatus: 'error',
|
||||
updateError: 'Error downloading update'
|
||||
});
|
||||
|
@ -268,7 +265,7 @@ const Updater = {
|
|||
try {
|
||||
window.applicationCache.swapCache();
|
||||
} catch (e) {}
|
||||
UpdateModel.instance.set('updateStatus', 'ready');
|
||||
UpdateModel.set({ updateStatus: 'ready' });
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
|
@ -20,8 +20,7 @@ class RuntimeDataModel extends Model {
|
|||
}
|
||||
|
||||
save() {
|
||||
console.log('save', this);
|
||||
// SettingsStore.save('runtime-data', this);
|
||||
SettingsStore.save('runtime-data', this);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,22 +1,7 @@
|
|||
import Backbone from 'backbone';
|
||||
import { Model } from 'framework/model';
|
||||
import { SettingsStore } from 'comp/settings/settings-store';
|
||||
|
||||
const UpdateModel = Backbone.Model.extend({
|
||||
defaults: {
|
||||
lastSuccessCheckDate: null,
|
||||
lastCheckDate: null,
|
||||
lastVersion: null,
|
||||
lastVersionReleaseDate: null,
|
||||
lastCheckError: null,
|
||||
lastCheckUpdMin: null,
|
||||
status: null,
|
||||
updateStatus: null,
|
||||
updateError: null,
|
||||
updateManual: false
|
||||
},
|
||||
|
||||
initialize() {},
|
||||
|
||||
class UpdateModel extends Model {
|
||||
load() {
|
||||
return SettingsStore.load('update-info').then(data => {
|
||||
if (data) {
|
||||
|
@ -32,10 +17,10 @@ const UpdateModel = Backbone.Model.extend({
|
|||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
}
|
||||
|
||||
save() {
|
||||
const attr = _.clone(this.attributes);
|
||||
const attr = _.clone(this);
|
||||
Object.keys(attr).forEach(key => {
|
||||
if (key.lastIndexOf('update', 0) === 0) {
|
||||
delete attr[key];
|
||||
|
@ -43,8 +28,21 @@ const UpdateModel = Backbone.Model.extend({
|
|||
});
|
||||
SettingsStore.save('update-info', attr);
|
||||
}
|
||||
}
|
||||
|
||||
UpdateModel.defineModelProperties({
|
||||
lastSuccessCheckDate: null,
|
||||
lastCheckDate: null,
|
||||
lastVersion: null,
|
||||
lastVersionReleaseDate: null,
|
||||
lastCheckError: null,
|
||||
lastCheckUpdMin: null,
|
||||
status: null,
|
||||
updateStatus: null,
|
||||
updateError: null,
|
||||
updateManual: false
|
||||
});
|
||||
|
||||
UpdateModel.instance = new UpdateModel();
|
||||
const instance = new UpdateModel();
|
||||
|
||||
export { UpdateModel };
|
||||
export { instance as UpdateModel };
|
||||
|
|
|
@ -86,7 +86,7 @@ class AppView extends View {
|
|||
this.listenTo(Events, 'second-instance', this.showSingleInstanceAlert);
|
||||
this.listenTo(Events, 'file-modified', this.handleAutoSaveTimer);
|
||||
|
||||
this.listenTo(UpdateModel.instance, 'change:updateReady', this.updateApp);
|
||||
this.listenTo(UpdateModel, 'change:updateReady', this.updateApp);
|
||||
|
||||
this.listenTo(Events, 'enter-full-screen', this.enterFullScreen);
|
||||
this.listenTo(Events, 'leave-full-screen', this.leaveFullScreen);
|
||||
|
@ -189,7 +189,7 @@ class AppView extends View {
|
|||
|
||||
updateApp() {
|
||||
if (
|
||||
UpdateModel.instance.get('updateStatus') === 'ready' &&
|
||||
UpdateModel.updateStatus === 'ready' &&
|
||||
!Launcher &&
|
||||
!this.model.files.hasOpenFiles()
|
||||
) {
|
||||
|
|
|
@ -32,14 +32,14 @@ class FooterView extends View {
|
|||
this.listenTo(this, 'hide', this.viewHidden);
|
||||
this.listenTo(this.model.files, 'change', this.render);
|
||||
this.listenTo(Events, 'set-locale', this.render);
|
||||
this.listenTo(UpdateModel.instance, 'change:updateStatus', this.render);
|
||||
this.listenTo(UpdateModel, 'change:updateStatus', this.render);
|
||||
}
|
||||
|
||||
render() {
|
||||
super.render({
|
||||
files: this.model.files,
|
||||
updateAvailable:
|
||||
['ready', 'found'].indexOf(UpdateModel.instance.get('updateStatus')) >= 0
|
||||
['ready', 'found'].indexOf(UpdateModel.updateStatus) >= 0
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -55,14 +55,14 @@ class SettingsGeneralView extends View {
|
|||
|
||||
constructor(model, options) {
|
||||
super(model, options);
|
||||
this.listenTo(UpdateModel.instance, 'change:status', this.render);
|
||||
this.listenTo(UpdateModel.instance, 'change:updateStatus', this.render);
|
||||
this.listenTo(UpdateModel, 'change:status', this.render);
|
||||
this.listenTo(UpdateModel, 'change:updateStatus', this.render);
|
||||
}
|
||||
|
||||
render() {
|
||||
const updateReady = UpdateModel.instance.get('updateStatus') === 'ready';
|
||||
const updateFound = UpdateModel.instance.get('updateStatus') === 'found';
|
||||
const updateManual = UpdateModel.instance.get('updateManual');
|
||||
const updateReady = UpdateModel.updateStatus === 'ready';
|
||||
const updateFound = UpdateModel.updateStatus === 'found';
|
||||
const updateManual = UpdateModel.updateManual;
|
||||
const storageProviders = this.getStorageProviders();
|
||||
|
||||
super.render({
|
||||
|
@ -128,25 +128,25 @@ class SettingsGeneralView extends View {
|
|||
}
|
||||
|
||||
getUpdateInfo() {
|
||||
switch (UpdateModel.instance.get('status')) {
|
||||
switch (UpdateModel.status) {
|
||||
case 'checking':
|
||||
return Locale.setGenUpdateChecking + '...';
|
||||
case 'error': {
|
||||
let errMsg = Locale.setGenErrorChecking;
|
||||
if (UpdateModel.instance.get('lastError')) {
|
||||
errMsg += ': ' + UpdateModel.instance.get('lastError');
|
||||
if (UpdateModel.lastError) {
|
||||
errMsg += ': ' + UpdateModel.lastError;
|
||||
}
|
||||
if (UpdateModel.instance.get('lastSuccessCheckDate')) {
|
||||
if (UpdateModel.lastSuccessCheckDate) {
|
||||
errMsg +=
|
||||
'. ' +
|
||||
Locale.setGenLastCheckSuccess.replace(
|
||||
'{}',
|
||||
DateFormat.dtStr(UpdateModel.instance.get('lastSuccessCheckDate'))
|
||||
DateFormat.dtStr(UpdateModel.lastSuccessCheckDate)
|
||||
) +
|
||||
': ' +
|
||||
Locale.setGenLastCheckVer.replace(
|
||||
'{}',
|
||||
UpdateModel.instance.get('lastVersion')
|
||||
UpdateModel.lastVersion
|
||||
);
|
||||
}
|
||||
return errMsg;
|
||||
|
@ -155,21 +155,21 @@ class SettingsGeneralView extends View {
|
|||
let msg =
|
||||
Locale.setGenCheckedAt +
|
||||
' ' +
|
||||
DateFormat.dtStr(UpdateModel.instance.get('lastCheckDate')) +
|
||||
DateFormat.dtStr(UpdateModel.lastCheckDate) +
|
||||
': ';
|
||||
const cmp = SemVer.compareVersions(
|
||||
RuntimeInfo.version,
|
||||
UpdateModel.instance.get('lastVersion')
|
||||
UpdateModel.lastVersion
|
||||
);
|
||||
if (cmp >= 0) {
|
||||
msg += Locale.setGenLatestVer;
|
||||
} else {
|
||||
msg +=
|
||||
Locale.setGenNewVer.replace('{}', UpdateModel.instance.get('lastVersion')) +
|
||||
Locale.setGenNewVer.replace('{}', UpdateModel.lastVersion) +
|
||||
' ' +
|
||||
DateFormat.dStr(UpdateModel.instance.get('lastVersionReleaseDate'));
|
||||
DateFormat.dStr(UpdateModel.lastVersionReleaseDate);
|
||||
}
|
||||
switch (UpdateModel.instance.get('updateStatus')) {
|
||||
switch (UpdateModel.updateStatus) {
|
||||
case 'downloading':
|
||||
return msg + '. ' + Locale.setGenDownloadingUpdate;
|
||||
case 'extracting':
|
||||
|
|
Loading…
Reference in New Issue