comparison src/streamed_graph_client.ts @ 80:7c93e17b1111

more fixes, more original code uncommented
author drewp@bigasterisk.com
date Wed, 17 Nov 2021 15:00:17 -0800
parents 0c188ed3bcd8
children 43e016fa302e
comparison
equal deleted inserted replaced
79:0c188ed3bcd8 80:7c93e17b1111
1 // // from /my/site/homepage/www/rdf/streamed-graph.js 1 // // from /my/site/homepage/www/rdf/streamed-graph.js
2 2
3 // import { eachJsonLdQuad } from "./json_ld_quads"; 3 // import { eachJsonLdQuad } from "./json_ld_quads";
4 // import { N3Store } from "n3"; 4 // import { N3Store } from "n3";
5 // import { Store } from "n3"; 5 import { Store } from "n3";
6 6
7 // export class StreamedGraphClient { 7 export class StreamedGraphClient {
8 // // holds a n3 Store, which is synced to a server-side 8 // // holds a n3 Store, which is synced to a server-side
9 // // store that sends patches over SSE 9 // // store that sends patches over SSE
10 10
11 // onStatus: (msg: string) => void; 11 onStatus: (msg: string) => void = function(m){};
12 // onGraphChanged: () => void; 12 onGraphChanged: () => void = function(){};
13 // store: N3Store; 13 store: Store;
14 // _deletedCount: number = 0; 14 // _deletedCount: number = 0;
15 // events!: EventSource; 15 // events!: EventSource;
16 // constructor( 16 constructor(
17 // eventsUrl: string, 17 eventsUrl: string,
18 // onGraphChanged: () => void, 18 onGraphChanged: () => void,
19 // onStatus: (status: string) => void, 19 onStatus: (status: string) => void,
20 // prefixes: Array<Record<string, string>>, 20 prefixes: Array<Record<string, string>>,
21 // staticGraphUrls: Array<string> 21 staticGraphUrls: Array<string>
22 // ) { 22 ) {
23 // console.log("new StreamedGraph", eventsUrl); 23 console.log("new StreamedGraph", eventsUrl);
24 // this.onStatus = onStatus; 24 this.onStatus = onStatus;
25 // this.onGraphChanged = onGraphChanged; 25 this.onGraphChanged = onGraphChanged;
26 // this.onStatus("startup..."); 26 this.onStatus("startup...");
27 27
28 // this.store = new Store(); 28 this.store = new Store();
29 29
30 // // // Object.keys(prefixes).forEach((prefix) => { 30 // // // Object.keys(prefixes).forEach((prefix) => {
31 // // // this.store.setPrefix(prefix, prefixes[prefix]); 31 // // // this.store.setPrefix(prefix, prefixes[prefix]);
32 // // // }); 32 // // // });
33 33
34 // this.connect(eventsUrl); 34 this.connect(eventsUrl);
35 // this.reconnectOnWake(); 35 // this.reconnectOnWake();
36 36
37 // // staticGraphUrls.forEach((url) => { 37 // // staticGraphUrls.forEach((url) => {
38 // // fetch(url).then((response) => response.text()) 38 // // fetch(url).then((response) => response.text())
39 // // .then((body) => { 39 // // .then((body) => {
40 // // // parse with n3, add to output 40 // // // parse with n3, add to output
41 // // }); 41 // // });
42 // // }); 42 // // });
43 // } 43 }
44 44
45 // _vacuum() { 45 // _vacuum() {
46 // // workaround for the growing _ids map 46 // // workaround for the growing _ids map
47 // this.store = new Store(this.store.getQuads(null, null, null, null)); 47 // this.store = new Store(this.store.getQuads(null, null, null, null));
48 // } 48 // }
50 // reconnectOnWake() { 50 // reconnectOnWake() {
51 // // 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 51 // // 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
52 // //window.addEventListener('focus', function() { this.connect(eventsUrl); }.bind(this)); 52 // //window.addEventListener('focus', function() { this.connect(eventsUrl); }.bind(this));
53 // } 53 // }
54 54
55 // connect(eventsUrl: string) { 55 connect(eventsUrl: string) {
56 // // need to exit here if this obj has been replaced 56 // // need to exit here if this obj has been replaced
57 57
58 // this.onStatus("start connect..."); 58 // this.onStatus("start connect...");
59 // this.close(); 59 // this.close();
60 // if (this.events && this.events.readyState != EventSource.CLOSED) { 60 // if (this.events && this.events.readyState != EventSource.CLOSED) {
86 // this.onStatus("sync- updating"); 86 // this.onStatus("sync- updating");
87 // await this.patchGraph((ev as MessageEvent).data); 87 // await this.patchGraph((ev as MessageEvent).data);
88 // this.onStatus(`synced ${this.store.size}`); 88 // this.onStatus(`synced ${this.store.size}`);
89 // this.onGraphChanged(); 89 // this.onGraphChanged();
90 // }); 90 // });
91 // this.onStatus("connecting..."); 91 this.onStatus("connecting...");
92 // } 92 }
93 93
94 // // these need some locks 94 // // these need some locks
95 // async replaceFullGraph(jsonLdText: string) { 95 // async replaceFullGraph(jsonLdText: string) {
96 // this.store = new Store(); 96 // this.store = new Store();
97 // await eachJsonLdQuad( 97 // await eachJsonLdQuad(
113 // this._vacuum(); 113 // this._vacuum();
114 // this._deletedCount = 0; 114 // this._deletedCount = 0;
115 // } 115 // }
116 // } 116 // }
117 117
118 // close() { 118 close() {
119 // if (this.events) { 119 // if (this.events) {
120 // this.events.close(); 120 // this.events.close();
121 // } 121 // }
122 // } 122 }
123 123
124 // async testEventUrl(eventsUrl: string): Promise<void> { 124 // async testEventUrl(eventsUrl: string): Promise<void> {
125 // return new Promise<void>((resolve, reject) => { 125 // return new Promise<void>((resolve, reject) => {
126 // this.onStatus("testing connection"); 126 // this.onStatus("testing connection");
127 // fetch(eventsUrl, { 127 // fetch(eventsUrl, {
137 // }) 137 // })
138 // .catch(err => { 138 // .catch(err => {
139 // reject(); 139 // reject();
140 // }); 140 // });
141 // }); 141 // });
142 // } 142 // }
143 // } 143 }