-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
synchronize.ts
65 lines (57 loc) · 1.75 KB
/
synchronize.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { utils, CommandRuntime, CommandDeclaration, CommandContext } from '../services/CommandService';
import { _ } from '../locale';
import { reg } from '../registry';
import Setting from '../models/Setting';
export const declaration: CommandDeclaration = {
name: 'synchronize',
label: () => _('Synchronise'),
iconName: 'fa-sync-alt',
};
// Note that this command actually acts as a toggle - it starts or cancels
// synchronisation depending on the "syncStarted" parameter
export const runtime = (): CommandRuntime => {
return {
execute: async (context: CommandContext, syncStarted: boolean = null) => {
syncStarted = syncStarted === null ? context.state.syncStarted : syncStarted;
const action = syncStarted ? 'cancel' : 'start';
if (!Setting.value('sync.target')) {
context.dispatch({
type: 'DIALOG_OPEN',
name: 'syncWizard',
});
return 'init';
}
if (!(await reg.syncTarget().isAuthenticated())) {
if (reg.syncTarget().authRouteName()) {
utils.store.dispatch({
type: 'NAV_GO',
routeName: reg.syncTarget().authRouteName(),
});
return 'auth';
}
reg.logger().error('Not authenticated with sync target - please check your credentials.');
return 'error';
}
let sync = null;
try {
sync = await reg.syncTarget().synchronizer();
} catch (error) {
reg.logger().error('Could not initialise synchroniser: ');
reg.logger().error(error);
error.message = `Could not initialise synchroniser: ${error.message}`;
utils.store.dispatch({
type: 'SYNC_REPORT_UPDATE',
report: { errors: [error] },
});
return 'error';
}
if (action === 'cancel') {
sync.cancel();
return 'cancel';
} else {
void reg.scheduleSync(0);
return 'sync';
}
},
};
};