-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathquery-subscription.ts
More file actions
347 lines (301 loc) · 10.5 KB
/
Copy pathquery-subscription.ts
File metadata and controls
347 lines (301 loc) · 10.5 KB
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import DatabaseStore from '../stores/database-store';
import { QueryRange } from './query-range';
import { MutableQueryResultSet } from './mutable-query-result-set';
import ModelQuery from './query';
import { Model } from './model';
import { DatabaseChangeRecord } from '../stores/database-change-record';
import { QueryResultSet } from './query-result-set';
type QuerySubscriptionResult<T extends Model> = QueryResultSet<T> | number | T | T[];
type QuerySubscriptionCallback<T extends Model> = (result: QuerySubscriptionResult<T>) => void;
export class QuerySubscription<T extends Model> {
_set: MutableQueryResultSet<T> = null;
_callbacks: QuerySubscriptionCallback<T>[] = [];
_lastResult: QuerySubscriptionResult<T> | undefined = undefined; // null is a valid result!
_updateInFlight = false;
_queuedChangeRecords = [];
_queryVersion = 1;
_query: ModelQuery<T> | ModelQuery<T[]>;
_options: any;
constructor(
query: ModelQuery<T> | ModelQuery<T[]>,
options: {
initialModels?: T[];
emitResultSet?: boolean;
updateOnSeparateThread?: boolean;
} = {}
) {
this._query = query;
this._options = options;
if (this._query) {
if (this._query._count) {
throw new Error('QuerySubscription::constructor - You cannot listen to count queries.');
}
this._query.finalize();
if (this._options.initialModels) {
this._set = new MutableQueryResultSet();
this._set.addModelsInRange(
this._options.initialModels,
new QueryRange({
limit: this._options.initialModels.length,
offset: 0,
})
);
this._createResultAndTrigger();
} else {
this.update();
}
}
}
query = () => {
return this._query;
};
addCallback = (callback: QuerySubscriptionCallback<T>) => {
if (!(callback instanceof Function)) {
throw new Error(`QuerySubscription:addCallback - expects a function, received ${callback}`);
}
this._callbacks.push(callback);
if (this._lastResult !== undefined) {
callback(this._lastResult);
}
};
hasCallback = (callback: QuerySubscriptionCallback<T>) => {
return this._callbacks.indexOf(callback) !== -1;
};
removeCallback(callback: QuerySubscriptionCallback<T>) {
if (!(callback instanceof Function)) {
throw new Error(
`QuerySubscription:removeCallback - expects a function, received ${callback}`
);
}
this._callbacks = this._callbacks.filter((c) => c !== callback);
if (this.callbackCount() === 0) {
this.onLastCallbackRemoved();
}
}
onLastCallbackRemoved() {}
callbackCount = () => {
return this._callbacks.length;
};
applyChangeRecord = (record: DatabaseChangeRecord<Model>) => {
if (!this._query || record.objectClass !== this._query.objectClass()) {
return;
}
if (record.objects.length === 0) {
return;
}
this._queuedChangeRecords.push(record);
if (!this._updateInFlight) {
this._processChangeRecords();
}
};
cancelPendingUpdate = () => {
this._queryVersion += 1;
this._updateInFlight = false;
};
// Scan through change records and apply them to the last result set.
_processChangeRecords = () => {
if (this._queuedChangeRecords.length === 0) {
return;
}
if (!this._set) {
this.update();
return;
}
let knownImpacts = 0;
let unknownImpacts = 0;
this._queuedChangeRecords.forEach((record) => {
if (record.type === 'unpersist') {
for (const item of record.objects) {
const offset = this._set.offsetOfId(item.id);
if (offset !== -1) {
this._set.removeModelAtOffset(item, offset);
unknownImpacts += 1;
}
}
} else if (record.type === 'persist') {
for (const item of record.objects) {
const offset = this._set.offsetOfId(item.id);
const itemIsInSet = offset !== -1;
const itemShouldBeInSet = item.matches(this._query.matchers());
if (itemIsInSet && !itemShouldBeInSet) {
this._set.removeModelAtOffset(item, offset);
unknownImpacts += 1;
} else if (itemShouldBeInSet && !itemIsInSet) {
this._set.updateModel(item);
unknownImpacts += 1;
} else if (itemIsInSet) {
const oldItem = this._set.modelWithId(item.id);
this._set.updateModel(item);
if (this._itemSortOrderHasChanged(oldItem, item)) {
unknownImpacts += 1;
} else {
knownImpacts += 1;
}
}
}
// If we're not at the top of the result set, we can't be sure whether an
// item previously matched the set and doesn't anymore, impacting the items
// in the query range. We need to refetch IDs to be sure our set === correct.
if (
this._query.range().offset > 0 &&
unknownImpacts + knownImpacts < record.objects.length
) {
unknownImpacts += 1;
}
}
});
this._queuedChangeRecords = [];
if (unknownImpacts > 0) {
this.update({ mustRefetchEntireRange: true });
} else if (knownImpacts > 0) {
this._createResultAndTrigger();
}
};
_itemSortOrderHasChanged(old: T, updated: T) {
if (!old || !updated) return true;
for (const descriptor of this._query.orderSortDescriptors()) {
const oldSortValue = old[descriptor.attr.modelKey];
const updatedSortValue = updated[descriptor.attr.modelKey];
// http://stackoverflow.com/questions/4587060/determining-date-equality-in-javascript
if (!(oldSortValue >= updatedSortValue && oldSortValue <= updatedSortValue)) {
return true;
}
}
return false;
}
update({ mustRefetchEntireRange }: { mustRefetchEntireRange?: boolean } = {}) {
this._updateInFlight = true;
const desiredRange = this._query.range();
const currentRange = this._set ? this._set.range() : null;
const hasNonInfiniteRange =
currentRange && !currentRange.isInfinite() && !desiredRange.isInfinite();
// If we have a limited range, and changes don't require that we refetch
// the entire range, just fetch the missing items. This is the path typically
// used while scrolling.
if (hasNonInfiniteRange && !mustRefetchEntireRange) {
const missingRange = this._getMissingRange(desiredRange, currentRange);
this._fetchRange(missingRange, {
version: this._queryVersion,
fetchEntireModels: true,
});
} else {
const haveNoModels = !this._set || this._set.modelCacheCount() === 0;
this._fetchRange(desiredRange, {
version: this._queryVersion,
fetchEntireModels: haveNoModels,
});
}
}
_getMissingRange = (desiredRange: QueryRange, currentRange: QueryRange) => {
if (currentRange && !currentRange.isInfinite() && !desiredRange.isInfinite()) {
const ranges = QueryRange.rangesBySubtracting(desiredRange, currentRange);
return ranges.length === 1 ? ranges[0] : desiredRange;
}
return desiredRange;
};
_getQueryForRange = (range: QueryRange, fetchEntireModels: boolean) => {
let rangeQuery = null;
if (!range.isInfinite()) {
rangeQuery = rangeQuery || this._query.clone();
rangeQuery.offset(range.offset).limit(range.limit);
}
if (!fetchEntireModels) {
rangeQuery = rangeQuery || this._query.clone();
rangeQuery.idsOnly();
}
rangeQuery = rangeQuery || this._query;
return rangeQuery;
};
_fetchRange(
range: QueryRange,
{ version, fetchEntireModels }: { version: number; fetchEntireModels: boolean }
) {
const rangeQuery = this._getQueryForRange(range, fetchEntireModels);
const haveModels = this._set && this._set.modelCacheCount() > 0;
if (haveModels && this._options.updateOnSeparateThread) {
rangeQuery.background();
}
DatabaseStore.run<T[] | string[]>(rangeQuery, { format: false }).then(async (results) => {
if (this._queryVersion !== version) {
return;
}
if (this._set && !this._set.range().isContiguousWith(range)) {
this._set = null;
}
this._set = this._set || new MutableQueryResultSet();
if (fetchEntireModels) {
this._set.addModelsInRange(results as T[], range);
} else {
this._set.addIdsInRange(results as string[], range);
}
this._set.clipToRange(this._query.range());
// todo: this is returning fewer objects because they're being deleted immediately after being saved
const models = await this._fetchMissingModels();
if (this._queryVersion !== version) {
return;
}
for (const m of models) {
this._set.updateModel(m);
}
this._createResultAndTrigger();
});
}
async _fetchMissingModels() {
const missingIds = this._set.ids().filter((id) => !this._set.modelWithId(id));
if (missingIds.length === 0) {
return [];
}
return DatabaseStore.findAll<T>(this._query._klass, { id: missingIds });
}
optimisticallyRemoveItemsById = (ids: string[]) => {
if (!this._set) {
return;
}
let removed = 0;
for (const id of ids) {
const offset = this._set.offsetOfId(id);
if (offset !== -1) {
this._set.removeModelAtOffset({ id } as T, offset);
removed += 1;
}
}
if (removed > 0) {
this._createResultAndTrigger();
}
};
_createResultAndTrigger() {
const allCompleteModels = this._set.isComplete();
const d = {};
const a = this._set.ids();
for (const ai of a) d[ai] = 1;
const allUniqueIds = Object.keys(d).length === a.length;
let error = null;
if (!allCompleteModels) {
error = new Error('QuerySubscription: Applied all changes and result set is missing models.');
}
if (!allUniqueIds) {
error = new Error(
'QuerySubscription: Applied all changes and result set contains duplicate IDs.'
);
}
if (error) {
console.warn(error);
// this._set = null;
// this.update();
// return;
}
if (this._options.emitResultSet) {
this._set.setQuery(this._query);
this._lastResult = this._set.immutableClone();
} else {
const models = this._set.models();
this._lastResult = this._query.formatResult(models) as QuerySubscriptionResult<T>;
}
this._callbacks.forEach((callback) => callback(this._lastResult));
// process any additional change records that have arrived
if (this._updateInFlight) {
this._updateInFlight = false;
this._processChangeRecords();
}
}
}