| 
									
										
										
										
											2017-12-14 19:39:13 +00:00
										 |  |  | const EncryptionService = require('lib/services/EncryptionService.js'); | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | class BaseSyncTarget { | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 	constructor(db, options = null) { | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 		this.db_ = db; | 
					
						
							|  |  |  | 		this.synchronizer_ = null; | 
					
						
							|  |  |  | 		this.initState_ = null; | 
					
						
							|  |  |  | 		this.logger_ = null; | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 		this.options_ = options; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-06 18:59:36 +00:00
										 |  |  | 	static supportsConfigCheck() { | 
					
						
							|  |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-08 07:36:45 +01:00
										 |  |  | 	static resourceDirName() { | 
					
						
							|  |  |  | 		return '.resource'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 	option(name, defaultValue = null) { | 
					
						
							| 
									
										
										
										
											2019-07-29 15:43:53 +02:00
										 |  |  | 		return this.options_ && name in this.options_ ? this.options_[name] : defaultValue; | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	logger() { | 
					
						
							|  |  |  | 		return this.logger_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	setLogger(v) { | 
					
						
							|  |  |  | 		this.logger_ = v; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	db() { | 
					
						
							|  |  |  | 		return this.db_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-26 18:33:55 +01:00
										 |  |  | 	async isAuthenticated() { | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 		return false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 19:01:14 +00:00
										 |  |  | 	authRouteName() { | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 18:59:16 +00:00
										 |  |  | 	static id() { | 
					
						
							|  |  |  | 		throw new Error('id() not implemented'); | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 19:21:30 +00:00
										 |  |  | 	// Note: it cannot be called just "name()" because that's a reserved keyword and
 | 
					
						
							|  |  |  | 	// it would throw an obscure error in React Native.
 | 
					
						
							|  |  |  | 	static targetName() { | 
					
						
							|  |  |  | 		throw new Error('targetName() not implemented'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 18:59:16 +00:00
										 |  |  | 	static label() { | 
					
						
							|  |  |  | 		throw new Error('label() not implemented'); | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async initSynchronizer() { | 
					
						
							| 
									
										
										
										
											2017-11-24 18:59:16 +00:00
										 |  |  | 		throw new Error('initSynchronizer() not implemented'); | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 18:59:16 +00:00
										 |  |  | 	async initFileApi() { | 
					
						
							|  |  |  | 		throw new Error('initFileApi() not implemented'); | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 18:59:16 +00:00
										 |  |  | 	async fileApi() { | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 		if (this.fileApi_) return this.fileApi_; | 
					
						
							| 
									
										
										
										
											2017-11-24 18:59:16 +00:00
										 |  |  | 		this.fileApi_ = await this.initFileApi(); | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 		return this.fileApi_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-24 19:35:10 +00:00
										 |  |  | 	fileApiSync() { | 
					
						
							|  |  |  | 		return this.fileApi_; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 	// Usually each sync target should create and setup its own file API via initFileApi()
 | 
					
						
							|  |  |  | 	// but for testing purposes it might be convenient to provide it here so that multiple
 | 
					
						
							|  |  |  | 	// clients can share and sync to the same file api (see test-utils.js)
 | 
					
						
							|  |  |  | 	setFileApi(v) { | 
					
						
							|  |  |  | 		this.fileApi_ = v; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 	async synchronizer() { | 
					
						
							|  |  |  | 		if (this.synchronizer_) return this.synchronizer_; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (this.initState_ == 'started') { | 
					
						
							|  |  |  | 			// Synchronizer is already being initialized, so wait here till it's done.
 | 
					
						
							|  |  |  | 			return new Promise((resolve, reject) => { | 
					
						
							|  |  |  | 				const iid = setInterval(() => { | 
					
						
							|  |  |  | 					if (this.initState_ == 'ready') { | 
					
						
							|  |  |  | 						clearInterval(iid); | 
					
						
							|  |  |  | 						resolve(this.synchronizer_); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 					if (this.initState_ == 'error') { | 
					
						
							|  |  |  | 						clearInterval(iid); | 
					
						
							|  |  |  | 						reject(new Error('Could not initialise synchroniser')); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}, 1000); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			this.initState_ = 'started'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				this.synchronizer_ = await this.initSynchronizer(); | 
					
						
							|  |  |  | 				this.synchronizer_.setLogger(this.logger()); | 
					
						
							| 
									
										
										
										
											2017-12-14 19:39:13 +00:00
										 |  |  | 				this.synchronizer_.setEncryptionService(EncryptionService.instance()); | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 				this.synchronizer_.dispatch = BaseSyncTarget.dispatch; | 
					
						
							|  |  |  | 				this.initState_ = 'ready'; | 
					
						
							|  |  |  | 				return this.synchronizer_; | 
					
						
							|  |  |  | 			} catch (error) { | 
					
						
							|  |  |  | 				this.initState_ = 'error'; | 
					
						
							|  |  |  | 				throw error; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async syncStarted() { | 
					
						
							|  |  |  | 		if (!this.synchronizer_) return false; | 
					
						
							| 
									
										
										
										
											2019-07-29 15:43:53 +02:00
										 |  |  | 		if (!(await this.isAuthenticated())) return false; | 
					
						
							| 
									
										
										
										
											2017-11-23 23:10:55 +00:00
										 |  |  | 		const sync = await this.synchronizer(); | 
					
						
							|  |  |  | 		return sync.state() != 'idle'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-12 22:16:42 +00:00
										 |  |  | BaseSyncTarget.dispatch = () => {}; | 
					
						
							| 
									
										
										
										
											2017-11-24 18:37:40 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-29 15:43:53 +02:00
										 |  |  | module.exports = BaseSyncTarget; |