annotate web/AutoDependencies.ts @ 2450:a4052905ca7d default tip

notes about how rdfdb syncs, or should sync
author drewp@bigasterisk.com
date Mon, 03 Jun 2024 23:01:54 -0700
parents 4556eebe5d73
children
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";
2258
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
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
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
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
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
32 log("window.ad");
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
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
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
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
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
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
d3ecee9bfab5 refactor Patch into a class
drewp@bigasterisk.com
parents: 2108
diff changeset
53 this._rerunHandler(h, /*patch=*/ undefined);
2268
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
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
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
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
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
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
7d26fa1ed4e7 renames and comments (mostly)
drewp@bigasterisk.com
parents: 2258
diff changeset
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
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
78 const shorten = (x: Term | null) => {
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
79 if (x === null) {
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
80 return "null";
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
81 }
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
82 if (!Util.isNamedNode(x)) {
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
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
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
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
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
87
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
88 var prn = (h: Handler, indent: string) => {
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
89 log(`${indent} 🤝 handler "${h.label}" ${h.patterns.length} pats`);
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
90 for (let pat of h.patterns) {
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
91 log(`${indent} ⣝ s=${shorten(pat.subject)} p=${shorten(pat.predicate)} o=${shorten(pat.object)}`);
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
92 }
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
93 Array.from(h.innerHandlers).map((c: any) => prn(c, indent + " "));
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
94 };
094e6b84b291 logging and refactor
drewp@bigasterisk.com
parents: 2259
diff changeset
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
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
98 _handlerIsAffected(child: Handler, patch: Patch): boolean {
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
99 // it should be correct but slow to always return true here
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
100 for (let pat of child.patterns) {
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
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
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
114 const match = this._handlerIsAffected(child, patch);
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
115
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
116 if (match) {
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
117 log("match", child.label, match);
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
118 child.innerHandlers = []; // let all children get called again
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
119 this._rerunHandler(child, patch);
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
120 } else {
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
121 rerunInners(child);
9ca3d356b950 attempted rewrite of rerunInners
drewp@bigasterisk.com
parents: 2270
diff changeset
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
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
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
0617b6006ec4 ts cleanup
drewp@bigasterisk.com
parents: 2074
diff changeset
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 }