You've already forked joplin
							
							
				mirror of
				https://github.com/laurent22/joplin.git
				synced 2025-10-31 00:07:48 +02:00 
			
		
		
		
	Sync progress report
This commit is contained in:
		| @@ -328,12 +328,34 @@ commands.push({ | ||||
| 	usage: 'sync', | ||||
| 	description: 'Synchronizes with remote storage.', | ||||
| 	action: function(args, end) { | ||||
|  | ||||
| 		let redrawnCalled = false; | ||||
|  | ||||
| 		let options = { | ||||
| 			onProgress: (report) => { | ||||
| 				let line = []; | ||||
| 				if (report.remotesToUpdate) line.push(_('Items to upload: %d/%d.', report.createRemote + report.updateRemote, report.remotesToUpdate)); | ||||
| 				if (report.remotesToDelete) line.push(_('Remote items to delete: %d/%d.', report.deleteRemote, report.remotesToDelete)); | ||||
| 				if (report.localsToUdpate) line.push(_('Items to download: %d/%d.', report.createLocal + report.updateLocal, report.localsToUdpate)); | ||||
| 				if (report.localsToDelete) line.push(_('Local items to delete: %d/%d.', report.deleteLocal, report.localsToDelete)); | ||||
| 				redrawnCalled = true; | ||||
| 				vorpal.ui.redraw(line.join(' '));				 | ||||
| 			}, | ||||
| 			onMessage: (msg) => { | ||||
| 				if (redrawnCalled) vorpal.ui.redraw.done(); | ||||
| 				this.log(msg); | ||||
| 			}, | ||||
| 		}; | ||||
|  | ||||
| 		this.log(_('Synchronization target: %s', Setting.value('sync.target'))); | ||||
| 		synchronizer(Setting.value('sync.target')).then((s) => { | ||||
| 			return s.start(); | ||||
| 			this.log(_('Starting synchronization...')); | ||||
| 			return s.start(options); | ||||
| 		}).catch((error) => { | ||||
| 			this.log(error); | ||||
| 		}).then(() => { | ||||
| 			if (redrawnCalled) vorpal.ui.redraw.done(); | ||||
| 			this.log(_('Done.')); | ||||
| 			end(); | ||||
| 		}); | ||||
| 	}, | ||||
|   | ||||
| @@ -77,7 +77,10 @@ class Synchronizer { | ||||
| 		return this.syncDirName_; | ||||
| 	} | ||||
|  | ||||
| 	async start() { | ||||
| 	async start(options = null) { | ||||
| 		if (!options) options = {}; | ||||
| 		if (!options.onProgress) options.onProgress = function(o) {}; | ||||
|  | ||||
| 		if (this.state() != 'idle') { | ||||
| 			this.logger().warn('Synchronization is already in progress. State: ' + this.state()); | ||||
| 			return; | ||||
| @@ -94,6 +97,11 @@ class Synchronizer { | ||||
| 		this.state_ = 'started'; | ||||
|  | ||||
| 		let report = { | ||||
| 			remotesToUpdate: 0, | ||||
| 			remotesToDelete: 0, | ||||
| 			localsToUdpate: 0, | ||||
| 			localsToDelete: 0, | ||||
|  | ||||
| 			createLocal: 0, | ||||
| 			updateLocal: 0, | ||||
| 			deleteLocal: 0, | ||||
| @@ -112,6 +120,9 @@ class Synchronizer { | ||||
| 				let result = await BaseItem.itemsThatNeedSync(); | ||||
| 				let locals = result.items; | ||||
|  | ||||
| 				report.remotesToUpdate += locals.length; | ||||
| 				options.onProgress(report); | ||||
|  | ||||
| 				for (let i = 0; i < locals.length; i++) { | ||||
| 					let local = locals[i]; | ||||
| 					let ItemClass = BaseItem.itemClass(local); | ||||
| @@ -196,6 +207,8 @@ class Synchronizer { | ||||
| 					report[action]++; | ||||
|  | ||||
| 					donePaths.push(path); | ||||
|  | ||||
| 					options.onProgress(report); | ||||
| 				} | ||||
|  | ||||
| 				if (!result.hasMore) break; | ||||
| @@ -206,6 +219,8 @@ class Synchronizer { | ||||
| 			// ------------------------------------------------------------------------ | ||||
|  | ||||
| 			let deletedItems = await BaseModel.deletedItems(); | ||||
| 			report.remotesToDelete = deletedItems.length; | ||||
| 			options.onProgress(report); | ||||
| 			for (let i = 0; i < deletedItems.length; i++) { | ||||
| 				let item = deletedItems[i]; | ||||
| 				let path = BaseItem.systemPath(item.item_id) | ||||
| @@ -214,6 +229,7 @@ class Synchronizer { | ||||
| 				await BaseModel.remoteDeletedItem(item.item_id); | ||||
|  | ||||
| 				report['deleteRemote']++; | ||||
| 				options.onProgress(report); | ||||
| 			} | ||||
|  | ||||
| 			// ------------------------------------------------------------------------ | ||||
| @@ -252,6 +268,9 @@ class Synchronizer { | ||||
|  | ||||
| 					if (!action) continue; | ||||
|  | ||||
| 					report.localsToUdpate++; | ||||
| 					options.onProgress(report); | ||||
|  | ||||
| 					if (action == 'createLocal' || action == 'updateLocal') { | ||||
| 						let content = await this.api().get(path); | ||||
| 						if (content === null) { | ||||
| @@ -273,6 +292,8 @@ class Synchronizer { | ||||
| 					} | ||||
|  | ||||
| 					report[action]++; | ||||
|  | ||||
| 					options.onProgress(report); | ||||
| 				} | ||||
|  | ||||
| 				if (!listResult.hasMore) break; | ||||
| @@ -288,9 +309,12 @@ class Synchronizer { | ||||
| 			for (let i = 0; i < noteIds.length; i++) { | ||||
| 				let noteId = noteIds[i]; | ||||
| 				if (remoteIds.indexOf(noteId) < 0) { | ||||
| 					report.localsToDelete++; | ||||
| 					options.onProgress(report); | ||||
| 					this.logSyncOperation('deleteLocal', { id: noteId }, null, 'remote has been deleted'); | ||||
| 					await Note.delete(noteId, { trackDeleted: false }); | ||||
| 					report['deleteLocal']++; | ||||
| 					options.onProgress(report); | ||||
| 				} | ||||
| 			} | ||||
| 		} catch (error) { | ||||
| @@ -298,6 +322,8 @@ class Synchronizer { | ||||
| 			throw error; | ||||
| 		} | ||||
|  | ||||
| 		options.onProgress(report); | ||||
|  | ||||
| 		this.logger().info('Synchronization complete [' + synchronizationId + ']:'); | ||||
| 		await this.logSyncSummary(report); | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user