-
Notifications
You must be signed in to change notification settings - Fork 48
/
QueryState.js
154 lines (144 loc) · 5 KB
/
QueryState.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
import {findIndex, isCursor, updateComponent} from './util.js';
// A QueryState represents the state of a single RethinkDB query, which may be
// shared among multiple components subscribing to the same data. It keeps
// track of the query results, the active cursors, the subscriptions from the
// active QueryRequests, and the loading and error status.
//
// The constructor takes an QueryRequest from the components interested in
// this query, a runQuery function from the Session, an onUpdate handler
// function that is called when any QueryResults/components have updated,
// and an onCloseQueryState handler function that is called when this
// QueryState is closed (when the last component has unsubscribed).
//
// The subscribe method registers a component and its corresponding
// QueryResult so that the component will have access to the query results.
// It returns an object with an unsubscribe() method to be called when the
// component is no longer interested in this query.
export class QueryState {
constructor(queryRequest, runQuery, onUpdate, onCloseQueryState) {
this.value = undefined;
this.loading = true;
this.errors = [];
this.lastSubscriptionId = 0;
this.subscriptions = {};
this.updateHandler = onUpdate;
this.closeHandlers = [onCloseQueryState];
this.queryRequest = queryRequest;
this.runQuery = runQuery;
}
handleConnect() {
if (this.loading || this.queryRequest.changes) {
this.loading = true;
this.errors = [];
this._runQuery(this.queryRequest.query, this.runQuery, this.queryRequest.changes);
}
}
subscribe(component, queryResult) {
this._initQueryResult(queryResult);
const subscriptionId = ++this.lastSubscriptionId;
this.subscriptions[subscriptionId] = {queryResult, component};
const unsubscribe = () => {
delete this.subscriptions[subscriptionId];
if (!Object.keys(this.subscriptions).length) {
this.closeHandlers.forEach(handler => handler());
}
};
return {unsubscribe};
}
_runQuery(query, runQuery, changes = false) {
const changeQuery = query.changes({includeStates: true, includeInitial: true});
const promise = runQuery(changes ? changeQuery : query);
this.closeHandlers.push(() => promise.then(x => isCursor(x) && x.close()));
promise.then(cursor => {
const isFeed = !!cursor.constructor.name.match(/Feed$/);
if (isFeed) {
const isPointFeed = cursor.constructor.name === 'AtomFeed';
this.value = isPointFeed ? undefined : [];
cursor.each((error, row) => {
if (error) {
this._addError(error);
} else {
if (row.state) {
if (row.state === 'ready') {
this.loading = false;
this._updateSubscriptions();
}
} else {
this._applyChangeDelta(row.old_val, row.new_val);
}
}
});
} else {
if (isCursor(cursor)) {
cursor.toArray().then(result => {
this._updateValue(result);
});
} else {
this._updateValue(cursor);
}
}
}, error => {
if (error.msg === 'Unrecognized optional argument `include_initial`.') {
console.error('react-rethinkdb requires rethinkdb >= 2.2 on backend');
}
this._addError(error);
});
}
_initQueryResult(queryResult) {
if (this.loading) {
queryResult._reset();
} else {
queryResult._setValue(this.value);
}
queryResult._setErrors(this.errors);
}
_updateSubscriptions() {
Object.keys(this.subscriptions).forEach(subscriptionId => {
const subscription = this.subscriptions[subscriptionId];
subscription.queryResult._setValue(this.value);
subscription.queryResult._setErrors(this.errors);
updateComponent(subscription.component);
});
this.updateHandler();
}
_addError(error) {
console.error(error.stack || error.message || error);
this.errors.push(error);
this._updateSubscriptions();
}
_updateValue(value) {
this.loading = false;
this.value = value;
this._updateSubscriptions();
}
_applyChangeDelta(oldVal, newVal) {
if (Array.isArray(this.value)) {
// TODO Make more efficient, with O(1) hashtables with cached
// JSON.stringify keys. But this may not be necessary after RethinkDB
// #3714 is implemented, since the server should give us the indices.
let oldIndex = -1;
if (oldVal) {
const lookup = JSON.stringify(oldVal);
oldIndex = findIndex(this.value, x => JSON.stringify(x) === lookup);
}
if (oldIndex < 0) {
if (newVal) {
this.value.push(newVal);
} else {
throw new Error('Change delta deleted nonexistent element');
}
} else {
if (newVal) {
this.value[oldIndex] = newVal;
} else {
this.value.splice(oldIndex, 1);
}
}
} else {
this.value = newVal;
}
if (!this.loading) {
this._updateSubscriptions();
}
}
}