2020-10-18 22:52:10 +02:00
|
|
|
import { utils, CommandRuntime, CommandDeclaration, CommandContext } from '../services/CommandService';
|
2020-11-05 18:58:23 +02:00
|
|
|
import { _ } from '../locale';
|
2021-01-29 20:45:11 +02:00
|
|
|
import { reg } from '../registry';
|
2020-07-03 23:32:39 +02:00
|
|
|
|
2020-11-12 21:13:28 +02:00
|
|
|
export const declaration: CommandDeclaration = {
|
2020-07-03 23:32:39 +02:00
|
|
|
name: 'synchronize',
|
2020-09-15 13:08:25 +02:00
|
|
|
label: () => _('Synchronise'),
|
2020-07-03 23:32:39 +02:00
|
|
|
iconName: 'fa-sync-alt',
|
|
|
|
};
|
|
|
|
|
2020-10-18 22:52:10 +02:00
|
|
|
// Note that this command actually acts as a toggle - it starts or cancels
|
|
|
|
// synchronisation depending on the "syncStarted" parameter
|
2020-11-12 21:13:28 +02:00
|
|
|
export const runtime = (): CommandRuntime => {
|
2020-07-03 23:32:39 +02:00
|
|
|
return {
|
2020-11-12 21:13:28 +02:00
|
|
|
execute: async (context: CommandContext, syncStarted: boolean = null) => {
|
2020-10-21 18:22:29 +02:00
|
|
|
syncStarted = syncStarted === null ? context.state.syncStarted : syncStarted;
|
|
|
|
|
2020-07-03 23:32:39 +02:00
|
|
|
const action = syncStarted ? 'cancel' : 'start';
|
|
|
|
|
|
|
|
if (!(await reg.syncTarget().isAuthenticated())) {
|
|
|
|
if (reg.syncTarget().authRouteName()) {
|
|
|
|
utils.store.dispatch({
|
|
|
|
type: 'NAV_GO',
|
|
|
|
routeName: reg.syncTarget().authRouteName(),
|
|
|
|
});
|
|
|
|
return 'auth';
|
|
|
|
}
|
|
|
|
|
|
|
|
reg.logger().info('Not authentified with sync target - please check your credential.');
|
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
let sync = null;
|
|
|
|
try {
|
|
|
|
sync = await reg.syncTarget().synchronizer();
|
|
|
|
} catch (error) {
|
|
|
|
reg.logger().info('Could not acquire synchroniser:');
|
|
|
|
reg.logger().info(error);
|
|
|
|
return 'error';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action == 'cancel') {
|
|
|
|
sync.cancel();
|
|
|
|
return 'cancel';
|
|
|
|
} else {
|
2021-01-29 20:45:11 +02:00
|
|
|
void reg.scheduleSync(0);
|
2020-07-03 23:32:39 +02:00
|
|
|
return 'sync';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|