2017-12-14 21:39:13 +02:00
|
|
|
const EncryptionService = require('lib/services/EncryptionService.js');
|
2017-11-24 01:10:55 +02:00
|
|
|
|
|
|
|
class BaseSyncTarget {
|
2017-11-24 20:37:40 +02:00
|
|
|
constructor(db, options = null) {
|
2017-11-24 01:10:55 +02:00
|
|
|
this.db_ = db;
|
|
|
|
this.synchronizer_ = null;
|
|
|
|
this.initState_ = null;
|
|
|
|
this.logger_ = null;
|
2017-11-24 20:37:40 +02:00
|
|
|
this.options_ = options;
|
|
|
|
}
|
|
|
|
|
2018-02-06 20:59:36 +02:00
|
|
|
static supportsConfigCheck() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-10-08 08:36:45 +02:00
|
|
|
static resourceDirName() {
|
|
|
|
return '.resource';
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:37:40 +02: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-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
|
|
|
}
|
|
|
|
|
|
|
|
setLogger(v) {
|
|
|
|
this.logger_ = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
db() {
|
|
|
|
return this.db_;
|
|
|
|
}
|
|
|
|
|
2020-02-08 13:59:19 +02:00
|
|
|
// If [] is returned it means all platforms are supported
|
|
|
|
static unsupportedPlatforms() {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-03-26 19:33:55 +02:00
|
|
|
async isAuthenticated() {
|
2017-11-24 01:10:55 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-25 21:01:14 +02:00
|
|
|
authRouteName() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:59:16 +02:00
|
|
|
static id() {
|
|
|
|
throw new Error('id() not implemented');
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 21:21:30 +02: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 20:59:16 +02:00
|
|
|
static label() {
|
|
|
|
throw new Error('label() not implemented');
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async initSynchronizer() {
|
2017-11-24 20:59:16 +02:00
|
|
|
throw new Error('initSynchronizer() not implemented');
|
2017-11-24 01:10:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 20:59:16 +02:00
|
|
|
async initFileApi() {
|
|
|
|
throw new Error('initFileApi() not implemented');
|
2017-11-24 20:37:40 +02:00
|
|
|
}
|
|
|
|
|
2017-11-24 20:59:16 +02:00
|
|
|
async fileApi() {
|
2017-11-24 20:37:40 +02:00
|
|
|
if (this.fileApi_) return this.fileApi_;
|
2017-11-24 20:59:16 +02:00
|
|
|
this.fileApi_ = await this.initFileApi();
|
2017-11-24 20:37:40 +02:00
|
|
|
return this.fileApi_;
|
|
|
|
}
|
|
|
|
|
2018-03-24 21:35:10 +02:00
|
|
|
fileApiSync() {
|
|
|
|
return this.fileApi_;
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:37:40 +02: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-24 01:10:55 +02: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 21:39:13 +02:00
|
|
|
this.synchronizer_.setEncryptionService(EncryptionService.instance());
|
2017-11-24 01:10:55 +02: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-24 01:10:55 +02:00
|
|
|
const sync = await this.synchronizer();
|
|
|
|
return sync.state() != 'idle';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-13 00:16:42 +02:00
|
|
|
BaseSyncTarget.dispatch = () => {};
|
2017-11-24 20:37:40 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = BaseSyncTarget;
|