2023-07-27 17:05:56 +02:00
|
|
|
import Logger from '@joplin/utils/Logger';
|
2021-01-29 20:45:11 +02:00
|
|
|
import Setting from './models/Setting';
|
|
|
|
import shim from './shim';
|
2021-08-16 16:20:14 +02:00
|
|
|
import SyncTargetRegistry from './SyncTargetRegistry';
|
2021-01-29 20:45:11 +02:00
|
|
|
|
|
|
|
class Registry {
|
|
|
|
|
|
|
|
private syncTargets_: any = {};
|
|
|
|
private logger_: Logger = null;
|
|
|
|
private schedSyncCalls_: boolean[] = [];
|
2022-08-31 13:57:28 +02:00
|
|
|
private waitForReSyncCalls_: boolean[] = [];
|
2021-01-29 20:45:11 +02:00
|
|
|
private setupRecurrentCalls_: boolean[] = [];
|
|
|
|
private timerCallbackCalls_: boolean[] = [];
|
|
|
|
private showErrorMessageBoxHandler_: any;
|
|
|
|
private scheduleSyncId_: any;
|
|
|
|
private recurrentSyncId_: any;
|
|
|
|
private db_: any;
|
2021-03-29 10:35:39 +02:00
|
|
|
private isOnMobileData_ = false;
|
2021-01-29 20:45:11 +02:00
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public logger() {
|
2021-01-29 20:45:11 +02:00
|
|
|
if (!this.logger_) {
|
|
|
|
// console.warn('Calling logger before it is initialized');
|
|
|
|
return new Logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.logger_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public setLogger(l: Logger) {
|
2021-01-29 20:45:11 +02:00
|
|
|
this.logger_ = l;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public setShowErrorMessageBoxHandler(v: any) {
|
2021-01-29 20:45:11 +02:00
|
|
|
this.showErrorMessageBoxHandler_ = v;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public showErrorMessageBox(message: string) {
|
2021-01-29 20:45:11 +02:00
|
|
|
if (!this.showErrorMessageBoxHandler_) return;
|
|
|
|
this.showErrorMessageBoxHandler_(message);
|
|
|
|
}
|
|
|
|
|
2021-03-29 10:35:39 +02:00
|
|
|
// If isOnMobileData is true, the doWifiConnectionCheck is not set
|
|
|
|
// and the sync.mobileWifiOnly setting is true it will cancel the sync.
|
2023-03-06 16:22:01 +02:00
|
|
|
public setIsOnMobileData(isOnMobileData: boolean) {
|
2021-03-29 10:35:39 +02:00
|
|
|
this.isOnMobileData_ = isOnMobileData;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public resetSyncTarget(syncTargetId: number = null) {
|
2021-01-29 20:45:11 +02:00
|
|
|
if (syncTargetId === null) syncTargetId = Setting.value('sync.target');
|
|
|
|
delete this.syncTargets_[syncTargetId];
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public syncTargetNextcloud() {
|
2021-01-29 20:45:11 +02:00
|
|
|
return this.syncTarget(SyncTargetRegistry.nameToId('nextcloud'));
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public syncTarget = (syncTargetId: number = null) => {
|
2021-01-29 20:45:11 +02:00
|
|
|
if (syncTargetId === null) syncTargetId = Setting.value('sync.target');
|
|
|
|
if (this.syncTargets_[syncTargetId]) return this.syncTargets_[syncTargetId];
|
|
|
|
|
|
|
|
const SyncTargetClass = SyncTargetRegistry.classById(syncTargetId);
|
|
|
|
if (!this.db()) throw new Error('Cannot initialize sync without a db');
|
|
|
|
|
|
|
|
const target = new SyncTargetClass(this.db());
|
|
|
|
target.setLogger(this.logger());
|
|
|
|
this.syncTargets_[syncTargetId] = target;
|
|
|
|
return target;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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.
|
2023-03-06 16:22:01 +02:00
|
|
|
public waitForSyncFinishedThenSync = async () => {
|
2021-08-16 17:18:32 +02:00
|
|
|
if (!Setting.value('sync.target')) {
|
|
|
|
this.logger().info('waitForSyncFinishedThenSync - cancelling because no sync target is selected.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
this.waitForReSyncCalls_.push(true);
|
|
|
|
try {
|
|
|
|
const synchronizer = await this.syncTarget().synchronizer();
|
|
|
|
await synchronizer.waitForSyncToFinish();
|
|
|
|
await this.scheduleSync(0);
|
|
|
|
} finally {
|
|
|
|
this.waitForReSyncCalls_.pop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-06-30 10:11:26 +02:00
|
|
|
public scheduleSync = async (delay: number = null, syncOptions: any = null, doWifiConnectionCheck = false) => {
|
2021-01-29 20:45:11 +02:00
|
|
|
this.schedSyncCalls_.push(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (delay === null) delay = 1000 * 10;
|
|
|
|
if (syncOptions === null) syncOptions = {};
|
|
|
|
|
2023-06-30 11:30:29 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-types -- Old code before rule was applied
|
2021-01-29 20:45:11 +02:00
|
|
|
let promiseResolve: Function = null;
|
|
|
|
const promise = new Promise((resolve) => {
|
|
|
|
promiseResolve = resolve;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (this.scheduleSyncId_) {
|
|
|
|
shim.clearTimeout(this.scheduleSyncId_);
|
|
|
|
this.scheduleSyncId_ = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Setting.value('env') === 'dev' && delay !== 0) {
|
2021-05-13 18:57:37 +02:00
|
|
|
// this.logger().info('Schedule sync DISABLED!!!');
|
|
|
|
// return;
|
2021-01-29 20:45:11 +02:00
|
|
|
}
|
|
|
|
|
2021-02-07 17:29:36 +02:00
|
|
|
this.logger().debug('Scheduling sync operation...', delay);
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
const timeoutCallback = async () => {
|
|
|
|
this.timerCallbackCalls_.push(true);
|
|
|
|
try {
|
|
|
|
this.scheduleSyncId_ = null;
|
|
|
|
this.logger().info('Preparing scheduled sync');
|
|
|
|
|
2021-03-29 10:35:39 +02:00
|
|
|
if (doWifiConnectionCheck && Setting.value('sync.mobileWifiOnly') && this.isOnMobileData_) {
|
|
|
|
this.logger().info('Sync cancelled because we\'re on mobile data');
|
|
|
|
promiseResolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
const syncTargetId = Setting.value('sync.target');
|
|
|
|
|
2021-08-16 17:18:32 +02:00
|
|
|
if (!syncTargetId) {
|
|
|
|
this.logger().info('Sync cancelled - no sync target is selected.');
|
|
|
|
promiseResolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-29 20:45:11 +02:00
|
|
|
if (!(await this.syncTarget(syncTargetId).isAuthenticated())) {
|
|
|
|
this.logger().info('Synchroniser is missing credentials - manual sync required to authenticate.');
|
|
|
|
promiseResolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const sync = await this.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.
|
|
|
|
this.logger().warn(`Could not parse JSON sync context ${contextKey}:`, context);
|
|
|
|
this.logger().info('Clearing context and starting from scratch');
|
|
|
|
context = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
this.logger().info('Starting scheduled sync');
|
2023-06-01 13:02:36 +02:00
|
|
|
const options = { ...syncOptions, context: context };
|
2021-01-29 20:45:11 +02:00
|
|
|
if (!options.saveContextHandler) {
|
|
|
|
options.saveContextHandler = (newContext: any) => {
|
|
|
|
Setting.setValue(contextKey, JSON.stringify(newContext));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
const newContext = await sync.start(options);
|
|
|
|
Setting.setValue(contextKey, JSON.stringify(newContext));
|
|
|
|
} catch (error) {
|
2022-07-23 11:33:12 +02:00
|
|
|
if (error.code === 'alreadyStarted') {
|
2021-01-29 20:45:11 +02:00
|
|
|
this.logger().info(error.message);
|
|
|
|
} else {
|
|
|
|
promiseResolve();
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.logger().info('Could not run background sync:');
|
|
|
|
this.logger().info(error);
|
|
|
|
}
|
|
|
|
this.setupRecurrentSync();
|
|
|
|
promiseResolve();
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
this.timerCallbackCalls_.pop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (delay === 0) {
|
|
|
|
void timeoutCallback();
|
|
|
|
} else {
|
|
|
|
this.scheduleSyncId_ = shim.setTimeout(timeoutCallback, delay);
|
|
|
|
}
|
|
|
|
return promise;
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
this.schedSyncCalls_.pop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public setupRecurrentSync() {
|
2021-01-29 20:45:11 +02:00
|
|
|
this.setupRecurrentCalls_.push(true);
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (this.recurrentSyncId_) {
|
|
|
|
shim.clearInterval(this.recurrentSyncId_);
|
|
|
|
this.recurrentSyncId_ = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Setting.value('sync.interval')) {
|
|
|
|
this.logger().debug('Recurrent sync is disabled');
|
|
|
|
} else {
|
|
|
|
this.logger().debug(`Setting up recurrent sync with interval ${Setting.value('sync.interval')}`);
|
|
|
|
|
|
|
|
if (Setting.value('env') === 'dev') {
|
|
|
|
this.logger().info('Recurrent sync operation DISABLED!!!');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.recurrentSyncId_ = shim.setInterval(() => {
|
|
|
|
this.logger().info('Running background sync on timer...');
|
2021-03-29 10:35:39 +02:00
|
|
|
void this.scheduleSync(0, null, true);
|
2021-01-29 20:45:11 +02:00
|
|
|
}, 1000 * Setting.value('sync.interval'));
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
this.setupRecurrentCalls_.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public setDb = (v: any) => {
|
2021-01-29 20:45:11 +02:00
|
|
|
this.db_ = v;
|
|
|
|
};
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public db() {
|
2021-01-29 20:45:11 +02:00
|
|
|
return this.db_;
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
private cancelTimers_() {
|
2021-01-29 20:45:11 +02:00
|
|
|
if (this.recurrentSyncId_) {
|
|
|
|
shim.clearInterval(this.recurrentSyncId_);
|
|
|
|
this.recurrentSyncId_ = null;
|
|
|
|
}
|
|
|
|
if (this.scheduleSyncId_) {
|
|
|
|
shim.clearTimeout(this.scheduleSyncId_);
|
|
|
|
this.scheduleSyncId_ = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 16:22:01 +02:00
|
|
|
public cancelTimers = async () => {
|
2021-01-29 20:45:11 +02:00
|
|
|
this.logger().info('Cancelling sync timers');
|
|
|
|
this.cancelTimers_();
|
|
|
|
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
shim.setInterval(() => {
|
|
|
|
// ensure processing complete
|
|
|
|
if (!this.setupRecurrentCalls_.length && !this.schedSyncCalls_.length && !this.timerCallbackCalls_.length && !this.waitForReSyncCalls_.length) {
|
|
|
|
this.cancelTimers_();
|
|
|
|
resolve(null);
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
const reg = new Registry();
|
|
|
|
|
|
|
|
// eslint-disable-next-line import/prefer-default-export
|
|
|
|
export { reg };
|