Changeset - bbd6816d9e9e
[Not reviewed]
default
0 4 0
drewp@bigasterisk.com - 3 years ago 2022-05-29 09:54:57
drewp@bigasterisk.com
big optimization on effect editing. 50% time still in rebuildSettingsFromGraph though, and the rest is in setLabel
4 files changed with 18 insertions and 8 deletions:
0 comments (0 inline, 0 general)
light9/web/live/Effect.ts
Show inline comments
 
import debug from "debug";
 
import { Literal, NamedNode, Quad_Object, Quad_Predicate, Quad_Subject, Term } from "n3";
 
import { some } from "underscore";
 
import { Patch } from "../patch";
 
import { Patch, patchContainsPreds } from "../patch";
 
import { SyncedGraph } from "../SyncedGraph";
 

	
 
type Color = string;
 
export type ControlValue = number | Color | NamedNode;
 

	
 
const log = debug("effect");
 
@@ -53,20 +53,25 @@ export class Effect {
 
    const patch = { adds: addQuads, dels: [] } as Patch;
 
    log("init new effect", patch);
 
    this.settings = [];
 
    this.graph.applyAndSendPatch(patch);
 
  }
 

	
 
  rebuildSettingsFromGraph() {
 
  rebuildSettingsFromGraph(patch?: Patch) {
 
    const U = this.graph.U();
 
    log("syncFromGraph", this.uri);
 
    if (patch && !patchContainsPreds(patch, [U(":setting"), U(":device"), U(":deviceAttr")])) {
 
      // that's an approx list of preds , but it just means we'll miss some pathological settings edits
 
    //   return;
 
    }
 

	
 
    // log("syncFromGraph", this.uri);
 

	
 
    const newSettings = [];
 

	
 
    for (let setting of Array.from(this.graph.objects(this.uri, U(":setting")))) {
 
      log(`  setting ${setting.value}`);
 
      //   log(`  setting ${setting.value}`);
 
      if (!isUri(setting)) throw new Error();
 
      let value: ControlValue;
 
      const device = this.graph.uriValue(setting, U(":device"));
 
      const deviceAttr = this.graph.uriValue(setting, U(":deviceAttr"));
 

	
 
      const pred = valuePred(this.graph, deviceAttr);
 
@@ -79,28 +84,30 @@ export class Effect {
 
        try {
 
          value = this.graph.floatValue(setting, pred);
 
        } catch (error1) {
 
          value = this.graph.stringValue(setting, pred); // this may find multi values and throw
 
        }
 
      }
 
      log(`change: graph contains ${deviceAttr.value} ${value}`);
 
    //   log(`change: graph contains ${deviceAttr.value} ${value}`);
 

	
 
      newSettings.push({ device, deviceAttr, setting, value });
 
    }
 
    this.settings = newSettings;
 
    log(`rebuild to ${this.settings.length}`);
 
    this.onValuesChanged();
 
  }
 

	
 
  currentValue(device: NamedNode, deviceAttr: NamedNode): ControlValue | null {
 
    for (let s of this.settings) {
 
      if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) {
 
        return s.value;
 
      }
 
    }
 
    return null;
 
  }
 

	
 
  // change this object now, but return the patch to be applied to the graph so it can be coalesced.
 
  edit(device: NamedNode, deviceAttr: NamedNode, newValue: ControlValue | null): Patch {
 
    log(`edit: value=${newValue}`);
 
    let existingSetting: NamedNode | null = null;
 
    for (let s of this.settings) {
 
      if (device.equals(s.device) && deviceAttr.equals(s.deviceAttr)) {
light9/web/live/GraphToControls.ts
Show inline comments
 
@@ -3,13 +3,13 @@ import { NamedNode } from "n3";
 
import { SyncedGraph } from "../SyncedGraph";
 
import { ControlValue, Effect } from "./Effect";
 
const log = debug("g2c");
 

	
 
type NewValueCb = (newValue: ControlValue | null) => void;
 

	
 
// More efficient bridge between liveControl widgets and graph edits,
 
// More efficient bridge between liveControl widgets and graph edits (inside Effect),
 
// as opposed to letting each widget scan the graph and push lots of
 
// tiny patches to it.
 
export class GraphToControls {
 
  // rename to PageControls?
 
  effect: Effect | null = null; // this uri should sync to the editchoice
 
  registeredWidgets: Map<NamedNode, Map<NamedNode, NewValueCb>> = new Map();
light9/web/live/Light9DeviceControl.ts
Show inline comments
 
@@ -143,13 +143,13 @@ export class Light9DeviceControl extends
 
    }
 
    // this.syncDeviceAttrsFromGraph();
 
  }
 

	
 
  syncDeviceAttrsFromGraph(patch?: Patch) {
 
    const U = this.graph.U();
 
    if (patch != null && !patchContainsPreds(patch, [U("rdf:type"), U(":deviceAttr"), U(":dataType"), U(":choice")])) {
 
    if (patch && !patchContainsPreds(patch, [U("rdf:type"), U(":deviceAttr"), U(":dataType"), U(":choice")])) {
 
      return;
 
    }
 
    try {
 
    this.deviceClass = this.graph.uriValue(this.uri, U("rdf:type"));
 
    } catch(e) {
 
      // what's likely is we're going through a graph reload and the graph 
light9/web/live/Light9LiveControls.ts
Show inline comments
 
import debug from "debug";
 
import { css, html, LitElement, PropertyValues } from "lit";
 
import { customElement, property } from "lit/decorators.js";
 
import { NamedNode } from "n3";
 
import { sortBy, uniq } from "underscore";
 
import { Patch } from "../patch";
 
import { Patch, patchContainsPreds } from "../patch";
 
import { getTopGraph } from "../RdfdbSyncedGraph";
 
import { SyncedGraph } from "../SyncedGraph";
 
import { GraphToControls } from "./GraphToControls";
 
export { Light9DeviceControl as Light9LiveDeviceControl } from "./Light9DeviceControl";
 
export { EditChoice } from "../EditChoice";
 
const log = debug("controls");
 
@@ -87,12 +87,15 @@ export class Light9LiveControls extends 
 
      this.onEffectChoice();
 
    }
 
  }
 

	
 
  findDevices(patch?: Patch) {
 
    const U = this.graph.U();
 
    if (patch && !patchContainsPreds(patch, [U("rdf:type")])) {
 
      return;
 
    }
 

	
 
    this.devices = [];
 
    let classes = this.graph.subjects(U("rdf:type"), U(":DeviceClass"));
 
    log(`found ${classes.length} device classes`)
 
    uniq(sortBy(classes, "value"), true).forEach((dc) => {
 
      sortBy(this.graph.subjects(U("rdf:type"), dc), "value").forEach((dev) => {
0 comments (0 inline, 0 general)