Changeset - cdfd2901918a
[Not reviewed]
default
0 4 0
drewp@bigasterisk.com - 20 months ago 2023-06-01 21:21:20
drewp@bigasterisk.com
logging
4 files changed with 9 insertions and 6 deletions:
0 comments (0 inline, 0 general)
light9/web/RdfDbChannel.ts
Show inline comments
 
@@ -48,27 +48,27 @@ export class RdfDbChannel {
 
  sendMessage(body: string): boolean {
 
    // one try, best effort, true if we think it worked
 
    if (!this.ws || this.ws.readyState !== this.ws.OPEN) {
 
      return false;
 
    }
 
    log("send patch to server, " + body.length + " bytes");
 
    this.ws.send(body);
 
    this.messagesSent++;
 
    this.updateStatus();
 
    return true;
 
  }
 

	
 
  disconnect() {
 
  disconnect(why:string) {
 
    // will be followed by an autoconnect
 
    log("disconnect requested");
 
    log("disconnect requested:", why);
 
    if (this.ws !== undefined) {
 
      const closeHandler = this.ws.onclose?.bind(this.ws);
 
      if (!closeHandler) {
 
        throw new Error();
 
      }
 
      closeHandler(new CloseEvent("forced"));
 
    }
 
  }
 

	
 
  private openConnection() {
 
    const wsOrWss = window.location.protocol.replace("http", "ws");
 
    const fullUrl = wsOrWss + "//" + window.location.host + this.patchSenderUrl;
 
@@ -114,25 +114,25 @@ export class RdfDbChannel {
 
    }
 
  }
 

	
 
  private onPatch(input: SyncgraphPatchMessage) {
 
    log(`patch msg from server`);
 
    this.serverMessage.emit({ evType: "patch", body: input });
 
    this.messagesReceived++;
 
    this.updateStatus();
 
  }
 

	
 
  private onWsError(e: Event) {
 
    log("ws error", e);
 
    this.disconnect();
 
    this.disconnect("ws error");
 
    this.updateStatus();
 
  }
 

	
 
  private onWsClose(ev: CloseEvent) {
 
    log("ws close");
 
    this.updateStatus();
 
    if (this.reconnectTimer !== undefined) {
 
      clearTimeout(this.reconnectTimer);
 
    }
 
    this.reconnectTimer = setTimeout(this.openConnection.bind(this), 1000);
 
  }
 

	
light9/web/SyncedGraph.ts
Show inline comments
 
@@ -29,25 +29,25 @@ export class SyncedGraph {
 
  constructor(
 
    // The /syncedGraph path of an rdfdb server.
 
    patchSenderUrl: string,
 
    // prefixes can be used in Uri(curie) calls. This mapping may grow during loadTrig calls.
 
    public prefixes: Map<string, string>,
 
    private setStatus: (status: string) => void
 
  ) {
 
    this.prefixFuncs = this.rebuildPrefixFuncs(prefixes);
 
    this.graph = new N3.Store();
 
    this.autoDeps = new AutoDependencies(this);
 
    this.autoDeps.graphError.subscribe((e) => {
 
      log("graph learned of error - reconnecting", e);
 
      this.client.disconnect();
 
      this.client.disconnect("graph error");
 
    });
 
    this.clearGraph();
 

	
 
    this.client = new RdfDbClient(patchSenderUrl, this._clearGraphOnNewConnection.bind(this), this._applyPatch.bind(this), this.setStatus);
 
  }
 

	
 
  clearGraph() {
 
    // must not try send a patch to the server!
 
    // just deletes the statements; watchers are unaffected.
 
    this.cachedFloatValues = new Map(); // s + '|' + p -> number
 
    this.cachedUriValues = new Map(); // s + '|' + p -> Uri
 

	
light9/web/patch.ts
Show inline comments
 
@@ -68,24 +68,27 @@ export class Patch {
 
  }
 

	
 
  update(other: Patch): Patch {
 
    // this is approx, since it doesnt handle cancelling existing quads.
 
    return new Patch(this.dels.union(other.dels), this.adds.union(other.adds));
 
  }
 

	
 
  summary(): string {
 
    return "-" + this.dels.size + " +" + this.adds.size;
 
  }
 

	
 
  dump(): string {
 
    if (this.dels.size + this.adds.size > 20) {
 
      return this.summary();
 
    }
 
    const lines: string[] = [];
 
    const s = (term: N3.Term): string => {
 
      if (term.termType == "Literal") return term.value;
 
      if (term.termType == "NamedNode")
 
        return term.value
 
          .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;
 
    };
light9/web/rdfdbclient.ts
Show inline comments
 
@@ -35,26 +35,26 @@ export class RdfDbClient {
 
        }
 
        this.applyPatch(p);
 
      });
 
    });
 
  }
 

	
 
  sendPatch(patch: Patch) {
 
    log("queue patch to server ", patch.summary());
 
    this._patchesToSend.push(patch);
 
    this._continueSending();
 
  }
 

	
 
  disconnect() {
 
    this.channel.disconnect();
 
  disconnect(why:string) {
 
    this.channel.disconnect(why);
 
  }
 

	
 
  async _continueSending() {
 
    // we could call this less often and coalesce patches together to optimize
 
    // the dragging cases. See rdfdb 'compactPatches' and 'processInbox'.
 
    while (this._patchesToSend.length) {
 
      const patch = this._patchesToSend.splice(0, 1)[0];
 
      const json = await patch.toJsonPatch();
 
      const ret = this.channel.sendMessage(json);
 
      if (!ret) {
 
        setTimeout(this._continueSending.bind(this), 500);
 

	
0 comments (0 inline, 0 general)