Mercurial > code > home > repos > light9
annotate web/AutoDependencies.ts @ 2435:207fe0670952
+ bin/rdf2dot
author | drewp@bigasterisk.com |
---|---|
date | Wed, 29 May 2024 14:56:58 -0700 |
parents | 4556eebe5d73 |
children |
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"; |
2258 | 2 import { NamedNode, Quad_Graph, Quad_Object, Quad_Predicate, Quad_Subject, Term, Util } from "n3"; |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
3 import { filter } from "underscore"; |
2258 | 4 import { Patch, QuadPattern } from "./patch"; |
2270
b51c74da9d35
more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents:
2268
diff
changeset
|
5 import { SubEvent } from "sub-events"; |
b51c74da9d35
more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents:
2268
diff
changeset
|
6 import { SyncedGraph } from "./SyncedGraph"; |
2074
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 const log = debug("autodep"); |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
9 |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
10 // 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
|
11 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
|
12 |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
13 class Handler { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
14 patterns: QuadPattern[]; |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
15 innerHandlers: Handler[]; |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
16 // 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
|
17 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
|
18 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
|
19 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
|
20 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
21 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
22 |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
23 export class AutoDependencies { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
24 handlers: Handler; |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
25 handlerStack: Handler[]; |
2270
b51c74da9d35
more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents:
2268
diff
changeset
|
26 graphError: SubEvent<string> = new SubEvent(); |
b51c74da9d35
more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents:
2268
diff
changeset
|
27 constructor(private graph: SyncedGraph) { |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
28 // 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
|
29 // 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
|
30 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
|
31 this.handlerStack = [this.handlers]; // currently running |
2268 | 32 log("window.ad"); |
33 (window as any).ad = this; | |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
34 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
35 |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
36 runHandler(func: HandlerFunc, label: string) { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
37 // 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
|
38 if (label == null) { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
39 throw new Error("missing label"); |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
40 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
41 |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
42 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
|
43 const tailChildren = this.handlerStack[this.handlerStack.length - 1].innerHandlers; |
2088 | 44 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
|
45 // 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
|
46 if (matchingLabel < 2) { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
47 tailChildren.push(h); |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
48 } |
2088 | 49 //console.time("handler #{label}") |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
50 // 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
|
51 // 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
|
52 // haven't gotten the graph yet. |
2258 | 53 this._rerunHandler(h, /*patch=*/ undefined); |
2268 | 54 log(`new handler ${label} ran first time and requested ${h.patterns.length} pats`); |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
55 } |
2268 | 56 |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
57 _rerunHandler(handler: Handler, patch?: Patch) { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
58 handler.patterns = []; |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
59 this.handlerStack.push(handler); |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
60 try { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
61 if (handler.func === null) { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
62 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
|
63 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
64 handler.func(patch); |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
65 } catch (e) { |
2268 | 66 this.graphError.emit(String(e)); |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
67 } finally { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
68 // 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
|
69 // 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
|
70 // log('done. got: ', handler.patterns) |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
71 this.handlerStack.pop(); |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
72 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
73 } |
2270
b51c74da9d35
more cleanup- mixed up with other commits
drewp@bigasterisk.com
parents:
2268
diff
changeset
|
74 |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
75 // handler might have no watches, in which case we could forget about it |
2259 | 76 logHandlerTree() { |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
77 log("handler tree:"); |
2268 | 78 const shorten = (x: Term | null) => { |
79 if (x === null) { | |
80 return "null"; | |
81 } | |
82 if (!Util.isNamedNode(x)) { | |
83 return x.value; | |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
84 } |
2268 | 85 return this.graph.shorten(x as NamedNode); |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
86 }; |
2268 | 87 |
88 var prn = (h: Handler, indent: string) => { | |
89 log(`${indent} 🤝 handler "${h.label}" ${h.patterns.length} pats`); | |
90 for (let pat of h.patterns) { | |
91 log(`${indent} ⣝ s=${shorten(pat.subject)} p=${shorten(pat.predicate)} o=${shorten(pat.object)}`); | |
92 } | |
93 Array.from(h.innerHandlers).map((c: any) => prn(c, indent + " ")); | |
94 }; | |
95 prn(this.handlers, ""); | |
2074
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 |
2272 | 98 _handlerIsAffected(child: Handler, patch: Patch): boolean { |
99 // it should be correct but slow to always return true here | |
100 for (let pat of child.patterns) { | |
101 if (patch.matches(pat)) { | |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
102 return true; |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
103 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
104 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
105 return false; |
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 graphChanged(patch: Patch) { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
109 // 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
|
110 |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
111 var rerunInners = (cur: Handler) => { |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
112 const toRun = cur.innerHandlers.slice(); |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
113 for (let child of Array.from(toRun)) { |
2272 | 114 const match = this._handlerIsAffected(child, patch); |
115 | |
116 if (match) { | |
117 log("match", child.label, match); | |
118 child.innerHandlers = []; // let all children get called again | |
119 this._rerunHandler(child, patch); | |
120 } else { | |
121 rerunInners(child); | |
122 } | |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
123 } |
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
124 }; |
2088 | 125 rerunInners(this.handlers); |
2074
1a96f8647126
big graph & autodep porting to make collector display labels from a syncedgraph
drewp@bigasterisk.com
parents:
diff
changeset
|
126 } |
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 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
|
129 // 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
|
130 // 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
|
131 // 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
|
132 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
|
133 if (current != null && current !== this.handlers) { |
2088 | 134 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
|
135 } |
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 } |