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
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
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
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
24 // holds a n3 Store, which is synced to a server-side
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
25 // store that sends patches over SSE
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
26 this.onStatus = onStatus;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
27 this.onGraphChanged = onGraphChanged;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
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
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
32 // // Object.keys(prefixes).forEach((prefix) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
33 // // this.store.setPrefix(prefix, prefixes[prefix]);
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
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
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
39 // staticGraphUrls.forEach((url) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
40 // fetch(url).then((response) => response.text())
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
41 // .then((body) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
42 // // parse with n3, add to output
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
43 // });
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
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
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
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
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
78 this.onStatus('sync- full graph update');
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
79 let onReplaced = () => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
80 this.onStatus('synced');
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
81 this.onGraphChanged();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
82 };
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
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
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
87 this.onStatus('sync- updating');
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
88 let onPatched = () => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
89 this.onStatus('synced');
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
90 this.onGraphChanged();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
91 };
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
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
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
97 replaceFullGraph(jsonLdText: string, done: () => void) {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
98 // this.quadStore.clear();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
99 // eachJsonLdQuad(this.store.rdf, JSON.parse(jsonLdText),
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
100 // this.quadStore.add.bind(this.quadStore), function () {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
101 // done();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
102 // });
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
103 // or this.store.insert([quad], quad.graph, function() {});
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
104 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
105
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
106 patchGraph(patchJson: string, done: () => void) {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
107 var patch = JSON.parse(patchJson).patch;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
108
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
109 async.series([
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
110 // (done) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
111 // eachJsonLdQuad(this.store.rdf, patch.deletes,
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
112 // this.quadStore.remove.bind(this.quadStore), done);
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
113 // },
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
114 (done) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
115 // eachJsonLdQuad(this.store.rdf, patch.adds,
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
116 // this.quadStore.add.bind(this.quadStore), done);
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
117 },
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
118 /* seriesDone */ (done) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
119 done();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
120 }
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
121 ], done);
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
122 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
123
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
124 close() {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
125 if (this.events) {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
126 this.events.close();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
127 }
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
128 }
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
129
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
130 testEventUrl(eventsUrl: string): Promise<void> {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
131 return new Promise<void>((resolve, reject) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
132 this.onStatus('testing connection');
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
133 fetch(eventsUrl, {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
134 method: "HEAD",
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
135 credentials: "include",
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
136 }).then((value) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
137 if (value.status == 403) {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
138 reject();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
139 return;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
140 }
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
141 resolve();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
142 }).catch((err) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
143 reject();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
144 });
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
145 });
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
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 }