-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathsync.ts
336 lines (319 loc) · 13.2 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
import BaseWorkerHandler from '@akylas/nativescript-app-utils/worker/BaseWorkerHandler';
import { lc } from '@nativescript-community/l';
import { ApplicationSettings, EventData } from '@nativescript/core';
import { debounce } from '@nativescript/core/utils';
import { CustomError } from '@shared/utils/error';
import { showError } from '@shared/utils/showError';
import { writable } from 'svelte/store';
import { OCRDocument, OCRPage } from '~/models/OCRDocument';
import {
EVENT_DOCUMENT_ADDED,
EVENT_DOCUMENT_DELETED,
EVENT_DOCUMENT_MOVED_FOLDER,
EVENT_DOCUMENT_PAGES_ADDED,
EVENT_DOCUMENT_PAGE_UPDATED,
EVENT_DOCUMENT_UPDATED,
EVENT_FOLDER_ADDED,
EVENT_FOLDER_UPDATED,
EVENT_STATE,
EVENT_SYNC_STATE,
SETTINGS_SYNC_SERVICES
} from '~/utils/constants';
import type SyncWorker from '~/workers/SyncWorker';
import { DocumentAddedEventData, DocumentDeletedEventData, DocumentEvents, DocumentPagesAddedEventData, DocumentUpdatedEventData, FolderUpdatedEventData, documentsService } from './documents';
import { SYNC_TYPES, SyncType, getRemoteDeleteDocumentSettingsKey } from './sync/types';
import { WebdavDataSyncOptions } from './sync/WebdavDataSyncService';
export const syncServicesStore = writable([]);
const SETTINGS_KEY = 'webdav_config';
export function findArrayDiffs<S, T>(array1: S[], array2: T[], compare: (a: S, b: T) => boolean) {
const union: S[] = [];
array1 = Array.from(array1);
array2 = Array.from(array2);
for (let i = 0; i < array1.length; i++) {
const a = array1[i];
for (let j = 0; j < array2.length; j++) {
const b = array2[j];
if (compare(a, b)) {
union.push(a);
array1.splice(i, 1);
array2.splice(j, 1);
i--;
break;
}
}
}
return {
toBeAdded: array2,
toBeDeleted: array1,
union
};
}
export const SERVICES_SYNC_TITLES: { [key in SYNC_TYPES]: string } = {
webdav_image: lc('webdav_server'),
webdav_pdf: lc('webdav_server'),
webdav_data: lc('webdav_server'),
folder_image: lc('local_folder'),
folder_pdf: lc('local_folder')
};
export interface SyncStateEventData extends EventData {
state: 'finished' | 'running';
}
export interface SyncEnabledEventData extends EventData {
enabled: boolean;
}
const SYNC_DELAY = 1000;
// function debounceSync(fn, delay = 300, { ignoreFirstTrail = false, leading = false, trailing = true } = {}) {
// let timer;
// return (event) => {
// let noTrail = false;
// if (timer === undefined && leading) {
// noTrail = ignoreFirstTrail === true;
// fn.call(this, event);
// }
// clearTimeout(timer);
// timer = setTimeout(() => {
// if (trailing !== false && !noTrail) {
// syncService.syncDocuments({ force: true });
// }
// timer = undefined;
// }, delay);
// };
// }
export class SyncService extends BaseWorkerHandler<SyncWorker> {
constructor() {
super(() => new Worker('~/workers/SyncWorker'));
}
handleError(error: any) {
showError(error);
}
handleWorkerError(error: any) {
showError(CustomError.fromObject({ ...error, sentryReportTranslatedName: true }));
}
async onWorkerEvent(eventData) {
// DEV_LOG && console.log('onWorkerEvent', eventData);
// DEV_LOG && console.info('worker event', documentsService.id, eventData.eventName, eventData.target, !!eventData.object, Object.keys(eventData));
if (eventData.target === 'documentsService') {
if (eventData.doc) {
eventData.doc = OCRDocument.fromJSON(eventData.doc);
}
if (eventData.documents) {
eventData.documents = eventData.documents.map((d) => OCRDocument.fromJSON(d));
}
if (eventData.pages) {
eventData.pages = eventData.pages.map((d) => OCRPage.fromJSON(d));
}
if (eventData.object?.id === eventData.doc?.id) {
eventData.object = eventData.doc;
} else {
delete eventData.object;
}
// DEV_LOG && console.info('worker notifying event', documentsService.id, eventData.eventName, documentsService.notify);
documentsService.notify({ ...eventData, object: eventData.object || documentsService, fromWorker: true });
} else {
this.notify({ ...eventData });
}
}
services: any[] = [];
onDocumentAdded(event: DocumentAddedEventData) {
DEV_LOG && console.log('SYNC', 'onDocumentAdded', event.fromWorker);
let type = SyncType.PDF;
if (event.fromWorker !== true) {
type |= SyncType.DATA;
}
this.syncDocumentsInternal({ event, type, fromEvent: event.eventName });
}
onDocumentDeleted(event: DocumentDeletedEventData) {
DEV_LOG && console.log('SYNC', 'onDocumentDeleted');
this.getStoredSyncServices()
// .filter((s) => s instanceof BaseDataSyncService)
.forEach(async (service) => {
const key = getRemoteDeleteDocumentSettingsKey(service as any);
const documentsToDeleteOnRemote = JSON.parse(ApplicationSettings.getString(key, '[]'));
documentsToDeleteOnRemote.push(...event.documents.map((d) => d.id));
ApplicationSettings.setString(key, JSON.stringify(documentsToDeleteOnRemote));
});
this.sendDataEvent(event);
}
onDocumentUpdated(event: DocumentUpdatedEventData) {
// only used for data sync
DEV_LOG && console.log('SYNC', 'onDocumentUpdated', event.updateModifiedDate);
if (event.updateModifiedDate !== false) {
let type = SyncType.PDF;
if (event.fromWorker !== true) {
type |= SyncType.DATA;
}
this.syncDocumentsInternal({ event, type, fromEvent: event.eventName });
}
}
sendImageEvent(event: DocumentPagesAddedEventData) {
DEV_LOG && console.log('Sync', 'sendImageEvent');
// only used for image sync
this.syncDocumentsInternal({ event, type: SyncType.IMAGE, fromEvent: event.eventName });
}
sendDataEvent(event: FolderUpdatedEventData) {
if (event.fromWorker !== true) {
this.syncDocumentsInternal({ event, type: SyncType.DATA, fromEvent: event.eventName });
}
}
get enabled() {
return this.services?.length > 0;
}
getStoredSyncServices() {
return JSON.parse(ApplicationSettings.getString(SETTINGS_SYNC_SERVICES, '[]')) as (WebdavDataSyncOptions & { id?: number; type: SYNC_TYPES })[];
}
addService(type: SYNC_TYPES, data) {
const syncServices = this.getStoredSyncServices();
const dataToAdd = { type, id: Date.now(), ...data };
syncServices.push(dataToAdd);
ApplicationSettings.setString(SETTINGS_SYNC_SERVICES, JSON.stringify(syncServices));
DEV_LOG && console.log('Sync', 'addService', type, JSON.stringify(data));
if (!this.enabled) {
this.start();
} else {
this.onServiceChanged();
}
return dataToAdd;
}
removeService(data) {
const syncServices = this.getStoredSyncServices();
const index = syncServices.findIndex((s) => s.id === data.id);
DEV_LOG && console.log('Sync', 'removeService', index, JSON.stringify(data));
if (index !== -1) {
syncServices.splice(index, 1);
ApplicationSettings.setString(SETTINGS_SYNC_SERVICES, JSON.stringify(syncServices));
}
this.onServiceChanged();
return true;
}
updateService(data) {
const syncServices = this.getStoredSyncServices();
const index = syncServices.findIndex((s) => s.id === data.id);
DEV_LOG && console.log('Sync', 'updateService', index, JSON.stringify(data));
if (index !== -1) {
syncServices.splice(index, 1, data);
ApplicationSettings.setString(SETTINGS_SYNC_SERVICES, JSON.stringify(syncServices));
}
this.onServiceChanged(true);
return true;
// }
}
async start() {
if (this.enabled) {
return;
}
const syncServices = (this.services = this.getStoredSyncServices().filter((s) => s.enabled !== false));
DEV_LOG && console.log('Sync', 'start', syncServices);
// bring back old data config
const configStr = ApplicationSettings.getString(SETTINGS_KEY);
if (configStr) {
// TODO: remove this after a few releases
ApplicationSettings.remove(SETTINGS_KEY);
syncServices.push({ type: 'webdav_data', id: Date.now(), ...JSON.parse(configStr) });
ApplicationSettings.setString(SETTINGS_SYNC_SERVICES, JSON.stringify(syncServices));
}
syncServicesStore.set(syncServices);
DEV_LOG && console.log('Sync', 'start', /* this.services.length, */ this.enabled);
if (this.enabled) {
this.notify({ eventName: EVENT_STATE, enabled: this.enabled } as SyncEnabledEventData);
documentsService.on(EVENT_DOCUMENT_ADDED, this.onDocumentAdded, this);
documentsService.on(EVENT_DOCUMENT_UPDATED, this.onDocumentUpdated, this);
documentsService.on(EVENT_DOCUMENT_DELETED, this.onDocumentDeleted, this);
documentsService.on(EVENT_DOCUMENT_PAGE_UPDATED, this.sendImageEvent, this);
documentsService.on(EVENT_DOCUMENT_PAGES_ADDED, this.sendImageEvent, this);
documentsService.on(EVENT_DOCUMENT_MOVED_FOLDER, this.sendDataEvent, this);
documentsService.on(EVENT_FOLDER_UPDATED, this.sendDataEvent, this);
documentsService.on(EVENT_FOLDER_ADDED, this.sendDataEvent, this);
this.on(EVENT_SYNC_STATE, this.onSyncState, this);
DEV_LOG && console.log('SyncService', 'start');
}
}
async stop() {
DEV_LOG && console.log('Sync', 'stop');
this.off(EVENT_SYNC_STATE, this.onSyncState, this);
documentsService.off(EVENT_DOCUMENT_ADDED, this.onDocumentAdded, this);
documentsService.off(EVENT_DOCUMENT_UPDATED, this.onDocumentUpdated, this);
documentsService.off(EVENT_DOCUMENT_DELETED, this.onDocumentDeleted, this);
documentsService.off(EVENT_DOCUMENT_PAGE_UPDATED, this.sendImageEvent, this);
documentsService.off(EVENT_DOCUMENT_PAGES_ADDED, this.sendImageEvent, this);
documentsService.off(EVENT_DOCUMENT_MOVED_FOLDER, this.sendDataEvent, this);
documentsService.off(EVENT_FOLDER_UPDATED, this.sendDataEvent, this);
documentsService.off(EVENT_FOLDER_ADDED, this.sendDataEvent, this);
if (this.syncRunning) {
// if sync is running wait for it to be finished
await new Promise((resolve) => this.once(EVENT_SYNC_STATE, resolve));
}
this.worker?.stop();
this.worker = null;
}
onServiceChanged(force = false) {
const services = this.getStoredSyncServices().filter((s) => s.enabled !== false);
this.services = services;
syncServicesStore.set(services);
DEV_LOG && console.log('onServiceChanged', this.services.length);
}
syncRunning = false;
onSyncState(event: EventData & { state: 'running' | 'finished' }) {
this.syncRunning = event.state === 'running';
}
syncDocuments = debounce(
async ({
bothWays = false,
force = true,
type = SyncType.ALL,
withFolders = false
}: {
withFolders?;
force?;
bothWays?;
type?: number;
} = {}) => {
this.syncDocumentsInternal({ force, bothWays, type, withFolders });
},
SYNC_DELAY
);
async syncDocumentsInternal(
data: {
withFolders?;
force?;
bothWays?;
type?: number;
fromEvent?: string;
event?: DocumentEvents;
} = {}
) {
try {
const db = documentsService.db?.db?.db;
if (!this.enabled || !db) {
return;
}
// send syncState event right now for the UI to be updated as soon as possible
this.notify({ eventName: EVENT_SYNC_STATE, state: 'running' } as SyncStateEventData);
let eventData;
if (data.event) {
const { object, ...otherProps } = data.event;
// if (object?.['toJSONObject']) {
// eventData = { ...otherProps, object: event.object['toJSONObject']?.() };
// } else {
eventData = otherProps;
// }
DEV_LOG && console.warn('syncDocumentsInternal', data.type, data.fromEvent, Object.keys(otherProps));
}
await this.sendMessageToWorker(
'sync',
{
...data,
event: eventData
},
undefined,
undefined,
false,
0,
{ db: documentsService.db.db.db }
);
} catch (error) {
DEV_LOG && console.error(error);
showError(error);
}
}
}
export const syncService = new SyncService();