document-drive 1.0.0-alpha.45 → 1.0.0-alpha.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/server/index.ts +55 -52
- package/src/utils/document-helpers.ts +8 -5
package/package.json
CHANGED
package/src/server/index.ts
CHANGED
|
@@ -22,6 +22,8 @@ import {
|
|
|
22
22
|
OperationScope
|
|
23
23
|
} from 'document-model/document';
|
|
24
24
|
import { createNanoEvents, Unsubscribe } from 'nanoevents';
|
|
25
|
+
import { ICache } from '../cache';
|
|
26
|
+
import InMemoryCache from '../cache/memory';
|
|
25
27
|
import { MemoryStorage } from '../storage/memory';
|
|
26
28
|
import type {
|
|
27
29
|
DocumentDriveStorage,
|
|
@@ -66,8 +68,6 @@ import {
|
|
|
66
68
|
type SynchronizationUnit
|
|
67
69
|
} from './types';
|
|
68
70
|
import { filterOperationsByRevision } from './utils';
|
|
69
|
-
import { ICache } from '../cache';
|
|
70
|
-
import InMemoryCache from '../cache/memory';
|
|
71
71
|
|
|
72
72
|
export * from './listener';
|
|
73
73
|
export type * from './types';
|
|
@@ -127,16 +127,16 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
127
127
|
|
|
128
128
|
const result = await (!strand.documentId
|
|
129
129
|
? this.addDriveOperations(
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
130
|
+
strand.driveId,
|
|
131
|
+
operations as Operation<DocumentDriveAction | BaseAction>[],
|
|
132
|
+
false
|
|
133
|
+
)
|
|
134
134
|
: this.addOperations(
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
135
|
+
strand.driveId,
|
|
136
|
+
strand.documentId,
|
|
137
|
+
operations,
|
|
138
|
+
false
|
|
139
|
+
));
|
|
140
140
|
|
|
141
141
|
if (result.status === 'ERROR') {
|
|
142
142
|
this.updateSyncStatus(strand.driveId, result.status, result.error);
|
|
@@ -223,26 +223,23 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
223
223
|
const drives = await this.getDrives();
|
|
224
224
|
for (const drive of drives) {
|
|
225
225
|
await this._initializeDrive(drive).catch(error => {
|
|
226
|
-
logger.error(
|
|
227
|
-
`Error initializing drive ${drive}`,
|
|
228
|
-
error
|
|
229
|
-
);
|
|
226
|
+
logger.error(`Error initializing drive ${drive}`, error);
|
|
230
227
|
errors.push(error as Error);
|
|
231
228
|
});
|
|
232
229
|
}
|
|
233
230
|
|
|
234
231
|
// if network connect comes online then
|
|
235
232
|
// triggers the listeners update
|
|
236
|
-
if (typeof window !==
|
|
233
|
+
if (typeof window !== 'undefined') {
|
|
237
234
|
window.addEventListener('online', () => {
|
|
238
|
-
this.listenerStateManager
|
|
239
|
-
this.handleListenerError.bind(this))
|
|
235
|
+
this.listenerStateManager
|
|
236
|
+
.triggerUpdate(false, this.handleListenerError.bind(this))
|
|
237
|
+
.catch(error => {
|
|
240
238
|
logger.error(
|
|
241
239
|
'Non handled error updating listeners',
|
|
242
240
|
error
|
|
243
241
|
);
|
|
244
242
|
});
|
|
245
|
-
|
|
246
243
|
});
|
|
247
244
|
}
|
|
248
245
|
|
|
@@ -307,14 +304,14 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
307
304
|
const nodeUnits =
|
|
308
305
|
scope?.length || branch?.length
|
|
309
306
|
? node.synchronizationUnits.filter(
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
307
|
+
unit =>
|
|
308
|
+
(!scope?.length ||
|
|
309
|
+
scope.includes(unit.scope) ||
|
|
310
|
+
scope.includes('*')) &&
|
|
311
|
+
(!branch?.length ||
|
|
312
|
+
branch.includes(unit.branch) ||
|
|
313
|
+
branch.includes('*'))
|
|
314
|
+
)
|
|
318
315
|
: node.synchronizationUnits;
|
|
319
316
|
if (!nodeUnits.length) {
|
|
320
317
|
continue;
|
|
@@ -325,7 +322,8 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
325
322
|
: this.getDrive(driveId));
|
|
326
323
|
|
|
327
324
|
for (const { syncId, scope, branch } of nodeUnits) {
|
|
328
|
-
const operations =
|
|
325
|
+
const operations =
|
|
326
|
+
document.operations[scope as OperationScope] ?? [];
|
|
329
327
|
const lastOperation = operations[operations.length - 1];
|
|
330
328
|
synchronizationUnits.push({
|
|
331
329
|
syncId,
|
|
@@ -492,7 +490,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
492
490
|
|
|
493
491
|
async getDrive(drive: string, options?: GetDocumentOptions) {
|
|
494
492
|
try {
|
|
495
|
-
const document = await this.cache.getDocument(
|
|
493
|
+
const document = await this.cache.getDocument('drives', drive);
|
|
496
494
|
if (document && isDocumentDrive(document)) {
|
|
497
495
|
return document;
|
|
498
496
|
}
|
|
@@ -518,7 +516,9 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
518
516
|
`Document with id ${drive} is not a Document Drive`
|
|
519
517
|
);
|
|
520
518
|
} else {
|
|
521
|
-
this.cache
|
|
519
|
+
this.cache
|
|
520
|
+
.setDocument('drives', drive, document)
|
|
521
|
+
.catch(logger.error);
|
|
522
522
|
return document;
|
|
523
523
|
}
|
|
524
524
|
}
|
|
@@ -600,6 +600,11 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
600
600
|
storageDocumentOperations
|
|
601
601
|
);
|
|
602
602
|
|
|
603
|
+
// No operations to apply
|
|
604
|
+
if (branch.length < 1) {
|
|
605
|
+
continue;
|
|
606
|
+
}
|
|
607
|
+
|
|
603
608
|
const trunk = garbageCollect(
|
|
604
609
|
sortOperations(storageDocumentOperations)
|
|
605
610
|
);
|
|
@@ -663,11 +668,11 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
663
668
|
e instanceof OperationError
|
|
664
669
|
? e
|
|
665
670
|
: new OperationError(
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
+
'ERROR',
|
|
672
|
+
nextOperation,
|
|
673
|
+
(e as Error).message,
|
|
674
|
+
(e as Error).cause
|
|
675
|
+
);
|
|
671
676
|
|
|
672
677
|
// TODO: don't break on errors...
|
|
673
678
|
break;
|
|
@@ -707,9 +712,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
707
712
|
operation: Operation,
|
|
708
713
|
skipHashValidation = false
|
|
709
714
|
) {
|
|
710
|
-
const documentModel = this._getDocumentModel(
|
|
711
|
-
document.documentType
|
|
712
|
-
);
|
|
715
|
+
const documentModel = this._getDocumentModel(document.documentType);
|
|
713
716
|
|
|
714
717
|
const signalResults: SignalResult[] = [];
|
|
715
718
|
let newDocument = document;
|
|
@@ -927,11 +930,11 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
927
930
|
error instanceof OperationError
|
|
928
931
|
? error
|
|
929
932
|
: new OperationError(
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
933
|
+
'ERROR',
|
|
934
|
+
undefined,
|
|
935
|
+
(error as Error).message,
|
|
936
|
+
(error as Error).cause
|
|
937
|
+
);
|
|
935
938
|
|
|
936
939
|
return {
|
|
937
940
|
status: operationError.status,
|
|
@@ -1020,7 +1023,9 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
1020
1023
|
throw error ?? new Error('Invalid Document Drive document');
|
|
1021
1024
|
}
|
|
1022
1025
|
|
|
1023
|
-
this.cache
|
|
1026
|
+
this.cache
|
|
1027
|
+
.setDocument('drives', drive, document)
|
|
1028
|
+
.catch(logger.error);
|
|
1024
1029
|
|
|
1025
1030
|
for (const operation of operationsApplied) {
|
|
1026
1031
|
switch (operation.type) {
|
|
@@ -1097,11 +1102,11 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
1097
1102
|
error instanceof OperationError
|
|
1098
1103
|
? error
|
|
1099
1104
|
: new OperationError(
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
+
'ERROR',
|
|
1106
|
+
undefined,
|
|
1107
|
+
(error as Error).message,
|
|
1108
|
+
(error as Error).cause
|
|
1109
|
+
);
|
|
1105
1110
|
|
|
1106
1111
|
return {
|
|
1107
1112
|
status: operationError.status,
|
|
@@ -1118,9 +1123,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
|
|
|
1118
1123
|
actions: (T | BaseAction)[]
|
|
1119
1124
|
): Operation<T | BaseAction>[] {
|
|
1120
1125
|
const operations: Operation<T | BaseAction>[] = [];
|
|
1121
|
-
const { reducer } = this._getDocumentModel(
|
|
1122
|
-
document.documentType
|
|
1123
|
-
);
|
|
1126
|
+
const { reducer } = this._getDocumentModel(document.documentType);
|
|
1124
1127
|
for (const action of actions) {
|
|
1125
1128
|
document = reducer(document, action);
|
|
1126
1129
|
const operation = document.operations[action.scope].slice().pop();
|
|
@@ -483,11 +483,14 @@ export function removeExistingOperations(
|
|
|
483
483
|
return newOperations.filter(newOperation => {
|
|
484
484
|
return !operationsHistory.some(historyOperation => {
|
|
485
485
|
return (
|
|
486
|
-
newOperation.
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
newOperation.
|
|
490
|
-
|
|
486
|
+
(newOperation.type === 'NOOP' &&
|
|
487
|
+
newOperation.skip === 0 &&
|
|
488
|
+
newOperation.index === historyOperation.index) ||
|
|
489
|
+
(newOperation.index === historyOperation.index &&
|
|
490
|
+
newOperation.skip === historyOperation.skip &&
|
|
491
|
+
newOperation.scope === historyOperation.scope &&
|
|
492
|
+
newOperation.hash === historyOperation.hash &&
|
|
493
|
+
newOperation.type === historyOperation.type)
|
|
491
494
|
);
|
|
492
495
|
});
|
|
493
496
|
});
|