Changeset - f61531e21a77
[Not reviewed]
default
0 2 0
drewp@bigasterisk.com - 20 months ago 2023-05-29 22:18:04
drewp@bigasterisk.com
logging
2 files changed with 9 insertions and 6 deletions:
0 comments (0 inline, 0 general)
light9/web/SyncedGraph.ts
Show inline comments
 
@@ -147,44 +147,47 @@ export class SyncedGraph {
 

	
 
  quads(): any {
 
    // for debugging
 
    return Array.from(this.graph.getQuads(null, null, null, null)).map((q: Quad) => [q.subject, q.predicate, q.object, q.graph]);
 
  }
 

	
 
  applyAndSendPatch(patch: Patch) {
 
    console.time("applyAndSendPatch");
 
    if (!this.client) {
 
      log("not connected-- dropping patch");
 
      return;
 
    }
 
    if (patch.isEmpty()) throw 'should not get to this point with empty patch'
 
    if (patch.isEmpty()) throw "should not get to this point with empty patch";
 

	
 
    this._applyPatch(patch);
 
    if (this.client) {
 
      log("sending patch:\n", patch.dump())
 
      log("sending patch:\n", patch.dump());
 
      this.client.sendPatch(patch);
 
    }
 
    console.timeEnd("applyAndSendPatch");
 
  }
 

	
 
  _applyPatch(patch: Patch) {
 
    // In most cases you want applyAndSendPatch.
 
    //
 
    // This is the only method that writes to this.graph!
 
    log("_applyPatch [1]\n", patch.dump());
 
    log("_applyPatch [1] \n", patch.dump());
 
    this.cachedFloatValues.clear();
 
    this.cachedUriValues.clear();
 
    patch.applyToGraph(this.graph);
 
    log("applied patch locally", patch.summary());
 
    log('dump:\n'+patch.dump());
 
    if (false) {
 
      log("applied patch locally", patch.summary());
 
    } else {
 
      log("applied patch locally:\n" + patch.dump());
 
    }
 
    this.autoDeps.graphChanged(patch);
 
  }
 

	
 
  getObjectPatch(s: N3.NamedNode, p: N3.NamedNode, newObject: N3.Quad_Object | null, g: N3.NamedNode): Patch {
 
    // make a patch which removes existing values for (s,p,*,c) and
 
    // adds (s,p,newObject,c). Values in other graphs are not affected.
 
    const existing = this.graph.getQuads(s, p, null, g);
 
    return new Patch(existing, newObject !== null ? [this.Quad(s, p, newObject, g)] : []);
 
  }
 

	
 
  patchObject(s: N3.NamedNode, p: N3.NamedNode, newObject: N3.Quad_Object | null, g: N3.NamedNode) {
 
    this.applyAndSendPatch(this.getObjectPatch(s, p, newObject, g));
light9/web/patch.ts
Show inline comments
 
@@ -85,25 +85,25 @@ export class Patch {
 
          .replace("http://light9.bigasterisk.com/effect/", "effect:")
 
          .replace("http://light9.bigasterisk.com/", ":")
 
          .replace("http://www.w3.org/2000/01/rdf-schema#", "rdfs:")
 
          .replace("http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdf:");
 
      if (term.termType == "BlankNode") return "_:" + term.value;
 
      return term.id;
 
    };
 
    const delPrefix = "- ",
 
      addPrefix = "\u200B+ "; // dels to sort before adds
 
    this.dels.forEach((d) => lines.push(delPrefix + s(d.subject) + " " + s(d.predicate) + " " + s(d.object)));
 
    this.adds.forEach((d) => lines.push(addPrefix + s(d.subject) + " " + s(d.predicate) + " " + s(d.object)));
 
    lines.sort();
 
    return lines.join("\n");
 
    return lines.join("\n") + "\n" + (this.isEmpty() ? "(empty)" : "(nonempty)");
 
  }
 

	
 
  async toJsonPatch(): Promise<string> {
 
    return new Promise((res, rej) => {
 
      const out: SyncgraphPatchMessage = { patch: { adds: "", deletes: "" } };
 

	
 
      const writeDels = (cb1: () => void) => {
 
        const writer = new Writer({ format: "N-Quads" });
 
        writer.addQuads(this.dels.toArray());
 
        writer.end(function (err: any, result: string) {
 
          out.patch.deletes = result;
 
          cb1();
0 comments (0 inline, 0 general)