-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmemory.spimedb.js
124 lines (97 loc) · 3.31 KB
/
memory.spimedb.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
class SpimeDBMemory extends Memory {
constructor(url) {
super('spimedb:' + url);
this.url = url;
}
start(me) {
me.info([ 'start', this.I ]);
this.me = me;
//http://localhost:8080/facet?q=%3E
const that = this;
$.getJSON(this.url + '/facet', {d: '>'}, (obj)=>{
//const xx = _.map(x, zz => { I: zz[0] });
if (obj && obj.length > 0) {
for (var i = 0; i < obj.length; i++) {
const x = obj[i];
if (x && x.length > 0) {
if (x && x.length > 0) {
me.put({
I: x
}, that);
}
}
}
}
});
}
get(q, each) {
const that = this;
const _each = each;
each = (e) => {
_each(e);
//HACK intercept and save in memory locally
that.me.put(e);
};
$.getJSON(this.url + '/find', {q: q}, (results)=>{
if (results && results.length > 0) {
const obj = results[0];
for (var i = 0; i < obj.length; i++) {
const x = obj[i];
if (x && x.length > 0) {
each({
I: x
});
}
}
}
const facets = results[1];
if (facets && facets.length > 0) {
_.forEach(_.map(facets, g => g[0]), f => { each({
I: f
/* via: ... */
}); } );
}
});
}
stop() {
}
}
function SpimeSocket(path, add) {
const defaultHostname = window.location.hostname || 'localhost';
const defaultWSPort = window.location.port || 8080;
const options = undefined;
/** creates a websocket connection to a path on the server that hosts the currently visible webpage */
const ws = new ReconnectingWebSocket(
'ws://' + defaultHostname + ':' + defaultWSPort + '/' + path,
null /* protocols */,
options); //{
//Options: //https://github.com/joewalnes/reconnecting-websocket/blob/master/reconnecting-websocket.js#L112
/*
// The number of milliseconds to delay before attempting to reconnect.
reconnectInterval: 1000,
// The maximum number of milliseconds to delay a reconnection attempt.
maxReconnectInterval: 30000,
// The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist.
reconnectDecay: 1.5,
// The maximum time in milliseconds to wait for a connection to succeed before closing and retrying.
timeoutInterval: 2000,
*/
//});
ws.binaryType = 'arraybuffer';
ws.onopen = function () {
add('websocket connect');
};
ws.onmessage = m => add(msgpack.decode(new Uint8Array(m.data)));
// ws.onmessage = function (e) {
// try {
// var c = e.data;
// var d = JSON.parse(c);
// add(d);
// } catch (e) {
// add(c);
// }
// };
ws.onclose = e => add(['Websocket disconnected', e]);
ws.onerror = e => add(["Websocket error", e]);
return ws;
}