Mercurial > code > home > repos > streamed-graph
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 |
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 | 4 import { N3Store } from "n3"; |
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 | 8 // holds a n3 Store, which is synced to a server-side |
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 | 11 onStatus: (msg: string) => void; |
12 onGraphChanged: () => void; | |
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 | 15 events!: EventSource; |
16 constructor( | |
17 eventsUrl: string, | |
18 onGraphChanged: () => void, | |
19 onStatus: (status: string) => void, | |
20 prefixes: Array<Record<string, string>>, | |
21 staticGraphUrls: Array<string> | |
22 ) { | |
23 console.log("new StreamedGraph", eventsUrl); | |
24 this.onStatus = onStatus; | |
25 this.onGraphChanged = onGraphChanged; | |
26 this.onStatus("startup..."); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
27 |
36 | 28 this.store = new Store(); |
29 | |
30 // // Object.keys(prefixes).forEach((prefix) => { | |
31 // // this.store.setPrefix(prefix, prefixes[prefix]); | |
32 // // }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
33 |
36 | 34 this.connect(eventsUrl); |
35 this.reconnectOnWake(); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
36 |
36 | 37 // staticGraphUrls.forEach((url) => { |
38 // fetch(url).then((response) => response.text()) | |
39 // .then((body) => { | |
40 // // parse with n3, add to output | |
41 // }); | |
42 // }); | |
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 | 50 reconnectOnWake() { |
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 | |
52 //window.addEventListener('focus', function() { this.connect(eventsUrl); }.bind(this)); | |
53 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
54 |
36 | 55 connect(eventsUrl: string) { |
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 | 58 this.onStatus("start connect..."); |
59 this.close(); | |
60 if (this.events && this.events.readyState != EventSource.CLOSED) { | |
61 this.onStatus("zombie"); | |
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 | 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 | 67 this.events.addEventListener("error", ev => { |
68 // todo: this is piling up tons of retries and eventually multiple connections | |
69 // this.testEventUrl(eventsUrl); | |
70 this.onStatus("connection lost- retrying"); | |
71 setTimeout(() => { | |
72 requestAnimationFrame(() => { | |
73 this.connect(eventsUrl); | |
74 }); | |
75 }, 3000); | |
76 }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
77 |
36 | 78 this.events.addEventListener("fullGraph", async ev => { |
79 this.onStatus("sync- full graph update"); | |
80 await this.replaceFullGraph((ev as MessageEvent).data); | |
81 this.onStatus(`synced ${this.store.size}`); | |
82 this.onGraphChanged(); | |
83 }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
84 |
36 | 85 this.events.addEventListener("patch", async ev => { |
86 this.onStatus("sync- updating"); | |
87 await this.patchGraph((ev as MessageEvent).data); | |
88 this.onStatus(`synced ${this.store.size}`); | |
89 this.onGraphChanged(); | |
90 }); | |
91 this.onStatus("connecting..."); | |
92 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
93 |
36 | 94 // these need some locks |
95 async replaceFullGraph(jsonLdText: string) { | |
96 this.store = new Store(); | |
97 await eachJsonLdQuad( | |
98 JSON.parse(jsonLdText), | |
99 this.store.addQuad.bind(this.store) | |
100 ); | |
101 } | |
5 | 102 |
36 | 103 async patchGraph(patchJson: string) { |
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 | 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 | 116 } |
117 | |
118 close() { | |
119 if (this.events) { | |
120 this.events.close(); | |
5 | 121 } |
36 | 122 } |
5 | 123 |
36 | 124 async testEventUrl(eventsUrl: string): Promise<void> { |
125 return new Promise<void>((resolve, reject) => { | |
126 this.onStatus("testing connection"); | |
127 fetch(eventsUrl, { | |
128 method: "HEAD", | |
129 credentials: "include" | |
130 }) | |
131 .then(value => { | |
132 if (value.status == 403) { | |
133 reject(); | |
134 return; | |
135 } | |
136 resolve(); | |
137 }) | |
138 .catch(err => { | |
139 reject(); | |
5 | 140 }); |
36 | 141 }); |
142 } | |
143 } |