Changeset - 7d26fa1ed4e7
[Not reviewed]
default
0 4 0
drewp@bigasterisk.com - 20 months ago 2023-05-29 18:44:22
drewp@bigasterisk.com
renames and comments (mostly)
4 files changed with 37 insertions and 34 deletions:
0 comments (0 inline, 0 general)
light9/live/Effect.ts
Show inline comments
 
@@ -145,7 +145,7 @@ export class Effect {
 
    return value != null && value !== 0 && value !== "#000000";
 
  }
 

	
 
  _addEffectSetting(device: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch {
 
  private addEffectSetting(device: NamedNode, deviceAttr: NamedNode, value: ControlValue): Patch {
 
    log("  _addEffectSetting", deviceAttr.value, value);
 
    const U = (x: string) => this.graph.Uri(x);
 
    const quad = (s: Quad_Subject, p: Quad_Predicate, o: Quad_Object) => this.graph.Quad(s, p, o, this.ctxForEffect);
 
@@ -171,12 +171,12 @@ export class Effect {
 
    return this.graph.getObjectPatch(
 
      effectSetting, //
 
      valuePred(this.graph, deviceAttr),
 
      this._nodeForValue(value),
 
      this.nodeForValue(value),
 
      this.ctxForEffect
 
    );
 
  }
 

	
 
  _removeEffectSetting(effectSetting: NamedNode): Patch {
 
  private removeEffectSetting(effectSetting: NamedNode): Patch {
 
    const U = (x: string) => this.graph.Uri(x);
 
    log("  _removeEffectSetting", effectSetting.value);
 
    const toDel = [this.graph.Quad(this.uri, U(":setting"), effectSetting, this.ctxForEffect)];
 
@@ -186,7 +186,7 @@ export class Effect {
 
    return new Patch(toDel, []);
 
  }
 

	
 
  _nodeForValue(value: ControlValue): NamedNode | Literal {
 
  private nodeForValue(value: ControlValue): NamedNode | Literal {
 
    if (value === null) {
 
      throw new Error("no value");
 
    }
light9/live/Light9DeviceSettings.ts
Show inline comments
 
@@ -73,7 +73,7 @@ export class Light9DeviceSettings extend
 

	
 
    getTopGraph().then((g) => {
 
      this.graph = g;
 
      this.graph.runHandler(this.findDevices.bind(this), "findDevices");
 
      this.graph.runHandler(this.compile.bind(this), "findDevices");
 
      this.setEffectFromUrl();
 
    });
 
  }
 
@@ -98,7 +98,7 @@ export class Light9DeviceSettings extend
 

	
 
  // Note that this doesn't fetch setting values, so it only should get rerun
 
  // upon (rarer) changes to the devices etc. todo: make that be true
 
  findDevices(patch?: Patch) {
 
  private compile(patch?: Patch) {
 
    const U = this.graph.U();
 
    // if (patch && !patchContainsPreds(patch, [U("rdf:type")])) {
 
    //   return;
light9/web/AutoDependencies.ts
Show inline comments
 
@@ -74,7 +74,7 @@ export class AutoDependencies {
 
    }
 
  }
 
  // handler might have no watches, in which case we could forget about it
 
  _logHandlerTree() {
 
  logHandlerTree() {
 
    log("handler tree:");
 
    var prn = function (h: Handler, depth: number) {
 
      let indent = "";
light9/web/SyncedGraph.ts
Show inline comments
 
@@ -5,19 +5,20 @@ import { sortBy, unique } from "undersco
 
import { AutoDependencies, HandlerFunc } from "./AutoDependencies";
 
import { Patch, patchToDeleteEntireGraph } from "./patch";
 
import { RdfDbClient } from "./rdfdbclient";
 

	
 
const log = debug("graph");
 

	
 
const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
 

	
 
export class SyncedGraph {
 
  private _autoDeps: AutoDependencies;
 
  private _client: RdfDbClient;
 
  private autoDeps: AutoDependencies;
 
  private client: RdfDbClient;
 
  private graph: N3.Store;
 
  cachedFloatValues: any;
 
  cachedUriValues: any;
 
  private cachedFloatValues: Map<string, number> = new Map();
 
  private cachedUriValues: Map<string, N3.NamedNode> = new Map();
 
  private prefixFuncs: (prefix: string) => N3.PrefixedToIri;
 
  serial: any;
 
  _nextNumber: any;
 
  private serial: any;
 
  private nextNumber: any;
 
  // Main graph object for a browser to use. Consider using RdfdbSyncedGraph element to create & own
 
  // one of these. Syncs both ways with rdfdb. Meant to hide the choice of RDF lib, so we can change it
 
  // later.
 
@@ -26,23 +27,24 @@ export class SyncedGraph {
 
  // it can fire subscriptions.
 

	
 
  constructor(
 
    // url is the /syncedGraph path of an rdfdb server.
 
    public url: any,
 
    // 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: any,
 
    // called if we clear the graph
 
    private clearCb: any
 
    private setStatus: (status: string) => void
 
  ) {
 
    this.prefixFuncs = this.rebuildPrefixFuncs(prefixes);
 
    this.graph = new N3.Store();
 
    this._autoDeps = new AutoDependencies();
 
    this.autoDeps = new AutoDependencies(this);
 
    this.clearGraph();
 

	
 
    this._client = new RdfDbClient(this.url, this._clearGraphOnNewConnection.bind(this), this._applyPatch.bind(this), this.setStatus);
 
    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
 
@@ -54,7 +56,8 @@ export class SyncedGraph {
 
  }
 

	
 
  _clearGraphOnNewConnection() {
 
    // must not send a patch to the server!
 
    // must not try send a patch to the server
 

	
 
    log("clearGraphOnNewConnection");
 
    this.clearGraph();
 
    log("clearGraphOnNewConnection done");
 
@@ -140,14 +143,14 @@ export class SyncedGraph {
 

	
 
  applyAndSendPatch(patch: Patch) {
 
    console.time("applyAndSendPatch");
 
    if (!this._client) {
 
    if (!this.client) {
 
      log("not connected-- dropping patch");
 
      return;
 
    }
 

	
 
    this._applyPatch(patch);
 
    if (this._client) {
 
      this._client.sendPatch(patch);
 
    if (this.client) {
 
      this.client.sendPatch(patch);
 
    }
 
    console.timeEnd("applyAndSendPatch");
 
  }
 
@@ -193,11 +196,11 @@ export class SyncedGraph {
 
    this.serial += 1;
 
    //label = label + @serial
 

	
 
    this._autoDeps.runHandler(func, label);
 
    this.autoDeps.runHandler(func, label);
 
  }
 

	
 
  _singleValue(s: Quad_Subject, p: Quad_Predicate) {
 
    this._autoDeps.askedFor(s, p, null, null);
 
    this.autoDeps.askedFor(s, p, null, null);
 
    const quads = this.graph.getQuads(s, p, null, null);
 
    const objs = new Set(Array.from(quads).map((q: Quad) => q.object));
 

	
 
@@ -260,19 +263,19 @@ export class SyncedGraph {
 
  }
 

	
 
  objects(s: any, p: any): Quad_Object[] {
 
    this._autoDeps.askedFor(s, p, null, null);
 
    this.autoDeps.askedFor(s, p, null, null);
 
    const quads = this.graph.getQuads(s, p, null, null);
 
    return Array.from(quads).map((q: { object: any }) => q.object);
 
  }
 

	
 
  subjects(p: any, o: any): Quad_Subject[] {
 
    this._autoDeps.askedFor(null, p, o, null);
 
    this.autoDeps.askedFor(null, p, o, null);
 
    const quads = this.graph.getQuads(null, p, o, null);
 
    return Array.from(quads).map((q: { subject: any }) => q.subject);
 
  }
 

	
 
  subjectStatements(s: Quad_Subject): Quad[] {
 
    this._autoDeps.askedFor(s, null, null, null);
 
    this.autoDeps.askedFor(s, null, null, null);
 
    const quads = this.graph.getQuads(s, null, null, null);
 
    return quads;
 
  }
 
@@ -285,7 +288,7 @@ export class SyncedGraph {
 
        break;
 
      }
 

	
 
      this._autoDeps.askedFor(current, null, null, null); // a little loose
 
      this.autoDeps.askedFor(current, null, null, null); // a little loose
 

	
 
      const firsts = this.graph.getQuads(current, RDF + "first", null, null);
 
      const rests = this.graph.getQuads(current, RDF + "rest", null, null);
 
@@ -304,7 +307,7 @@ export class SyncedGraph {
 
  }
 

	
 
  contains(s: any, p: any, o: any): boolean {
 
    this._autoDeps.askedFor(s, p, o, null);
 
    this.autoDeps.askedFor(s, p, o, null);
 
    // Sure this is a nice warning to remind me to rewrite, but the graph.size call itself was taking 80% of the time in here
 
    // log("contains calling getQuads when graph has ", this.graph.size);
 
    return this.graph.getQuads(s, p, o, null).length > 0;
 
@@ -320,10 +323,10 @@ export class SyncedGraph {
 
    const results = [];
 

	
 
    // @contains is really slow.
 
    if (this._nextNumber == null) {
 
      this._nextNumber = new Map();
 
    if (this.nextNumber == null) {
 
      this.nextNumber = new Map();
 
    }
 
    let start = this._nextNumber.get(base);
 
    let start = this.nextNumber.get(base);
 
    if (start === undefined) {
 
      start = 0;
 
    }
 
@@ -333,7 +336,7 @@ export class SyncedGraph {
 
      if (!this.contains(uri, null, null)) {
 
        results.push(uri);
 
        log("nextNumberedResources", `picked ${uri}`);
 
        this._nextNumber.set(base, serial + 1);
 
        this.nextNumber.set(base, serial + 1);
 
        if (results.length >= howMany) {
 
          return results;
 
        }
 
@@ -347,7 +350,7 @@ export class SyncedGraph {
 
  }
 

	
 
  contextsWithPattern(s: any, p: any, o: any) {
 
    this._autoDeps.askedFor(s, p, o, null);
 
    this.autoDeps.askedFor(s, p, o, null);
 
    const ctxs = [];
 
    for (let q of Array.from(this.graph.getQuads(s, p, o, null))) {
 
      ctxs.push(q.graph);
0 comments (0 inline, 0 general)