-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathQueryRequest.js
45 lines (43 loc) · 1.54 KB
/
QueryRequest.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
import {normalizeQueryEncoding} from './util';
// An QueryRequest is used to represent a RethinkDB query that a component is
// subscribed to. It should be used for values in the object returned from the
// observe() function of components that use the provided mixin. See Mixin.js
// for the API.
//
// QueryRequests have have 3 required properties.
// * query: the RethinkDB query to run
// * changes: boolean specifying whether to subscribe to the realtime
// changefeed too
// * initial: value to be returned before the query finishes loading
// * transform: function applied after the query finishes loading
//
// Here is a simple example of an QueryRequest that a component might use in
// its observe() function:
// new QueryRequest({
// query: r.table('turtles'),
// changes: true,
// initial: [],
// transform: (rows) => { // array-to-object, using id as key for O(1) lookup
// rows.reduce((prev, cur) => {
// prev[cur.id] = cur
// prev
// }, {})
// }
// })
export class QueryRequest {
constructor({query, changes, initial, transform}) {
this.query = normalizeQueryEncoding(query);
this.changes = changes;
this.initial = initial;
this.transform = transform;
}
// Convert the QueryRequest into a string that can be used as a
// deterministic lookup key in an object. The key should be identical for two
// QueryRequests that need access to the same data.
toStringKey() {
return JSON.stringify({
query: this.query.build(),
changes: this.changes,
});
}
}