comparison src/streamed_graph_client.ts @ 3:a7ba8627a7b6

still trying to make imports work. add other files too
author drewp@bigasterisk.com
date Wed, 04 Dec 2019 00:09:15 -0800
parents
children a668a774b162
comparison
equal deleted inserted replaced
2:6cd3aaf431b6 3:a7ba8627a7b6
1 // from /my/site/homepage/www/rdf/streamed-graph.js
2
3 import * as async from "async";
4 import * as jsonld from "jsonld";
5
6 //import eachJsonLdQuad from "./json_ld_quads";
7 import { Store, DataFactory } from "n3"
8
9 /// <reference types="eventsource" />
10 const EventSource = window.EventSource;
11
12 export class StreamedGraphClient {
13 // onStatus: (msg: string) => void;
14 // onGraphChanged: () => void;
15 // store: Store;
16 // events: EventSource;
17 constructor(
18 eventsUrl: string,
19 onGraphChanged: () => void,
20 onStatus: (status: string) => void,
21 prefixes: Array<Record<string, string>>,
22 staticGraphUrls: Array<string>) {
23 console.log('new StreamedGraph', eventsUrl);
24 // holds a rdfstore.js 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...');
29
30 // this.store = new Store({});
31
32 // // Object.keys(prefixes).forEach((prefix) => {
33 // // this.store.setPrefix(prefix, prefixes[prefix]);
34 // // });
35
36 // this.connect(eventsUrl);
37 // this.reconnectOnWake();
38
39 // staticGraphUrls.forEach((url) => {
40 // fetch(url).then((response) => response.text())
41 // .then((body) => {
42 // // parse with n3, add to output
43 // });
44 // });
45
46 }
47
48 // reconnectOnWake() {
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
50 // //window.addEventListener('focus', function() { this.connect(eventsUrl); }.bind(this));
51
52 // }
53
54 // connect(eventsUrl: string) {
55 // // need to exit here if this obj has been replaced
56
57 // this.onStatus('start connect...');
58 // this.close();
59 // if (this.events && this.events.readyState != EventSource.CLOSED) {
60 // this.onStatus('zombie');
61 // throw new Error("zombie eventsource");
62 // }
63
64
65 // this.events = new EventSource(eventsUrl);
66
67 // this.events.addEventListener('error', (ev) => {
68 // // todo: this is piling up tons of retries and eventually multiple connections
69 // this.testEventUrl(eventsUrl);
70 // this.onStatus('connection lost- retrying');
71 // setTimeout(() => {
72 // requestAnimationFrame(() => {
73 // this.connect(eventsUrl);
74 // });
75 // }, 3000);
76 // });
77
78 // this.events.addEventListener('fullGraph', (ev) => {
79 // // this.updates.push({ type: 'fullGraph', data: ev.data });
80 // // this.flushUpdates();
81 // });
82
83 // this.events.addEventListener('patch', (ev) => {
84 // // this.updates.push({ type: 'patch', data: ev.data });
85 // // this.flushUpdates();
86 // });
87 // this.onStatus('connecting...');
88 // }
89
90 // replaceFullGraph(jsonLdText: string, done: () => void) {
91 // // this.quadStore.clear();
92 // // eachJsonLdQuad(this.store.rdf, JSON.parse(jsonLdText),
93 // // this.quadStore.add.bind(this.quadStore), function () {
94 // // done();
95 // // });
96 // // or this.store.insert([quad], quad.graph, function() {});
97 // }
98
99
100 // patchGraph(patchJson: string, done: () => void) {
101 // var patch = JSON.parse(patchJson).patch;
102
103 // // if (!this.store) {
104 // // throw new Error('store ' + this.store);
105 // // }
106
107 // async.series([
108 // // (done) => {
109 // // eachJsonLdQuad(this.store.rdf, patch.deletes,
110 // // this.quadStore.remove.bind(this.quadStore), done);
111 // // },
112 // (done) => {
113 // // eachJsonLdQuad(this.store.rdf, patch.adds,
114 // // this.quadStore.add.bind(this.quadStore), done);
115 // },
116 // /* seriesDone */ (done) => {
117 // done();
118 // }
119 // ], done);
120 // }
121 // close() {
122 // if (this.events) {
123 // this.events.close();
124 // }
125 // }
126
127 // testEventUrl(eventsUrl: string): Promise<void> {
128 // return new Promise<void>((resolve, reject) => {
129 // this.onStatus('testing connection');
130 // fetch(eventsUrl, {
131 // method: "HEAD",
132 // credentials: "include",
133 // }).then((value) => {
134 // if (value.status == 403) {
135 // reject();
136 // return;
137 // }
138 // resolve();
139 // }).catch((err) => {
140 // reject();
141 // });
142 // });
143 // }
144
145 // flushOneUpdate(update: Update, done: () => void) {
146 // if (update.type == 'fullGraph') {
147 // this.onStatus('sync- full graph update');
148 // let onReplaced = () => {
149 // this.onStatus('synced');
150 // this.onGraphChanged();
151 // done();
152 // };
153 // this.replaceFullGraph(update.data, onReplaced);
154 // } else if (update.type == 'patch') {
155 // this.onStatus('sync- updating');
156 // let onPatched = () => {
157 // this.onStatus('synced');
158 // this.onGraphChanged();
159 // done();
160 // };
161 // this.patchGraph(update.data, onPatched);
162 // } else {
163 // this.onStatus('sync- unknown update');
164 // throw new Error(update.type);
165 // }
166 // }
167
168 }