Mercurial > code > home > repos > streamed-graph
annotate src/streamed_graph_client.ts @ 6:a5bc13dcce41
why does import work but accessing the imported thing make it say "Module not found"?
author | drewp@bigasterisk.com |
---|---|
date | Wed, 04 Dec 2019 01:06:33 -0800 |
parents | 8aa42fa04b17 |
children | 6fefd287aff9 |
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 |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
3 import * as async from "async"; |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
4 import * as jsonld from "jsonld"; |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
5 |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
6 //import eachJsonLdQuad from "./json_ld_quads"; |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
7 import { Store, DataFactory } from "n3" |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
8 |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
9 /// <reference types="eventsource" /> |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
10 const EventSource = window.EventSource; |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
11 |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
12 export class StreamedGraphClient { |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
13 onStatus: (msg: string) => void; |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
14 onGraphChanged: () => void; |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
15 store: Store; |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
16 events: EventSource; |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
17 constructor( |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
18 eventsUrl: string, |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
19 onGraphChanged: () => void, |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
20 onStatus: (status: string) => void, |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
21 prefixes: Array<Record<string, string>>, |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
22 staticGraphUrls: Array<string>) { |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
23 console.log('new StreamedGraph', eventsUrl); |
5 | 24 // holds a n3 Store, which is synced to a server-side |
25 // store that sends patches over SSE | |
26 this.onStatus = onStatus; | |
27 this.onGraphChanged = onGraphChanged; | |
28 this.onStatus('startup...'); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
29 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
30 this.store = new Store({}); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
31 |
5 | 32 // // Object.keys(prefixes).forEach((prefix) => { |
33 // // this.store.setPrefix(prefix, prefixes[prefix]); | |
34 // // }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
35 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
36 this.connect(eventsUrl); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
37 this.reconnectOnWake(); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
38 |
5 | 39 // staticGraphUrls.forEach((url) => { |
40 // fetch(url).then((response) => response.text()) | |
41 // .then((body) => { | |
42 // // parse with n3, add to output | |
43 // }); | |
44 // }); | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
45 |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
46 } |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
47 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
48 reconnectOnWake() { |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
49 // 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
|
50 //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
|
51 } |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
52 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
53 connect(eventsUrl: string) { |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
54 // 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
|
55 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
56 this.onStatus('start connect...'); |
5 | 57 this.close(); |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
58 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
|
59 this.onStatus('zombie'); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
60 throw new Error("zombie eventsource"); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
61 } |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
62 |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
63 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
64 this.events = new EventSource(eventsUrl); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
65 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
66 this.events.addEventListener('error', (ev) => { |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
67 // 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
|
68 // this.testEventUrl(eventsUrl); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
69 this.onStatus('connection lost- retrying'); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
70 setTimeout(() => { |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
71 requestAnimationFrame(() => { |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
72 this.connect(eventsUrl); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
73 }); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
74 }, 3000); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
75 }); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
76 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
77 this.events.addEventListener('fullGraph', (ev) => { |
5 | 78 this.onStatus('sync- full graph update'); |
79 let onReplaced = () => { | |
80 this.onStatus('synced'); | |
81 this.onGraphChanged(); | |
82 }; | |
83 this.replaceFullGraph(ev.data, onReplaced); | |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
84 }); |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
85 |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
86 this.events.addEventListener('patch', (ev) => { |
5 | 87 this.onStatus('sync- updating'); |
88 let onPatched = () => { | |
89 this.onStatus('synced'); | |
90 this.onGraphChanged(); | |
91 }; | |
92 this.patchGraph(ev.data, onPatched); | |
4
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
93 }); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
94 this.onStatus('connecting...'); |
a668a774b162
back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents:
3
diff
changeset
|
95 } |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
96 |
5 | 97 replaceFullGraph(jsonLdText: string, done: () => void) { |
98 // this.quadStore.clear(); | |
99 // eachJsonLdQuad(this.store.rdf, JSON.parse(jsonLdText), | |
100 // this.quadStore.add.bind(this.quadStore), function () { | |
101 // done(); | |
102 // }); | |
103 // or this.store.insert([quad], quad.graph, function() {}); | |
104 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
105 |
5 | 106 patchGraph(patchJson: string, done: () => void) { |
107 var patch = JSON.parse(patchJson).patch; | |
108 | |
109 async.series([ | |
110 // (done) => { | |
111 // eachJsonLdQuad(this.store.rdf, patch.deletes, | |
112 // this.quadStore.remove.bind(this.quadStore), done); | |
113 // }, | |
114 (done) => { | |
115 // eachJsonLdQuad(this.store.rdf, patch.adds, | |
116 // this.quadStore.add.bind(this.quadStore), done); | |
117 }, | |
118 /* seriesDone */ (done) => { | |
119 done(); | |
120 } | |
121 ], done); | |
122 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
123 |
5 | 124 close() { |
125 if (this.events) { | |
126 this.events.close(); | |
127 } | |
128 } | |
129 | |
130 testEventUrl(eventsUrl: string): Promise<void> { | |
131 return new Promise<void>((resolve, reject) => { | |
132 this.onStatus('testing connection'); | |
133 fetch(eventsUrl, { | |
134 method: "HEAD", | |
135 credentials: "include", | |
136 }).then((value) => { | |
137 if (value.status == 403) { | |
138 reject(); | |
139 return; | |
140 } | |
141 resolve(); | |
142 }).catch((err) => { | |
143 reject(); | |
144 }); | |
145 }); | |
146 } | |
3
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
147 |
a7ba8627a7b6
still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff
changeset
|
148 } |