Joeri Exelmans před 3 roky
rodič
revize
b1834d40f0
3 změnil soubory, kde provedl 71 přidání a 52 odebrání
  1. 0 52
      src/onion/delta.ts
  2. 44 0
      src/onion/version.test.ts
  3. 27 0
      src/onion/version.ts

+ 0 - 52
src/onion/delta.ts

@@ -4,55 +4,3 @@ export interface Delta {
   // Get deltas that are conflicting with this delta.
   getConflicts(): Array<Delta>;
 }
-
-// export class CompositeDelta implements Delta {
-//   readonly deltas: Array<Delta>;
-//   readonly dependencies: Array<CompositeDelta>;
-
-//   constructor(deltas: Array<Delta>, dependencies: Array<CompositeDelta>) {
-//     this.deltas = deltas;
-//     this.dependencies = dependencies;
-//   }
-// }
-
-// export class CompositeDeltaGraph {
-//   conflictRegistry: ConflictRegistry;
-//   containedBy: Map<Delta, CompositeDelta>;
-
-//   constructor(lowerLevelConflictRegistry: ConflictRegistry) {
-//     this.conflictRegistry = new ConflictRegistry();
-//     this.containedBy = new Map();
-//     this.lowerLevelConflictRegistry = lowerLevelConflictRegistry;
-//   }
-
-//   newCompositeDelta(deltas: Array<Delta>) {
-//     // Find dependencies
-//     const dependencies = [];
-//     for (const delta of deltas) {
-//       for (const dependency of delta.getDependencies()) {
-//         const dependencyContainment = this.containedBy(dependency);
-//         if (dependencyContainment !== undefined) {
-//           // add dependency if we don't have it yet
-//           if (!dependencies.includes(dependencyContainment)) {
-//             dependencies.push(dependencyContainment);
-//           }
-//         }
-//       }
-//     }
-//     const composite = new CompositeDelta(deltas, dependencies);
-//     for (const delta of deltas) {
-//       this.containedBy.set(delta, composite);
-//     }
-//     // Find conflicts
-//     for (const delta of deltas) {
-//       for (const conflictsWith of this.lowerLevelConflictRegistry.lookup(delta)) {
-//         const conflictContainment = this.containedBy(conflictsWith);
-//         this.conflictRegistry.registerConflict({first: () => composite, second: () => conflictContainment});
-//       }
-//     }
-
-
-  
-//     return composite;
-//   }
-// }

+ 44 - 0
src/onion/version.test.ts

@@ -0,0 +1,44 @@
+import * as _ from "lodash";
+
+import {
+  Version,
+} from "./version";
+
+import {
+  NodeCreation,
+  NodeDeletion,
+} from "./micro_op";
+
+import {
+  getUuidCallback,
+  assert,
+} from "./test_helpers";
+
+describe("Version", () => {
+
+  it("Get deltas", () => {
+    const getId = getUuidCallback();
+
+    const nodeCreation = new NodeCreation(getId());
+    const nodeDeletion = new NodeDeletion(nodeCreation, []);
+
+    const version0 = new Version([]);
+    const version1 = new Version([[version0, nodeCreation]]);
+    const version2 = new Version([[version1, nodeDeletion]]);
+
+    assert(_.isEqual(version0.getDeltas(), []), "expected version0 to be empty");
+    assert(_.isEqual(version1.getDeltas(), [nodeCreation]), "expected version0 to be empty");
+    assert(_.isEqual(version2.getDeltas(), [nodeCreation, nodeDeletion]), "expected version0 to be empty");
+
+  });
+
+  // it("Invalid version", () => {
+  //   const getId = getUuidCallback();
+
+  //   const nodeCreation = new NodeCreation(getId());
+  //   const nodeDeletion = new NodeDeletion(nodeCreation, []);
+
+  //   const version0 = new Version([]);
+  //   const version1 = new Version([[version0, nodeDeletion]]); // missing dependency: nodeCreation
+  // })
+});

+ 27 - 0
src/onion/version.ts

@@ -0,0 +1,27 @@
+import {
+  Delta,
+} from "./delta";
+
+
+export class Version {
+  readonly parents: Array<[Version, Delta]>;
+
+  constructor(parents: Array<[Version, Delta]>) {
+    this.parents = parents;
+  }
+
+  // slow!
+  getDeltas(): Array<Delta> {
+    return Array<Delta>().concat(... // flatten the following array of arrays (type: Delta[][])
+      this.parents.map(([parentVersion, delta]) => [...parentVersion.getDeltas(), delta])
+    );
+  }
+
+  // findDelta(d: Delta): Version {
+  //   for (const [parentVersion, delta] of this.parents) {
+  //     if (delta === d) {
+  //       return this;
+  //     }
+  //   }
+  // }
+}