1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-06-15 23:00:36 +02:00

Made dispatching of sync events more consistent

This commit is contained in:
Laurent Cozic
2017-07-17 23:43:29 +01:00
parent 268dfec554
commit e19ed03b90
5 changed files with 76 additions and 67 deletions

View File

@ -7,7 +7,6 @@ import { sprintf } from 'sprintf-js';
import { time } from 'lib/time-utils.js';
import { Logger } from 'lib/logger.js'
import { _ } from 'lib/locale.js';
import { EventDispatcher } from 'lib/event-dispatcher.js';
import moment from 'moment';
class Synchronizer {
@ -25,15 +24,7 @@ class Synchronizer {
this.onProgress_ = function(s) {};
this.progressReport_ = {};
this.dispatcher_ = new EventDispatcher();
}
on(eventName, callback) {
return this.dispatcher_.on(eventName, callback);
}
off(eventName, callback) {
return this.dispatcher_.off(eventName, callback);
this.dispatch = function(action) {};
}
state() {
@ -56,7 +47,7 @@ class Synchronizer {
return this.logger_;
}
reportToLines(report) {
static reportToLines(report) {
let lines = [];
if (report.createLocal) lines.push(_('Created local items: %d.', report.createLocal));
if (report.updateLocal) lines.push(_('Updated local items: %d.', report.updateLocal));
@ -66,7 +57,9 @@ class Synchronizer {
if (report.deleteRemote) lines.push(_('Deleted remote items: %d.', report.deleteRemote));
if (report.state) lines.push(_('State: %s.', report.state.replace(/_/g, ' ')));
if (report.errors && report.errors.length) lines.push(_('Last error: %s (stacktrace in log).', report.errors[report.errors.length-1].message));
if (report.cancelling && !report.completedTime) lines.push(_('Cancelling...'));
if (report.completedTime) lines.push(_('Completed: %s', time.unixMsToLocalDateTime(report.completedTime)));
return lines;
}
@ -101,7 +94,7 @@ class Synchronizer {
this.progressReport_.state = this.state();
this.onProgress_(this.progressReport_);
this.dispatcher_.dispatch('progress', this.progressReport_);
this.dispatch({ type: 'SYNC_REPORT_UPDATE', report: Object.assign({}, this.progressReport_) });
}
async logSyncSummary(report) {
@ -141,7 +134,7 @@ class Synchronizer {
cancel() {
if (this.cancelling_) return;
this.logger().info('Cancelling synchronization...');
this.logSyncOperation('cancelling', null, null, '');
this.cancelling_ = true;
}
@ -173,11 +166,13 @@ class Synchronizer {
this.state_ = 'in_progress';
this.dispatch({ type: 'SYNC_STARTED' });
this.logSyncOperation('starting', null, null, 'Starting synchronization to ' + this.api().driver().syncTargetName() + ' (' + syncTargetId + ')... [' + synchronizationId + ']');
try {
await this.api().mkdir(this.syncDirName_);
await this.api().mkdir(this.resourceDirName_);
await this.api().mkdir(this.resourceDirName_);
let donePaths = [];
while (true) {
@ -442,16 +437,16 @@ class Synchronizer {
this.state_ = 'idle';
this.logSyncOperation('finished', null, null, 'Synchronization finished [' + synchronizationId + ']');
this.progressReport_.completedTime = time.unixMs();
this.logSyncOperation('finished', null, null, 'Synchronization finished [' + synchronizationId + ']');
await this.logSyncSummary(this.progressReport_);
this.onProgress_ = function(s) {};
this.progressReport_ = {};
this.dispatcher_.dispatch('complete');
this.dispatch({ type: 'SYNC_COMPLETED' });
}
}