joplin/ReactNativeClient/lib/registry.js

174 lines
4.9 KiB
JavaScript
Raw Normal View History

const { Logger } = require('lib/logger.js');
const { Setting } = require('lib/models/setting.js');
const { parameters } = require('lib/parameters.js');
const { FileApi } = require('lib/file-api.js');
const { Database } = require('lib/database.js');
const { Synchronizer } = require('lib/synchronizer.js');
const { FileApiDriverOneDrive } = require('lib/file-api-driver-onedrive.js');
const { shim } = require('lib/shim.js');
const { time } = require('lib/time-utils.js');
const { FileApiDriverMemory } = require('lib/file-api-driver-memory.js');
const { _ } = require('lib/locale.js');
2017-07-06 19:48:17 +00:00
const reg = {};
2017-07-31 18:32:51 +00:00
reg.initSynchronizerStates_ = {};
reg.syncTargetClasses_ = {
1: require('lib/SyncTarget1.js'),
// 2: require('lib/SyncTarget2.js'),
3: require('lib/SyncTarget3.js'),
};
reg.syncTargets_ = {};
//reg.synchronizers_ = {};
2017-07-31 18:32:51 +00:00
2017-07-06 19:48:17 +00:00
reg.logger = () => {
2017-07-14 23:12:32 +00:00
if (!reg.logger_) {
2017-08-22 17:57:35 +00:00
//console.warn('Calling logger before it is initialized');
2017-07-14 23:12:32 +00:00
return new Logger();
}
2017-07-06 19:48:17 +00:00
return reg.logger_;
}
reg.setLogger = (l) => {
reg.logger_ = l;
}
reg.syncTarget = (syncTargetId = null) => {
if (syncTargetId === null) syncTargetId = Setting.value('sync.target');
if (reg.syncTargets_[syncTargetId]) return reg.syncTargets_[syncTargetId];
2017-07-06 19:48:17 +00:00
const SyncTargetClass = reg.syncTargetClasses_[syncTargetId];
if (!reg.db()) throw new Error('Cannot initialize sync without a db');
2017-07-06 19:48:17 +00:00
const target = new SyncTargetClass(reg.db());
target.setLogger(reg.logger());
reg.syncTargets_[syncTargetId] = target;
return target;
2017-07-06 19:48:17 +00:00
}
// reg.synchronizer = async (syncTargetId) => {
// if (reg.synchronizers_[syncTargetId]) return reg.synchronizers_[syncTargetId];
// if (!reg.db()) throw new Error('Cannot initialize synchronizer: db not initialized');
// if (reg.initSynchronizerStates_[syncTargetId] == 'started') {
// // Synchronizer is already being initialized, so wait here till it's done.
// return new Promise((resolve, reject) => {
// const iid = setInterval(() => {
// if (reg.initSynchronizerStates_[syncTargetId] == 'ready') {
// clearInterval(iid);
// resolve(reg.synchronizers_[syncTargetId]);
// }
// if (reg.initSynchronizerStates_[syncTargetId] == 'error') {
// clearInterval(iid);
// reject(new Error('Could not initialise synchroniser'));
// }
// }, 1000);
// });
// } else {
// reg.initSynchronizerStates_[syncTargetId] = 'started';
// try {
// const sync = await reg.initSynchronizer_(syncTargetId);
// reg.synchronizers_[syncTargetId] = sync;
// reg.initSynchronizerStates_[syncTargetId] = 'ready';
// return sync;
// } catch (error) {
// reg.initSynchronizerStates_[syncTargetId] = 'error';
// throw error;
// }
// }
// }
// reg.syncHasAuth = (syncTargetId) => {
// return reg.syncTarget(syncTargetId).isAuthenticated();
// }
2017-07-19 19:15:55 +00:00
reg.scheduleSync = async (delay = null) => {
2017-11-03 18:51:13 +00:00
if (delay === null) delay = 1000 * 3;
2017-07-19 19:15:55 +00:00
2017-07-17 20:22:05 +00:00
if (reg.scheduleSyncId_) {
clearTimeout(reg.scheduleSyncId_);
reg.scheduleSyncId_ = null;
}
2017-07-16 21:17:22 +00:00
reg.logger().info('Scheduling sync operation...');
2017-11-03 18:51:13 +00:00
// if (Setting.value('env') === 'dev') {
// reg.logger().info('Scheduling sync operation DISABLED!!!');
// return;
// }
2017-07-24 20:36:49 +00:00
const timeoutCallback = async () => {
2017-07-16 21:17:22 +00:00
reg.scheduleSyncId_ = null;
reg.logger().info('Doing scheduled sync');
const syncTargetId = Setting.value('sync.target');
if (!reg.syncTarget(syncTargetId).isAuthenticated()) {
2017-07-26 21:27:03 +00:00
reg.logger().info('Synchroniser is missing credentials - manual sync required to authenticate.');
2017-07-16 21:17:22 +00:00
return;
}
2017-07-24 19:47:01 +00:00
try {
const sync = await reg.syncTarget(syncTargetId).synchronizer();
2017-07-30 20:22:57 +00:00
2017-08-19 20:56:28 +00:00
const contextKey = 'sync.' + syncTargetId + '.context';
let context = Setting.value(contextKey);
2017-07-30 20:22:57 +00:00
context = context ? JSON.parse(context) : {};
try {
let newContext = await sync.start({ context: context });
2017-08-19 20:56:28 +00:00
Setting.setValue(contextKey, JSON.stringify(newContext));
2017-07-30 20:22:57 +00:00
} catch (error) {
if (error.code == 'alreadyStarted') {
reg.logger().info(error.message);
} else {
throw error;
}
2017-07-24 19:47:01 +00:00
}
2017-07-30 20:22:57 +00:00
} catch (error) {
reg.logger().info('Could not run background sync: ');
reg.logger().info(error);
2017-07-24 19:47:01 +00:00
}
reg.setupRecurrentSync();
2017-07-24 20:36:49 +00:00
};
if (delay === 0) {
timeoutCallback();
} else {
reg.scheduleSyncId_ = setTimeout(timeoutCallback, delay);
}
2017-07-16 21:17:22 +00:00
}
// reg.syncStarted = async () => {
// return reg.syncTarget().syncStarted();
// }
reg.setupRecurrentSync = () => {
if (reg.recurrentSyncId_) {
2017-10-18 22:13:53 +00:00
shim.clearInterval(reg.recurrentSyncId_);
reg.recurrentSyncId_ = null;
}
2017-08-20 14:29:18 +00:00
if (!Setting.value('sync.interval')) {
reg.logger().debug('Recurrent sync is disabled');
} else {
reg.logger().debug('Setting up recurrent sync with interval ' + Setting.value('sync.interval'));
2017-10-18 22:13:53 +00:00
reg.recurrentSyncId_ = shim.setInterval(() => {
2017-08-20 14:29:18 +00:00
reg.logger().info('Running background sync on timer...');
reg.scheduleSync(0);
}, 1000 * Setting.value('sync.interval'));
}
}
2017-07-06 19:48:17 +00:00
reg.setDb = (v) => {
reg.db_ = v;
}
reg.db = () => {
return reg.db_;
}
2017-11-03 00:13:17 +00:00
module.exports = { reg };