-
Notifications
You must be signed in to change notification settings - Fork 456
/
query.js
221 lines (189 loc) · 7.28 KB
/
query.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
var emitter = require('./emitter');
// Queries are live requests to the database for particular sets of fields.
//
// The server actively tells the client when there's new data that matches
// a set of conditions.
var Query = exports.Query = function(type, connection, id, collection, query, options, callback) {
emitter.EventEmitter.call(this);
// 'fetch' or 'sub'
this.type = type;
this.connection = connection;
this.id = id;
this.collection = collection;
// The query itself. For mongo, this should look something like {"data.x":5}
this.query = query;
// Resultant document action for the server. Fetch mode will automatically
// fetch all results. Subscribe mode will automatically subscribe all
// results. Results are never unsubscribed.
this.docMode = options.docMode; // undefined, 'fetch' or 'sub'.
if (this.docMode === 'subscribe') this.docMode = 'sub';
// Do we repoll the entire query whenever anything changes? (As opposed to
// just polling the changed item). This needs to be enabled to be able to use
// ordered queries (sortby:) and paginated queries. Set to undefined, it will
// be enabled / disabled automatically based on the query's properties.
this.poll = options.poll;
// The backend we actually hit. If this isn't defined, it hits the snapshot
// database. Otherwise this can be used to hit another configured query
// index.
this.backend = options.backend || options.source;
// A list of resulting documents. These are actual documents, complete with
// data and all the rest. If fetch is false, these documents will not
// have any data. You should manually call fetch() or subscribe() on them.
//
// Calling subscribe() might be a good idea anyway, as you won't be
// subscribed to the documents by default.
this.knownDocs = options.knownDocs || [];
this.results = [];
// Do we have some initial data?
this.ready = false;
this.callback = callback;
};
emitter.mixin(Query);
Query.prototype.action = 'qsub';
// Helper for subscribe & fetch, since they share the same message format.
//
// This function actually issues the query.
Query.prototype._execute = function() {
if (!this.connection.canSend) return;
if (this.docMode) {
var collectionVersions = {};
// Collect the version of all the documents in the current result set so we
// don't need to be sent their snapshots again.
for (var i = 0; i < this.knownDocs.length; i++) {
var doc = this.knownDocs[i];
var c = collectionVersions[doc.collection] =
(collectionVersions[doc.collection] || {});
c[doc.name] = doc.version;
}
}
var msg = {
a: 'q' + this.type,
id: this.id,
c: this.collection,
o: {},
q: this.query,
};
if (this.docMode) {
msg.o.m = this.docMode;
// This should be omitted if empty, but whatever.
msg.o.vs = collectionVersions;
}
if (this.backend != null) msg.o.b = this.backend;
if (this.poll !== undefined) msg.o.p = this.poll;
this.connection.send(msg);
};
// Make a list of documents from the list of server-returned data objects
Query.prototype._dataToDocs = function(data) {
var results = [];
var lastType;
for (var i = 0; i < data.length; i++) {
var docData = data[i];
// Types are only put in for the first result in the set and every time the type changes in the list.
if (docData.type) {
lastType = docData.type;
} else {
docData.type = lastType;
}
var doc = this.connection.get(docData.c || this.collection, docData.d, docData);
if (this.docMode === 'sub') {
doc._finishQuerySubscribe(docData.v);
}
results.push(doc);
}
return results;
};
// Destroy the query object. Any subsequent messages for the query will be
// ignored by the connection. You should unsubscribe from the query before
// destroying it.
Query.prototype.destroy = function() {
if (this.connection.canSend && this.type === 'sub') {
this.connection.send({a:'qunsub', id:this.id});
}
this.connection._destroyQuery(this);
};
Query.prototype._onConnectionStateChanged = function(state, reason) {
if (this.connection.state === 'connecting') {
this._execute();
}
};
// Internal method called from connection to pass server messages to the query.
Query.prototype._onMessage = function(msg) {
if ((msg.a === 'qfetch') !== (this.type === 'fetch')) {
if (console) console.warn('Invalid message sent to query', msg, this);
return;
}
if (msg.error) this.emit('error', msg.error);
switch (msg.a) {
case 'qfetch':
var results = msg.data ? this._dataToDocs(msg.data) : undefined;
if (this.callback) this.callback(msg.error, results, msg.extra);
// Once a fetch query gets its data, it is destroyed.
this.connection._destroyQuery(this);
break;
case 'q':
// Query diff data (inserts and removes)
if (msg.diff) {
// We need to go through the list twice. First, we'll ingest all the
// new documents and set them as subscribed. After that we'll emit
// events and actually update our list. This avoids race conditions
// around setting documents to be subscribed & unsubscribing documents
// in event callbacks.
for (var i = 0; i < msg.diff.length; i++) {
var d = msg.diff[i];
if (d.type === 'insert') d.values = this._dataToDocs(d.values);
}
for (var i = 0; i < msg.diff.length; i++) {
var d = msg.diff[i];
switch (d.type) {
case 'insert':
var newDocs = d.values;
Array.prototype.splice.apply(this.results, [d.index, 0].concat(newDocs));
this.emit('insert', newDocs, d.index);
break;
case 'remove':
var howMany = d.howMany || 1;
var removed = this.results.splice(d.index, howMany);
this.emit('remove', removed, d.index);
break;
case 'move':
var howMany = d.howMany || 1;
var docs = this.results.splice(d.from, howMany);
Array.prototype.splice.apply(this.results, [d.to, 0].concat(docs));
this.emit('move', docs, d.from, d.to);
break;
}
}
}
if (msg.extra !== void 0) {
this.emit('extra', msg.extra);
}
break;
case 'qsub':
// This message replaces the entire result set with the set passed.
if (!msg.error) {
var previous = this.results;
// Then add everything in the new result set.
this.results = this.knownDocs = this._dataToDocs(msg.data);
this.extra = msg.extra;
this.ready = true;
this.emit('change', this.results, previous);
}
if (this.callback) {
this.callback(msg.error, this.results, this.extra);
delete this.callback;
}
break;
}
};
// Change the thing we're searching for. This isn't fully supported on the
// backend (it destroys the old query and makes a new one) - but its
// programatically useful and I might add backend support at some point.
Query.prototype.setQuery = function(q) {
if (this.type !== 'sub') throw new Error('cannot change a fetch query');
this.query = q;
if (this.connection.canSend) {
// There's no 'change' message to send to the server. Just resubscribe.
this.connection.send({a:'qunsub', id:this.id});
this._execute();
}
};