comparison src/streamed_graph_client.ts @ 5:8aa42fa04b17

enable more code
author drewp@bigasterisk.com
date Wed, 04 Dec 2019 00:32:24 -0800
parents a668a774b162
children a5bc13dcce41
comparison
equal deleted inserted replaced
4:a668a774b162 5:8aa42fa04b17
19 onGraphChanged: () => void, 19 onGraphChanged: () => void,
20 onStatus: (status: string) => void, 20 onStatus: (status: string) => void,
21 prefixes: Array<Record<string, string>>, 21 prefixes: Array<Record<string, string>>,
22 staticGraphUrls: Array<string>) { 22 staticGraphUrls: Array<string>) {
23 console.log('new StreamedGraph', eventsUrl); 23 console.log('new StreamedGraph', eventsUrl);
24 // holds a rdfstore.js store, which is synced to a server-side 24 // holds a n3 Store, which is synced to a server-side
25 // // store that sends patches over SSE 25 // store that sends patches over SSE
26 // this.onStatus = onStatus; 26 this.onStatus = onStatus;
27 // this.onGraphChanged = onGraphChanged; 27 this.onGraphChanged = onGraphChanged;
28 // this.onStatus('startup...'); 28 this.onStatus('startup...');
29 29
30 this.store = new Store({}); 30 this.store = new Store({});
31 31
32 // // Object.keys(prefixes).forEach((prefix) => { 32 // // Object.keys(prefixes).forEach((prefix) => {
33 // // this.store.setPrefix(prefix, prefixes[prefix]); 33 // // this.store.setPrefix(prefix, prefixes[prefix]);
34 // // }); 34 // // });
35 35
36 this.connect(eventsUrl); 36 this.connect(eventsUrl);
37 this.reconnectOnWake(); 37 this.reconnectOnWake();
38 38
39 // staticGraphUrls.forEach((url) => { 39 // staticGraphUrls.forEach((url) => {
40 // fetch(url).then((response) => response.text()) 40 // fetch(url).then((response) => response.text())
41 // .then((body) => { 41 // .then((body) => {
42 // // parse with n3, add to output 42 // // parse with n3, add to output
43 // }); 43 // });
44 // }); 44 // });
45 45
46 } 46 }
47 47
48 reconnectOnWake() { 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 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)); 50 //window.addEventListener('focus', function() { this.connect(eventsUrl); }.bind(this));
51
52 } 51 }
53 52
54 connect(eventsUrl: string) { 53 connect(eventsUrl: string) {
55 // need to exit here if this obj has been replaced 54 // need to exit here if this obj has been replaced
56 55
57 this.onStatus('start connect...'); 56 this.onStatus('start connect...');
58 // this.close(); 57 this.close();
59 if (this.events && this.events.readyState != EventSource.CLOSED) { 58 if (this.events && this.events.readyState != EventSource.CLOSED) {
60 this.onStatus('zombie'); 59 this.onStatus('zombie');
61 throw new Error("zombie eventsource"); 60 throw new Error("zombie eventsource");
62 } 61 }
63 62
74 }); 73 });
75 }, 3000); 74 }, 3000);
76 }); 75 });
77 76
78 this.events.addEventListener('fullGraph', (ev) => { 77 this.events.addEventListener('fullGraph', (ev) => {
79 // this.updates.push({ type: 'fullGraph', data: ev.data }); 78 this.onStatus('sync- full graph update');
80 // this.flushUpdates(); 79 let onReplaced = () => {
80 this.onStatus('synced');
81 this.onGraphChanged();
82 };
83 this.replaceFullGraph(ev.data, onReplaced);
81 }); 84 });
82 85
83 this.events.addEventListener('patch', (ev) => { 86 this.events.addEventListener('patch', (ev) => {
84 // this.updates.push({ type: 'patch', data: ev.data }); 87 this.onStatus('sync- updating');
85 // this.flushUpdates(); 88 let onPatched = () => {
89 this.onStatus('synced');
90 this.onGraphChanged();
91 };
92 this.patchGraph(ev.data, onPatched);
86 }); 93 });
87 this.onStatus('connecting...'); 94 this.onStatus('connecting...');
88 } 95 }
89 96
90 // replaceFullGraph(jsonLdText: string, done: () => void) { 97 replaceFullGraph(jsonLdText: string, done: () => void) {
91 // // this.quadStore.clear(); 98 // this.quadStore.clear();
92 // // eachJsonLdQuad(this.store.rdf, JSON.parse(jsonLdText), 99 // eachJsonLdQuad(this.store.rdf, JSON.parse(jsonLdText),
93 // // this.quadStore.add.bind(this.quadStore), function () { 100 // this.quadStore.add.bind(this.quadStore), function () {
94 // // done(); 101 // done();
95 // // }); 102 // });
96 // // or this.store.insert([quad], quad.graph, function() {}); 103 // or this.store.insert([quad], quad.graph, function() {});
97 // } 104 }
98 105
106 patchGraph(patchJson: string, done: () => void) {
107 var patch = JSON.parse(patchJson).patch;
99 108
100 // patchGraph(patchJson: string, done: () => void) { 109 // if (!this.store) {
101 // var patch = JSON.parse(patchJson).patch; 110 // throw new Error('store ' + this.store);
111 // }
102 112
103 // // if (!this.store) { 113 async.series([
104 // // throw new Error('store ' + this.store); 114 // (done) => {
105 // // } 115 // eachJsonLdQuad(this.store.rdf, patch.deletes,
116 // this.quadStore.remove.bind(this.quadStore), done);
117 // },
118 (done) => {
119 // eachJsonLdQuad(this.store.rdf, patch.adds,
120 // this.quadStore.add.bind(this.quadStore), done);
121 },
122 /* seriesDone */ (done) => {
123 done();
124 }
125 ], done);
126 }
106 127
107 // async.series([ 128 close() {
108 // // (done) => { 129 if (this.events) {
109 // // eachJsonLdQuad(this.store.rdf, patch.deletes, 130 this.events.close();
110 // // this.quadStore.remove.bind(this.quadStore), done); 131 }
111 // // }, 132 }
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 133
127 // testEventUrl(eventsUrl: string): Promise<void> { 134 testEventUrl(eventsUrl: string): Promise<void> {
128 // return new Promise<void>((resolve, reject) => { 135 return new Promise<void>((resolve, reject) => {
129 // this.onStatus('testing connection'); 136 this.onStatus('testing connection');
130 // fetch(eventsUrl, { 137 fetch(eventsUrl, {
131 // method: "HEAD", 138 method: "HEAD",
132 // credentials: "include", 139 credentials: "include",
133 // }).then((value) => { 140 }).then((value) => {
134 // if (value.status == 403) { 141 if (value.status == 403) {
135 // reject(); 142 reject();
136 // return; 143 return;
137 // } 144 }
138 // resolve(); 145 resolve();
139 // }).catch((err) => { 146 }).catch((err) => {
140 // reject(); 147 reject();
141 // }); 148 });
142 // }); 149 });
143 // } 150 }
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 151
168 } 152 }