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