diff src/Patch.ts @ 139:cf642d395be4

new simpler Patch class; fancier 'hide' view config support
author drewp@bigasterisk.com
date Mon, 08 May 2023 13:05:20 -0700
parents 5a1a79f54779
children
line wrap: on
line diff
--- a/src/Patch.ts	Sat May 06 15:35:11 2023 -0700
+++ b/src/Patch.ts	Mon May 08 13:05:20 2023 -0700
@@ -1,19 +1,24 @@
 import { Quad, Store } from "n3";
 import { Stream } from "rdf-js";
 
+export enum PatchDirection {
+  ADD = "+",
+  DEL = "-",
+}
+
 export class Patch {
-  delQuads: Quad[] = [];
-  addQuads: Quad[] = [];
+  quads: Quad[] = [];
+
+  constructor(public direction: PatchDirection) {}
+
   toString(): string {
-    return `Patch -${this.delQuads.length} +${this.addQuads.length}`;
+    return `Patch ${this.direction} ${this.quads.length}`;
   }
-  constructor() { }
 
-  // fill `addQuads` with this stream
   public async streamImport(quadStream: Stream): Promise<void> {
     return new Promise((resolve, reject) => {
       quadStream.on("data", (quad) => {
-        this.addQuads.push(quad);
+        this.quads.push(quad);
       });
       quadStream.on("error", reject);
       quadStream.on("end", resolve);
@@ -21,7 +26,13 @@
   }
 
   public applyToStore(s: Store) {
-    s.removeQuads(this.delQuads);
-    s.addQuads(this.addQuads);
+    if (this.direction == PatchDirection.ADD) {
+      s.addQuads(this.quads);
+    } else {
+      s.removeQuads(this.quads);
+    }
+  }
+  public isEmpty(): boolean {
+    return this.quads.length == 0;
   }
 }