From efb354667526a32dcfcda28feff308c720f383c4 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 8 Dec 2020 19:03:22 +0000 Subject: [PATCH] Plugins: Add support for workspace.onSyncStart event --- .../tests/support/plugins/events/src/index.ts | 7 ++++++- packages/lib/Synchronizer.ts | 14 +++++++++++--- .../services/plugins/api/JoplinWorkspace.ts | 18 ++++++++++++++++-- 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/packages/app-cli/tests/support/plugins/events/src/index.ts b/packages/app-cli/tests/support/plugins/events/src/index.ts index 1e30ee143..e6b0006d3 100644 --- a/packages/app-cli/tests/support/plugins/events/src/index.ts +++ b/packages/app-cli/tests/support/plugins/events/src/index.ts @@ -7,8 +7,13 @@ joplin.plugins.register({ console.info('Alarm was triggered for note: ', note); }); - joplin.workspace.onSyncComplete(async () => { + joplin.workspace.onSyncStart(async (event:any) => { + console.info('Sync has started...'); + }); + + joplin.workspace.onSyncComplete(async (event:any) => { console.info('Sync has completed'); + console.info('With errors:', event.withErrors); }); }, }); diff --git a/packages/lib/Synchronizer.ts b/packages/lib/Synchronizer.ts index af64950ea..675a45471 100644 --- a/packages/lib/Synchronizer.ts +++ b/packages/lib/Synchronizer.ts @@ -121,6 +121,10 @@ export default class Synchronizer { } } + private static reportHasErrors(report: any): boolean { + return !!report && !!report.errors && !!report.errors.length; + } + static reportToLines(report: any) { const lines = []; if (report.createLocal) lines.push(_('Created local items: %d.', report.createLocal)); @@ -132,7 +136,7 @@ export default class Synchronizer { if (report.fetchingTotal && report.fetchingProcessed) lines.push(_('Fetched items: %d/%d.', report.fetchingProcessed, report.fetchingTotal)); if (report.cancelling && !report.completedTime) lines.push(_('Cancelling...')); if (report.completedTime) lines.push(_('Completed: %s', time.formatMsToLocal(report.completedTime))); - if (report.errors && report.errors.length) lines.push(_('Last error: %s', report.errors[report.errors.length - 1].toString().substr(0, 500))); + if (this.reportHasErrors(report)) lines.push(_('Last error: %s', report.errors[report.errors.length - 1].toString().substr(0, 500))); return lines; } @@ -193,7 +197,7 @@ export default class Synchronizer { this.logger().info(`Total notes: ${noteCount}`); this.logger().info(`Total resources: ${resourceCount}`); - if (report.errors && report.errors.length) { + if (Synchronizer.reportHasErrors(report)) { this.logger().warn('There was some errors:'); for (let i = 0; i < report.errors.length; i++) { const e = report.errors[i]; @@ -315,6 +319,7 @@ export default class Synchronizer { const outputContext = Object.assign({}, lastContext); this.dispatch({ type: 'SYNC_STARTED' }); + eventManager.emit('syncStart'); this.logSyncOperation('starting', null, null, `Starting synchronisation to target ${syncTargetId}... [${synchronizationId}]`); @@ -899,11 +904,14 @@ export default class Synchronizer { await this.logSyncSummary(this.progressReport_); + eventManager.emit('syncComplete', { + withErrors: Synchronizer.reportHasErrors(this.progressReport_), + }); + this.onProgress_ = function() {}; this.progressReport_ = {}; this.dispatch({ type: 'SYNC_COMPLETED', isFullSync: this.isFullSync(syncSteps) }); - eventManager.emit('syncComplete'); this.state_ = 'idle'; diff --git a/packages/lib/services/plugins/api/JoplinWorkspace.ts b/packages/lib/services/plugins/api/JoplinWorkspace.ts index e1f02400e..50489f53d 100644 --- a/packages/lib/services/plugins/api/JoplinWorkspace.ts +++ b/packages/lib/services/plugins/api/JoplinWorkspace.ts @@ -19,7 +19,14 @@ interface ItemChangeEvent { event: ItemChangeEventType; } +interface SyncStartEvent { + // Tells whether there were errors during sync or not. The log will + // have the complete information about any error. + withErrors: boolean; +} + type ItemChangeHandler = (event: ItemChangeEvent)=> void; +type SyncStartHandler = (event: SyncStartEvent)=> void; /** * The workspace service provides access to all the parts of Joplin that @@ -80,8 +87,15 @@ export default class JoplinWorkspace { /** * Called when an alarm associated with a to-do is triggered. */ - public async onNoteAlarmTrigger(callback: Function): Promise { - return makeListener(eventManager, 'noteAlarmTrigger', callback); + public async onNoteAlarmTrigger(handler: Function): Promise { + return makeListener(eventManager, 'noteAlarmTrigger', handler); + } + + /** + * Called when the synchronisation process is starting. + */ + public async onSyncStart(handler: SyncStartHandler): Promise { + return makeListener(eventManager, 'syncStart', handler); } /**