-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsync.ts
232 lines (191 loc) · 7.78 KB
/
sync.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import * as db from './db';
import { HLTime } from './HLTime';
import { debug, libName, log } from './utils';
const plugins: SyncPlugin[] = [];
export async function sync(options: { forceFullSync?: boolean } = {}) {
const { nodeId: localClientId } = db.getSettings();
// Attempt to do a sync using each registered plugin
for (const plugin of plugins) {
const pluginId = plugin.getPluginId();
try {
debug && log.debug(`Attempting to sync with remote storage using '${pluginId}' plugin.`);
await plugin.saveRemoteClientRecord(localClientId);
// Which of this client's own oplog entries needs to be uploaded to the server?
let mostRecentUploadedEntryTime = options.forceFullSync ? null : await plugin.getMostRecentUploadedEntryTime();
if (mostRecentUploadedEntryTime) {
log.debug(`Uploading OWN local entries created after ${mostRecentUploadedEntryTime}.`);
} else {
log.debug(`Uploading ALL local entries.`);
}
// Upload own oplog entries that are missing from the server.
let ownEntryUploadCounter = 0;
for await (const localEntry of db.getEntriesByClient(localClientId, { afterTime: mostRecentUploadedEntryTime })) {
//TODO: Add support for uploading more than one entry at a time (batching)
let hlTime = HLTime.parse(localEntry.hlcTime);
let result = await plugin.saveRemoteEntry({
time: new Date(hlTime.millis()),
counter: hlTime.counter(),
clientId: hlTime.node(),
entry: localEntry,
});
ownEntryUploadCounter += result.numUploaded;
}
debug && log.debug(`Uploaded ${ownEntryUploadCounter} local oplog entries.`);
debug && log.debug(`Attempting to discover remote clients on server and download their oplog entries...`);
for await (const clientRecord of plugin.getRemoteClientRecords({ excludeClientIds: [localClientId] })) {
const remoteClientId = clientRecord.clientId;
// What is the most recent oplog entry time we know of for the current remote client?
let mostRecentKnownOplogTimeForRemoteClient = null;
try {
let mostRecentEntry = await db.getMostRecentEntryForClient(remoteClientId);
if (mostRecentEntry) {
mostRecentKnownOplogTimeForRemoteClient = new Date(HLTime.parse(mostRecentEntry.hlcTime).millis());
}
} catch (error) {
log.error(`Error on attempt to determine most recent oplog entry time for client ${remoteClientId}`, error);
}
let remoteEntryDownloadCounter = 0;
for await (const remoteEntry of plugin.getRemoteEntries({
clientId: remoteClientId,
afterTime: mostRecentKnownOplogTimeForRemoteClient,
})) {
db.applyOplogEntry(remoteEntry); // Note that this will increment the local HLC time.
remoteEntryDownloadCounter++;
}
log.debug(`Downloaded ${remoteEntryDownloadCounter} oplog entries for remote client '${remoteClientId}'.`);
}
//TODO: Save any plugin settings that may have changed as part of the sync (e.g., the plugin updated its info
// about the last oplog entry that was uploaded).
const syncProfile = { ...getSyncProfileForPlugin(pluginId) } as SyncProfile;
syncProfile.settings = plugin.getSettings();
saveSyncProfile(syncProfile);
} catch (error) {
log.error(`Error while attempting to sync with ${pluginId}:`, error);
}
}
}
export async function registerSyncPlugin(plugin: SyncPlugin) {
if (!isSyncPlugin(plugin)) {
throw new Error(`${libName}: argument does not properly implement the SyncPlugin interface`);
}
try {
await plugin.load();
} catch (error) {
log.error(`Failed to load plugin '${plugin.getPluginId()}':`, error);
throw error;
}
plugin.addSignInChangeListener((userProfile: UserProfile | null) => {
onPluginSignInChange(plugin, userProfile);
});
plugins.push(plugin);
const syncProfileForPlugin = getSyncProfileForPlugin(plugin.getPluginId());
if (syncProfileForPlugin) {
debug && log.debug(`Passing saved settings to '${plugin.getPluginId()}' plugin:`, syncProfileForPlugin.settings);
plugin.setSettings(syncProfileForPlugin.settings);
if (!plugin.isSignedIn()) {
debug && log.debug(`Asking '${plugin.getPluginId()}' plugin to sign-in to remote service...`);
try {
await plugin.signIn();
} catch (error) {
log.error(`Plugin sign-in failed for '${plugin.getPluginId()}':`, error);
throw error;
}
}
}
}
function onPluginSignInChange(plugin: SyncPlugin, userProfile: UserProfile | null) {
const pluginId = plugin.getPluginId();
debug && log.debug(`Handling sign-in change from '${pluginId}' plugin; user profile:`, userProfile);
if (userProfile) {
saveSyncProfile({
pluginId: pluginId,
userProfile: userProfile,
settings: plugin.getSettings(),
});
} else {
removeSyncProfile(plugin.getPluginId());
}
}
export function getSyncProfileForPlugin(pluginId: string): SyncProfile | undefined {
return getSyncProfiles().find((existing) => existing.pluginId === pluginId);
}
export function getSyncProfiles(): SyncProfile[] {
const settings = db.getSettings();
return Array.isArray(settings?.syncProfiles) ? settings.syncProfiles : [];
}
export async function saveSyncProfile(profile: SyncProfile) {
debug && log.debug(`Saving sync profile for '${profile.pluginId}'`);
const settings = { ...db.getSettings() };
// Remove any existing instance of the sync profile in case it already exists and we're replacing it.
settings.syncProfiles = settings.syncProfiles.filter((existing) => existing.pluginId !== profile.pluginId);
settings.syncProfiles.push(profile);
await db.saveSettings(settings);
}
export async function removeSyncProfile(pluginId: string) {
const existingProfileIndex = getSyncProfiles().findIndex((existing) => existing.pluginId === pluginId);
if (existingProfileIndex === -1) {
debug && log.debug(`Ignoring request to remove sync profile for plugin '${pluginId}'; profile doesn't exist.`);
return;
}
debug && log.debug(`Removing sync profile for '${pluginId}'`);
const newSettings = { ...db.getSettings() };
newSettings.syncProfiles = [
...newSettings.syncProfiles.slice(0, existingProfileIndex),
...newSettings.syncProfiles.slice(existingProfileIndex + 1),
];
await db.saveSettings(newSettings);
}
/**
* Utility / type guard function for verifying that something implements the SyncPlugin interface.
*/
export function isSyncPlugin(thing: unknown): thing is SyncPlugin {
if (!thing) {
return false;
}
const candidate = thing as SyncPlugin;
if (!(candidate.getPluginId instanceof Function)) {
return false;
} else if (typeof candidate.getPluginId() !== 'string') {
return false;
}
if (!(candidate.isLoaded instanceof Function)) {
return false;
}
if (!(candidate.load instanceof Function)) {
return false;
}
if (!(candidate.isSignedIn instanceof Function)) {
return false;
}
if (!(candidate.signIn instanceof Function)) {
return false;
}
if (!(candidate.signOut instanceof Function)) {
return false;
}
if (!(candidate.addSignInChangeListener instanceof Function)) {
return false;
}
if (!(candidate.getSettings instanceof Function)) {
return false;
}
if (!(candidate.setSettings instanceof Function)) {
return false;
}
if (!(candidate.getRemoteEntries instanceof Function)) {
return false;
}
if (!(candidate.saveRemoteEntry instanceof Function)) {
return false;
}
if (!(candidate.getRemoteClientRecords instanceof Function)) {
return false;
}
if (!(candidate.saveRemoteClientRecord instanceof Function)) {
return false;
}
if (!(candidate.getMostRecentUploadedEntryTime instanceof Function)) {
return false;
}
return true;
}