annotate light9/web/AutoDependencies.ts @ 2108:e92db17f3e7e

effectSequencer can now also process some note-like values coming from the fade/ ui
author drewp@bigasterisk.com
date Wed, 01 Jun 2022 17:02:46 -0700
parents 0617b6006ec4
children d3ecee9bfab5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
1 import debug from "debug";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
2 import { Quad_Graph, Quad_Object, Quad_Predicate, Quad_Subject } from "n3";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
3 import { filter } from "underscore";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
4 import { allPatchSubjs, Patch } from "./patch";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
5
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
6 const log = debug("autodep");
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
7
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
8 interface QuadPattern {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
9 subject: Quad_Subject | null;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
10 predicate: Quad_Predicate | null;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
11 object: Quad_Object | null;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
12 graph: Quad_Graph | null;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
13 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
14
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
15 // use patch as an optional optimization, but you can't count on it
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
16 export type HandlerFunc = (p?: Patch) => void;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
17
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
18 class Handler {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
19 patterns: QuadPattern[];
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
20 innerHandlers: Handler[];
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
21 // a function and the quad patterns it cared about
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
22 constructor(public func: HandlerFunc | null, public label: string) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
23 this.patterns = []; // s,p,o,g quads that should trigger the next run
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
24 this.innerHandlers = []; // Handlers requested while this one was running
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
25 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
26 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
27
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
28 export class AutoDependencies {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
29 handlers: Handler;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
30 handlerStack: Handler[];
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
31 constructor() {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
32 // tree of all known Handlers (at least those with non-empty
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
33 // patterns). Top node is not a handler.
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
34 this.handlers = new Handler(null, "root");
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
35 this.handlerStack = [this.handlers]; // currently running
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
36 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
37
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
38 runHandler(func: HandlerFunc, label: string) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
39 // what if we have this func already? duplicate is safe?
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
40 if (label == null) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
41 throw new Error("missing label");
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
42 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
43
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
44 const h = new Handler(func, label);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
45 const tailChildren = this.handlerStack[this.handlerStack.length - 1].innerHandlers;
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
46 const matchingLabel = filter(tailChildren, (c: Handler) => c.label === label).length;
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
47 // ohno, something depends on some handlers getting run twice :(
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
48 if (matchingLabel < 2) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
49 tailChildren.push(h);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
50 }
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
51 //console.time("handler #{label}")
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
52 // todo: this may fire 1-2 times before the
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
53 // graph is initially loaded, which is a waste. Try deferring it if we
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
54 // haven't gotten the graph yet.
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
55 this._rerunHandler(h, undefined);
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
56 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
57 //console.timeEnd("handler #{label}")
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
58 //@_logHandlerTree()
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
59 _rerunHandler(handler: Handler, patch?: Patch) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
60 handler.patterns = [];
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
61 this.handlerStack.push(handler);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
62 try {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
63 if (handler.func === null) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
64 throw new Error("tried to rerun root");
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
65 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
66 handler.func(patch);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
67 } catch (e) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
68 log("error running handler: ", e);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
69 } finally {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
70 // assuming here it didn't get to do all its queries, we could
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
71 // add a *,*,*,* handler to call for sure the next time?
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
72 // log('done. got: ', handler.patterns)
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
73 this.handlerStack.pop();
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
74 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
75 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
76 // handler might have no watches, in which case we could forget about it
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
77 _logHandlerTree() {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
78 log("handler tree:");
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
79 var prn = function (h: Handler, depth: number) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
80 let indent = "";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
81 for (let i = 0; i < depth; i++) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
82 indent += " ";
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
83 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
84 log(`${indent} \"${h.label}\" ${h.patterns.length} pats`);
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
85 Array.from(h.innerHandlers).map((c: any) => prn(c, depth + 1));
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
86 };
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
87 prn(this.handlers, 0);
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
88 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
89
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
90 _handlerIsAffected(child: Handler, patchSubjs: Set<string>) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
91 if (patchSubjs === null) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
92 return true;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
93 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
94 if (!child.patterns.length) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
95 return false;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
96 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
97
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
98 for (let stmt of Array.from(child.patterns)) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
99 if (stmt.subject === null) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
100 // wildcard on subject
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
101 return true;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
102 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
103 if (patchSubjs.has(stmt.subject.value)) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
104 return true;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
105 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
106 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
107
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
108 return false;
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
109 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
110
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
111 graphChanged(patch: Patch) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
112 // SyncedGraph is telling us this patch just got applied to the graph.
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
113 const subjs = allPatchSubjs(patch);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
114
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
115 var rerunInners = (cur: Handler) => {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
116 const toRun = cur.innerHandlers.slice();
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
117 for (let child of Array.from(toRun)) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
118 //match = @_handlerIsAffected(child, subjs)
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
119 //continue if not match
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
120 //log('match', child.label, match)
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
121 //child.innerHandlers = [] # let all children get called again
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
122 this._rerunHandler(child, patch);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
123 rerunInners(child);
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
124 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
125 };
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
126 rerunInners(this.handlers);
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
127 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
128
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
129 askedFor(s: Quad_Subject | null, p: Quad_Predicate | null, o: Quad_Object | null, g: Quad_Graph | null) {
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
130 // SyncedGraph is telling us someone did a query that depended on
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
131 // quads in the given pattern.
2108
e92db17f3e7e effectSequencer can now also process some note-like values coming from the fade/ ui
drewp@bigasterisk.com
parents: 2088
diff changeset
132 // console.log(` asked for s/${s?.id} p/${p?.id} o/${o?.id}`)
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
133 const current = this.handlerStack[this.handlerStack.length - 1];
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
134 if (current != null && current !== this.handlers) {
2088
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
135 current.patterns.push({ subject: s, predicate: p, object: o, graph: g } as QuadPattern);
2074
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
136 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
137 }
1a96f8647126 big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff changeset
138 }