annotate src/streamed_graph_client.ts @ 67:163b4339804d

try addressing decreasing performance by rebuilding the graph occasionally
author drewp@bigasterisk.com
date Sun, 26 Jan 2020 22:07:19 -0800
parents 8b4dc9e87b56
children 0c188ed3bcd8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
1 // from /my/site/homepage/www/rdf/streamed-graph.js
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
2
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
3 import { eachJsonLdQuad } from "./json_ld_quads";
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
4 import { N3Store } from "n3";
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
5 import { Store } from "n3";
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
6
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
7 export class StreamedGraphClient {
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
8 // holds a n3 Store, which is synced to a server-side
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
9 // store that sends patches over SSE
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
10
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
11 onStatus: (msg: string) => void;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
12 onGraphChanged: () => void;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
13 store: N3Store;
67
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
14 _deletedCount: number = 0;
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
15 events!: EventSource;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
16 constructor(
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
17 eventsUrl: string,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
18 onGraphChanged: () => void,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
19 onStatus: (status: string) => void,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
20 prefixes: Array<Record<string, string>>,
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
21 staticGraphUrls: Array<string>
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
22 ) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
23 console.log("new StreamedGraph", eventsUrl);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
24 this.onStatus = onStatus;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
25 this.onGraphChanged = onGraphChanged;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
26 this.onStatus("startup...");
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
27
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
28 this.store = new Store();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
29
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
30 // // Object.keys(prefixes).forEach((prefix) => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
31 // // this.store.setPrefix(prefix, prefixes[prefix]);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
32 // // });
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
33
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
34 this.connect(eventsUrl);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
35 this.reconnectOnWake();
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
36
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
37 // staticGraphUrls.forEach((url) => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
38 // fetch(url).then((response) => response.text())
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
39 // .then((body) => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
40 // // parse with n3, add to output
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
41 // });
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
42 // });
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
43 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
44
67
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
45 _vacuum() {
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
46 // workaround for the growing _ids map
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
47 this.store = new Store(this.store.getQuads(null, null, null, null));
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
48 }
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
49
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
50 reconnectOnWake() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
51 // it's not this, which fires on every mouse-in on a browser window, and doesn't seem to work for screen-turned-back-on
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
52 //window.addEventListener('focus', function() { this.connect(eventsUrl); }.bind(this));
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
53 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
54
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
55 connect(eventsUrl: string) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
56 // need to exit here if this obj has been replaced
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
57
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
58 this.onStatus("start connect...");
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
59 this.close();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
60 if (this.events && this.events.readyState != EventSource.CLOSED) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
61 this.onStatus("zombie");
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
62 throw new Error("zombie eventsource");
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
63 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
64
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
65 this.events = new EventSource(eventsUrl);
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
66
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
67 this.events.addEventListener("error", ev => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
68 // todo: this is piling up tons of retries and eventually multiple connections
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
69 // this.testEventUrl(eventsUrl);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
70 this.onStatus("connection lost- retrying");
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
71 setTimeout(() => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
72 requestAnimationFrame(() => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
73 this.connect(eventsUrl);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
74 });
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
75 }, 3000);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
76 });
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
77
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
78 this.events.addEventListener("fullGraph", async ev => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
79 this.onStatus("sync- full graph update");
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
80 await this.replaceFullGraph((ev as MessageEvent).data);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
81 this.onStatus(`synced ${this.store.size}`);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
82 this.onGraphChanged();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
83 });
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
84
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
85 this.events.addEventListener("patch", async ev => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
86 this.onStatus("sync- updating");
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
87 await this.patchGraph((ev as MessageEvent).data);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
88 this.onStatus(`synced ${this.store.size}`);
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
89 this.onGraphChanged();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
90 });
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
91 this.onStatus("connecting...");
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
92 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
93
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
94 // these need some locks
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
95 async replaceFullGraph(jsonLdText: string) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
96 this.store = new Store();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
97 await eachJsonLdQuad(
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
98 JSON.parse(jsonLdText),
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
99 this.store.addQuad.bind(this.store)
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
100 );
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
101 }
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
102
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
103 async patchGraph(patchJson: string) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
104 var patch = JSON.parse(patchJson).patch;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
105
67
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
106 await eachJsonLdQuad(patch.deletes, quad => {
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
107 this.store.removeQuad(quad);
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
108 this._deletedCount++;
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
109 });
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
110 await eachJsonLdQuad(patch.adds, this.store.addQuad.bind(this.store));
67
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
111
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
112 if (this._deletedCount > 100) {
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
113 this._vacuum();
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
114 this._deletedCount = 0;
163b4339804d try addressing decreasing performance by rebuilding the graph occasionally
drewp@bigasterisk.com
parents: 36
diff changeset
115 }
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
116 }
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
117
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
118 close() {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
119 if (this.events) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
120 this.events.close();
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
121 }
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
122 }
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
123
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
124 async testEventUrl(eventsUrl: string): Promise<void> {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
125 return new Promise<void>((resolve, reject) => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
126 this.onStatus("testing connection");
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
127 fetch(eventsUrl, {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
128 method: "HEAD",
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
129 credentials: "include"
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
130 })
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
131 .then(value => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
132 if (value.status == 403) {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
133 reject();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
134 return;
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
135 }
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
136 resolve();
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
137 })
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
138 .catch(err => {
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
139 reject();
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
140 });
36
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
141 });
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
142 }
8b4dc9e87b56 reindent to 2-spaces with prettier
drewp@bigasterisk.com
parents: 20
diff changeset
143 }