1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-01-05 12:50:29 +02:00
joplin/ReactNativeClient/lib/registry.js

131 lines
3.2 KiB
JavaScript
Raw Normal View History

const { Logger } = require('lib/logger.js');
const { Setting } = require('lib/models/setting.js');
const { shim } = require('lib/shim.js');
const SyncTargetRegistry = require('lib/SyncTargetRegistry.js');
const { _ } = require('lib/locale.js');
2017-07-06 21:48:17 +02:00
const reg = {};
reg.syncTargets_ = {};
2017-07-31 20:32:51 +02:00
2017-07-06 21:48:17 +02:00
reg.logger = () => {
2017-07-15 01:12:32 +02:00
if (!reg.logger_) {
2017-08-22 19:57:35 +02:00
//console.warn('Calling logger before it is initialized');
2017-07-15 01:12:32 +02:00
return new Logger();
}
2017-07-06 21:48:17 +02: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 21:48:17 +02:00
const SyncTargetClass = SyncTargetRegistry.classById(syncTargetId);
if (!reg.db()) throw new Error('Cannot initialize sync without a db');
2017-07-06 21:48:17 +02:00
const target = new SyncTargetClass(reg.db());
target.setLogger(reg.logger());
reg.syncTargets_[syncTargetId] = target;
return target;
2017-07-06 21:48:17 +02:00
}
2017-07-19 21:15:55 +02:00
reg.scheduleSync = async (delay = null) => {
2017-11-03 20:51:13 +02:00
if (delay === null) delay = 1000 * 3;
2017-07-19 21:15:55 +02:00
let promiseResolve = null;
const promise = new Promise((resolve, reject) => {
promiseResolve = resolve;
});
2017-07-17 22:22:05 +02:00
if (reg.scheduleSyncId_) {
clearTimeout(reg.scheduleSyncId_);
reg.scheduleSyncId_ = null;
}
2017-07-16 23:17:22 +02:00
reg.logger().info('Scheduling sync operation...');
2017-11-03 20:51:13 +02:00
// if (Setting.value('env') === 'dev') {
// reg.logger().info('Scheduling sync operation DISABLED!!!');
// return;
// }
2017-07-24 22:36:49 +02:00
const timeoutCallback = async () => {
2017-07-16 23:17:22 +02:00
reg.scheduleSyncId_ = null;
reg.logger().info('Doing scheduled sync');
const syncTargetId = Setting.value('sync.target');
if (!reg.syncTarget(syncTargetId).isAuthenticated()) {
2017-07-26 23:27:03 +02:00
reg.logger().info('Synchroniser is missing credentials - manual sync required to authenticate.');
promiseResolve();
2017-07-16 23:17:22 +02:00
return;
}
2017-07-24 21:47:01 +02:00
try {
const sync = await reg.syncTarget(syncTargetId).synchronizer();
2017-07-30 22:22:57 +02:00
2017-08-19 22:56:28 +02:00
const contextKey = 'sync.' + syncTargetId + '.context';
let context = Setting.value(contextKey);
2017-07-30 22:22:57 +02:00
context = context ? JSON.parse(context) : {};
try {
let newContext = await sync.start({ context: context });
2017-08-19 22:56:28 +02:00
Setting.setValue(contextKey, JSON.stringify(newContext));
2017-07-30 22:22:57 +02:00
} catch (error) {
if (error.code == 'alreadyStarted') {
reg.logger().info(error.message);
} else {
promiseResolve();
2017-07-30 22:22:57 +02:00
throw error;
}
2017-07-24 21:47:01 +02:00
}
2017-07-30 22:22:57 +02:00
} catch (error) {
reg.logger().info('Could not run background sync: ');
reg.logger().info(error);
2017-07-24 21:47:01 +02:00
}
reg.setupRecurrentSync();
promiseResolve();
2017-07-24 22:36:49 +02:00
};
if (delay === 0) {
timeoutCallback();
} else {
reg.scheduleSyncId_ = setTimeout(timeoutCallback, delay);
}
return promise;
2017-07-16 23:17:22 +02:00
}
reg.setupRecurrentSync = () => {
if (reg.recurrentSyncId_) {
2017-10-19 00:13:53 +02:00
shim.clearInterval(reg.recurrentSyncId_);
reg.recurrentSyncId_ = null;
}
2017-08-20 16:29:18 +02: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-19 00:13:53 +02:00
reg.recurrentSyncId_ = shim.setInterval(() => {
2017-08-20 16:29:18 +02:00
reg.logger().info('Running background sync on timer...');
reg.scheduleSync(0);
}, 1000 * Setting.value('sync.interval'));
}
}
2017-07-06 21:48:17 +02:00
reg.setDb = (v) => {
reg.db_ = v;
}
reg.db = () => {
return reg.db_;
}
2017-11-03 02:13:17 +02:00
module.exports = { reg };