document-drive 1.0.0-alpha.63 → 1.0.0-alpha.65

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "document-drive",
3
- "version": "1.0.0-alpha.63",
3
+ "version": "1.0.0-alpha.65",
4
4
  "license": "AGPL-3.0-only",
5
5
  "type": "module",
6
6
  "module": "./src/index.ts",
@@ -34,7 +34,7 @@
34
34
  "test:watch": "vitest watch"
35
35
  },
36
36
  "peerDependencies": {
37
- "document-model": "1.2.0",
37
+ "document-model": "1.3.0",
38
38
  "document-model-libs": "^1.53.1"
39
39
  },
40
40
  "optionalDependencies": {
@@ -65,7 +65,7 @@
65
65
  "@typescript-eslint/eslint-plugin": "^6.21.0",
66
66
  "@typescript-eslint/parser": "^6.21.0",
67
67
  "@vitest/coverage-v8": "^1.4.0",
68
- "document-model": "^1.2.0",
68
+ "document-model": "^1.3.0",
69
69
  "document-model-libs": "^1.53.1",
70
70
  "eslint": "^8.57.0",
71
71
  "eslint-config-prettier": "^9.1.0",
@@ -20,7 +20,7 @@ import {
20
20
  DocumentModel,
21
21
  Operation,
22
22
  OperationScope,
23
- State
23
+ utils as DocumentUtils
24
24
  } from 'document-model/document';
25
25
  import { createNanoEvents, Unsubscribe } from 'nanoevents';
26
26
  import { ICache } from '../cache';
@@ -631,6 +631,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
631
631
 
632
632
  async _processOperations<T extends Document, A extends Action>(
633
633
  drive: string,
634
+ documentId: string | undefined,
634
635
  storageDocument: DocumentStorage<T>,
635
636
  operations: Operation<A | BaseAction>[]
636
637
  ) {
@@ -690,6 +691,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
690
691
  try {
691
692
  const appliedResult = await this._performOperation(
692
693
  drive,
694
+ documentId,
693
695
  document,
694
696
  nextOperation,
695
697
  skipHashValidation
@@ -739,10 +741,6 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
739
741
  ) : documentStorage.operations;
740
742
  const operations = baseUtils.documentHelpers.garbageCollectDocumentOperations(revisionOperations);
741
743
 
742
- if (documentStorage.state && (!options || options.checkHashes === false)) {
743
- return documentStorage as T;
744
- }
745
-
746
744
  return baseUtils.replayDocument(
747
745
  documentStorage.initialState,
748
746
  operations,
@@ -760,6 +758,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
760
758
 
761
759
  private async _performOperation<T extends Document>(
762
760
  drive: string,
761
+ id: string | undefined,
763
762
  document: T,
764
763
  operation: Operation,
765
764
  skipHashValidation = false
@@ -769,6 +768,25 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
769
768
  const signalResults: SignalResult[] = [];
770
769
  let newDocument = document;
771
770
 
771
+ const scope = operation.scope;
772
+ const documentOperations = DocumentUtils.documentHelpers.garbageCollectDocumentOperations(
773
+ {
774
+ ...document.operations,
775
+ [scope]: DocumentUtils.documentHelpers.skipHeaderOperations(
776
+ document.operations[scope],
777
+ operation,
778
+ ),
779
+ },
780
+ );
781
+
782
+ const lastRemainingOperation = documentOperations[scope].at(-1);
783
+ // if the latest operation doesn't have a resulting state then tries
784
+ // to retrieve it from the db to avoid rerunning all the operations
785
+ if (lastRemainingOperation && !lastRemainingOperation.resultingState) {
786
+ lastRemainingOperation.resultingState = await (id ? this.storage.getOperationResultingState?.(drive, id, lastRemainingOperation.index, lastRemainingOperation.scope, "main") :
787
+ this.storage.getDriveOperationResultingState?.(drive, lastRemainingOperation.index, lastRemainingOperation.scope, "main"))
788
+ }
789
+
772
790
  const operationSignals: (() => Promise<SignalResult>)[] = [];
773
791
  newDocument = documentModel.reducer(
774
792
  newDocument,
@@ -924,6 +942,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
924
942
  await this._addOperations(drive, id, async documentStorage => {
925
943
  const result = await this._processOperations(
926
944
  drive,
945
+ id,
927
946
  documentStorage,
928
947
  operations
929
948
  );
@@ -1109,7 +1128,7 @@ export class DocumentDriveServer extends BaseDocumentDriveServer {
1109
1128
  const result = await this._processOperations<
1110
1129
  DocumentDriveDocument,
1111
1130
  DocumentDriveAction
1112
- >(drive, documentStorage, operations.slice());
1131
+ >(drive, undefined, documentStorage, operations.slice());
1113
1132
 
1114
1133
  document = result.document;
1115
1134
  operationsApplied.push(...result.operationsApplied);
@@ -505,4 +505,23 @@ export class PrismaStorage implements IDriveStorage {
505
505
  });
506
506
  await this.deleteDocument('drives', id);
507
507
  }
508
+
509
+ async getOperationResultingState(driveId: string, documentId: string, index: number, scope: string, branch: string): Promise<unknown> {
510
+ const operation = await this.db.operation.findUnique({
511
+ where: {
512
+ unique_operation: {
513
+ driveId,
514
+ documentId,
515
+ index,
516
+ scope,
517
+ branch
518
+ }
519
+ }
520
+ });
521
+ return operation?.resultingState?.toString();
522
+ }
523
+
524
+ getDriveOperationResultingState(drive: string, index: number, scope: string, branch: string): Promise<unknown> {
525
+ return this.getOperationResultingState("drives", drive, index, scope, branch);
526
+ }
508
527
  }
@@ -42,6 +42,7 @@ export interface IStorage {
42
42
  }>
43
43
  ): Promise<void>;
44
44
  deleteDocument(drive: string, id: string): Promise<void>;
45
+ getOperationResultingState?(drive: string, id: string, index: number, scope: string, branch: string): Promise<unknown>;
45
46
  }
46
47
 
47
48
  export interface IDriveStorage extends IStorage {
@@ -63,4 +64,5 @@ export interface IDriveStorage extends IStorage {
63
64
  header: DocumentHeader;
64
65
  }>
65
66
  ): Promise<void>;
67
+ getDriveOperationResultingState?(drive: string, index: number, scope: string, branch: string): Promise<unknown>;
66
68
  }