annotate src/streamed_graph_client.ts @ 9:26d3e4860adc

working on porting graph_view to n3.js. also working on making tests run
author drewp@bigasterisk.com
date Fri, 06 Dec 2019 09:19:43 -0800
parents 6fefd287aff9
children 7ca4ff2088c3
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";
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
4 // import * as jsonld from "jsonld";
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
5
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
6 import { eachJsonLdQuad } from "./json_ld_quads";
9
26d3e4860adc working on porting graph_view to n3.js. also working on making tests run
drewp@bigasterisk.com
parents: 8
diff changeset
7 import { Store } from "n3"
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
8
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
9 // /// <reference types="eventsource" />
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
10 // const EventSource = window.EventSource;
3
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 {
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
13 // 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
14 // 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
15
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
16 onStatus: (msg: string) => void;
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
17 onGraphChanged: () => void;
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
18 store: Store;
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
19 events: EventSource;
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
20 constructor(
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
21 eventsUrl: string,
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
22 onGraphChanged: () => void,
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
23 onStatus: (status: string) => void,
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
24 prefixes: Array<Record<string, string>>,
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
25 staticGraphUrls: Array<string>) {
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
26 console.log('new StreamedGraph', eventsUrl);
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
27 this.onStatus = onStatus;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
28 this.onGraphChanged = onGraphChanged;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
29 this.onStatus('startup...');
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
30
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
31 this.store = new Store({});
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
32
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
33 // // Object.keys(prefixes).forEach((prefix) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
34 // // this.store.setPrefix(prefix, prefixes[prefix]);
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
35 // // });
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
36
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
37 this.connect(eventsUrl);
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
38 this.reconnectOnWake();
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
39
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
40 // staticGraphUrls.forEach((url) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
41 // fetch(url).then((response) => response.text())
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
42 // .then((body) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
43 // // parse with n3, add to output
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
44 // });
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
45 // });
3
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 }
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 reconnectOnWake() {
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
50 // 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
51 //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
52 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
53
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
54 connect(eventsUrl: string) {
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
55 // 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
56
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
57 this.onStatus('start connect...');
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
58 this.close();
4
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
59 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
60 this.onStatus('zombie');
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
61 throw new Error("zombie eventsource");
a668a774b162 back up, slowly turn on code again until ts breaks
drewp@bigasterisk.com
parents: 3
diff changeset
62 }
3
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 = () => {
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
80 this.onStatus(`synced ${this.store.size}`);
5
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 = () => {
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
89 this.onStatus(`synced ${this.store.size}`);
5
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) {
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
98 this.store = new Store({});
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
99 eachJsonLdQuad(JSON.parse(jsonLdText),
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
100 this.store.addQuad.bind(this.store),
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
101 done);
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
102 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
103
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
104 patchGraph(patchJson: string, done: () => void) {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
105 var patch = JSON.parse(patchJson).patch;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
106
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
107 async.series([
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
108 (done) => {
8
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
109 eachJsonLdQuad(patch.deletes,
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
110 this.store.removeQuad.bind(this.store), done);
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
111 },
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
112 (done) => {
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
113 eachJsonLdQuad(patch.adds,
6fefd287aff9 closer- element now holds a changing graph, but can't draw it yet
drewp@bigasterisk.com
parents: 6
diff changeset
114 this.store.addQuad.bind(this.store), done);
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
115 },
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
116 /* seriesDone */ (done) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
117 done();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
118 }
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 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
121
5
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
122 close() {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
123 if (this.events) {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
124 this.events.close();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
125 }
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
126 }
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
127
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
128 testEventUrl(eventsUrl: string): Promise<void> {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
129 return new Promise<void>((resolve, reject) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
130 this.onStatus('testing connection');
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
131 fetch(eventsUrl, {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
132 method: "HEAD",
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
133 credentials: "include",
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
134 }).then((value) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
135 if (value.status == 403) {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
136 reject();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
137 return;
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
138 }
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
139 resolve();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
140 }).catch((err) => {
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
141 reject();
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
142 });
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
143 });
8aa42fa04b17 enable more code
drewp@bigasterisk.com
parents: 4
diff changeset
144 }
3
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
145
a7ba8627a7b6 still trying to make imports work. add other files too
drewp@bigasterisk.com
parents:
diff changeset
146 }