1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-21 09:38:01 +02:00
joplin/ReactNativeClient/lib/registry.js

234 lines
6.2 KiB
JavaScript
Raw Normal View History

const Logger = require('lib/Logger').default;
const Setting = require('lib/models/Setting').default;
const shim = require('lib/shim').default;
const SyncTargetRegistry = require('lib/SyncTargetRegistry.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_) {
2019-10-09 21:35:13 +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_;
2019-07-29 15:43:53 +02:00
};
2017-07-06 21:48:17 +02:00
reg.setLogger = l => {
reg.logger_ = l;
2019-07-29 15:43:53 +02:00
};
reg.setShowErrorMessageBoxHandler = v => {
reg.showErrorMessageBoxHandler_ = v;
2019-07-29 15:43:53 +02:00
};
reg.showErrorMessageBox = message => {
if (!reg.showErrorMessageBoxHandler_) return;
reg.showErrorMessageBoxHandler_(message);
2019-07-29 15:43:53 +02:00
};
reg.resetSyncTarget = (syncTargetId = null) => {
if (syncTargetId === null) syncTargetId = Setting.value('sync.target');
delete reg.syncTargets_[syncTargetId];
2019-07-29 15:43:53 +02:00
};
reg.syncTargetNextcloud = () => {
return reg.syncTarget(SyncTargetRegistry.nameToId('nextcloud'));
};
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;
2019-07-29 15:43:53 +02:00
};
2017-07-06 21:48:17 +02:00
// This can be used when some data has been modified and we want to make
// sure it gets synced. So we wait for the current sync operation to
// finish (if one is running), then we trigger a sync just after.
reg.waitForSyncFinishedThenSync = async () => {
reg.waitForReSyncCalls_.push(true);
try {
const synchronizer = await reg.syncTarget().synchronizer();
await synchronizer.waitForSyncToFinish();
await reg.scheduleSync(0);
} finally {
reg.waitForReSyncCalls_.pop();
}
};
reg.scheduleSync = async (delay = null, syncOptions = null) => {
reg.schedSyncCalls_.push(true);
2017-07-16 23:17:22 +02:00
try {
if (delay === null) delay = 1000 * 10;
if (syncOptions === null) syncOptions = {};
2017-07-16 23:17:22 +02:00
let promiseResolve = null;
const promise = new Promise((resolve) => {
promiseResolve = resolve;
});
2017-11-03 20:51:13 +02:00
if (reg.scheduleSyncId_) {
shim.clearTimeout(reg.scheduleSyncId_);
reg.scheduleSyncId_ = null;
}
2017-07-16 23:17:22 +02:00
reg.logger().debug('Scheduling sync operation...', delay);
if (Setting.value('env') === 'dev' && delay !== 0) {
reg.logger().info('Schedule sync DISABLED!!!');
2017-07-16 23:17:22 +02:00
return;
}
const timeoutCallback = async () => {
reg.timerCallbackCalls_.push(true);
try {
reg.scheduleSyncId_ = null;
reg.logger().info('Preparing scheduled sync');
const syncTargetId = Setting.value('sync.target');
if (!(await reg.syncTarget(syncTargetId).isAuthenticated())) {
reg.logger().info('Synchroniser is missing credentials - manual sync required to authenticate.');
promiseResolve();
return;
2017-07-30 22:22:57 +02:00
}
try {
const sync = await reg.syncTarget(syncTargetId).synchronizer();
const contextKey = `sync.${syncTargetId}.context`;
let context = Setting.value(contextKey);
try {
context = context ? JSON.parse(context) : {};
} catch (error) {
// Clearing the context is inefficient since it means all items are going to be re-downloaded
// however it won't result in duplicate items since the synchroniser is going to compare each
// item to the current state.
reg.logger().warn(`Could not parse JSON sync context ${contextKey}:`, context);
reg.logger().info('Clearing context and starting from scratch');
context = null;
}
try {
reg.logger().info('Starting scheduled sync');
const options = Object.assign({}, syncOptions, { context: context });
if (!options.saveContextHandler) {
options.saveContextHandler = newContext => {
Setting.setValue(contextKey, JSON.stringify(newContext));
};
}
const newContext = await sync.start(options);
Setting.setValue(contextKey, JSON.stringify(newContext));
} catch (error) {
if (error.code == 'alreadyStarted') {
reg.logger().info(error.message);
} else {
promiseResolve();
throw error;
}
}
} catch (error) {
reg.logger().info('Could not run background sync:');
reg.logger().info(error);
}
reg.setupRecurrentSync();
promiseResolve();
2017-07-24 22:36:49 +02:00
} finally {
reg.timerCallbackCalls_.pop();
}
};
if (delay === 0) {
timeoutCallback();
} else {
reg.scheduleSyncId_ = shim.setTimeout(timeoutCallback, delay);
}
return promise;
2017-07-16 23:17:22 +02:00
} finally {
reg.schedSyncCalls_.pop();
}
};
reg.setupRecurrentSync = () => {
reg.setupRecurrentCalls_.push(true);
try {
if (reg.recurrentSyncId_) {
shim.clearInterval(reg.recurrentSyncId_);
reg.recurrentSyncId_ = null;
2018-02-21 21:58:28 +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')}`);
if (Setting.value('env') === 'dev') {
reg.logger().info('Recurrent sync operation DISABLED!!!');
return;
}
reg.recurrentSyncId_ = shim.setInterval(() => {
reg.logger().info('Running background sync on timer...');
reg.scheduleSync(0);
}, 1000 * Setting.value('sync.interval'));
}
} finally {
reg.setupRecurrentCalls_.pop();
2017-08-20 16:29:18 +02:00
}
2019-07-29 15:43:53 +02:00
};
reg.setDb = v => {
2017-07-06 21:48:17 +02:00
reg.db_ = v;
2019-07-29 15:43:53 +02:00
};
2017-07-06 21:48:17 +02:00
reg.db = () => {
return reg.db_;
2019-07-29 15:43:53 +02:00
};
2017-07-06 21:48:17 +02:00
reg.cancelTimers_ = () => {
if (this.recurrentSyncId_) {
shim.clearInterval(reg.recurrentSyncId_);
this.recurrentSyncId_ = null;
}
if (reg.scheduleSyncId_) {
shim.clearTimeout(reg.scheduleSyncId_);
reg.scheduleSyncId_ = null;
}
};
reg.cancelTimers = async () => {
reg.logger().info('Cancelling sync timers');
reg.cancelTimers_();
return new Promise((resolve) => {
shim.setInterval(() => {
// ensure processing complete
if (!reg.setupRecurrentCalls_.length && !reg.schedSyncCalls_.length && !reg.timerCallbackCalls_.length && !reg.waitForReSyncCalls_.length) {
reg.cancelTimers_();
resolve();
}
}, 100);
});
};
reg.syncCalls_ = [];
reg.schedSyncCalls_ = [];
reg.waitForReSyncCalls_ = [];
reg.setupRecurrentCalls_ = [];
reg.timerCallbackCalls_ = [];
2019-07-29 15:43:53 +02:00
module.exports = { reg };