Mercurial > code > home > repos > streamed-graph
annotate src/streamed_graph_client.ts @ 36:8b4dc9e87b56
reindent to 2-spaces with prettier
author | drewp@bigasterisk.com |
---|---|
date | Sat, 28 Dec 2019 02:24:55 -0800 |
parents | 9ec3cbc8791a |
children | 163b4339804d |
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; | |
14 events!: EventSource; | |
15 constructor( | |
16 eventsUrl: string, | |
17 onGraphChanged: () => void, | |
18 onStatus: (status: string) => void, | |
19 prefixes: Array<Record<string, string>>, | |
20 staticGraphUrls: Array<string> | |
21 ) { | |
22 console.log("new StreamedGraph", eventsUrl); | |
23 this.onStatus = onStatus; | |
24 this.onGraphChanged = onGraphChanged; | |
25 this.onStatus("startup..."); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
26 |
36 | 27 this.store = new Store(); |
28 | |
29 // // Object.keys(prefixes).forEach((prefix) => { | |
30 // // this.store.setPrefix(prefix, prefixes[prefix]); | |
31 // // }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
32 |
36 | 33 this.connect(eventsUrl); |
34 this.reconnectOnWake(); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
35 |
36 | 36 // staticGraphUrls.forEach((url) => { |
37 // fetch(url).then((response) => response.text()) | |
38 // .then((body) => { | |
39 // // parse with n3, add to output | |
40 // }); | |
41 // }); | |
42 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
43 |
36 | 44 reconnectOnWake() { |
45 // 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 | |
46 //window.addEventListener('focus', function() { this.connect(eventsUrl); }.bind(this)); | |
47 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
48 |
36 | 49 connect(eventsUrl: string) { |
50 // 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
|
51 |
36 | 52 this.onStatus("start connect..."); |
53 this.close(); | |
54 if (this.events && this.events.readyState != EventSource.CLOSED) { | |
55 this.onStatus("zombie"); | |
56 throw new Error("zombie eventsource"); | |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
57 } |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
58 |
36 | 59 this.events = new EventSource(eventsUrl); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
60 |
36 | 61 this.events.addEventListener("error", ev => { |
62 // todo: this is piling up tons of retries and eventually multiple connections | |
63 // this.testEventUrl(eventsUrl); | |
64 this.onStatus("connection lost- retrying"); | |
65 setTimeout(() => { | |
66 requestAnimationFrame(() => { | |
67 this.connect(eventsUrl); | |
68 }); | |
69 }, 3000); | |
70 }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
71 |
36 | 72 this.events.addEventListener("fullGraph", async ev => { |
73 this.onStatus("sync- full graph update"); | |
74 await this.replaceFullGraph((ev as MessageEvent).data); | |
75 this.onStatus(`synced ${this.store.size}`); | |
76 this.onGraphChanged(); | |
77 }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
78 |
36 | 79 this.events.addEventListener("patch", async ev => { |
80 this.onStatus("sync- updating"); | |
81 await this.patchGraph((ev as MessageEvent).data); | |
82 this.onStatus(`synced ${this.store.size}`); | |
83 this.onGraphChanged(); | |
84 }); | |
85 this.onStatus("connecting..."); | |
86 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
87 |
36 | 88 // these need some locks |
89 async replaceFullGraph(jsonLdText: string) { | |
90 this.store = new Store(); | |
91 await eachJsonLdQuad( | |
92 JSON.parse(jsonLdText), | |
93 this.store.addQuad.bind(this.store) | |
94 ); | |
95 } | |
5 | 96 |
36 | 97 async patchGraph(patchJson: string) { |
98 var patch = JSON.parse(patchJson).patch; | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
99 |
36 | 100 await eachJsonLdQuad(patch.deletes, this.store.removeQuad.bind(this.store)); |
101 await eachJsonLdQuad(patch.adds, this.store.addQuad.bind(this.store)); | |
102 } | |
103 | |
104 close() { | |
105 if (this.events) { | |
106 this.events.close(); | |
5 | 107 } |
36 | 108 } |
5 | 109 |
36 | 110 async testEventUrl(eventsUrl: string): Promise<void> { |
111 return new Promise<void>((resolve, reject) => { | |
112 this.onStatus("testing connection"); | |
113 fetch(eventsUrl, { | |
114 method: "HEAD", | |
115 credentials: "include" | |
116 }) | |
117 .then(value => { | |
118 if (value.status == 403) { | |
119 reject(); | |
120 return; | |
121 } | |
122 resolve(); | |
123 }) | |
124 .catch(err => { | |
125 reject(); | |
5 | 126 }); |
36 | 127 }); |
128 } | |
129 } |