@airtable/blocks 1.9.0-experimental-5565d56-20211029 → 1.10.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/CHANGELOG.md +15 -4
  2. package/dist/cjs/error_utils.js +0 -17
  3. package/dist/cjs/models/grouped_record_query_result.js +14 -5
  4. package/dist/cjs/models/linked_records_query_result.js +27 -75
  5. package/dist/cjs/models/mutations.js +162 -18
  6. package/dist/cjs/models/record.js +56 -309
  7. package/dist/cjs/models/record_query_result.js +1 -4
  8. package/dist/cjs/models/record_store.js +765 -554
  9. package/dist/cjs/models/table.js +7 -7
  10. package/dist/cjs/models/table_or_view_query_result.js +414 -480
  11. package/dist/cjs/models/view.js +1 -1
  12. package/dist/cjs/models/view_data_store.js +295 -243
  13. package/dist/cjs/private_utils.js +0 -40
  14. package/dist/cjs/sdk.js +2 -12
  15. package/dist/cjs/testing/abstract_mock_airtable_interface.js +10 -55
  16. package/dist/cjs/ui/use_records.js +1 -5
  17. package/dist/cjs/watchable.js +1 -15
  18. package/dist/types/src/models/grouped_record_query_result.d.ts +3 -3
  19. package/dist/types/src/models/grouped_record_query_result.d.ts.map +1 -1
  20. package/dist/types/src/models/linked_records_query_result.d.ts.map +1 -1
  21. package/dist/types/src/models/mutations.d.ts.map +1 -1
  22. package/dist/types/src/models/record.d.ts +4 -13
  23. package/dist/types/src/models/record.d.ts.map +1 -1
  24. package/dist/types/src/models/record_query_result.d.ts +2 -3
  25. package/dist/types/src/models/record_query_result.d.ts.map +1 -1
  26. package/dist/types/src/models/record_store.d.ts.map +1 -1
  27. package/dist/types/src/models/table.d.ts +1 -1
  28. package/dist/types/src/models/table_or_view_query_result.d.ts +5 -3
  29. package/dist/types/src/models/table_or_view_query_result.d.ts.map +1 -1
  30. package/dist/types/src/models/view.d.ts +1 -1
  31. package/dist/types/src/models/view_data_store.d.ts +1 -0
  32. package/dist/types/src/models/view_data_store.d.ts.map +1 -1
  33. package/dist/types/src/models/view_metadata_query_result.d.ts +1 -1
  34. package/dist/types/src/models/view_metadata_query_result.d.ts.map +1 -1
  35. package/dist/types/src/private_utils.d.ts +3 -26
  36. package/dist/types/src/private_utils.d.ts.map +1 -1
  37. package/dist/types/src/sdk.d.ts.map +1 -1
  38. package/dist/types/src/testing/abstract_mock_airtable_interface.d.ts +11 -11
  39. package/dist/types/src/testing/abstract_mock_airtable_interface.d.ts.map +1 -1
  40. package/dist/types/src/types/airtable_interface.d.ts +19 -43
  41. package/dist/types/src/types/airtable_interface.d.ts.map +1 -1
  42. package/dist/types/src/types/table.d.ts +2 -0
  43. package/dist/types/src/types/table.d.ts.map +1 -1
  44. package/dist/types/src/types/view.d.ts +8 -3
  45. package/dist/types/src/types/view.d.ts.map +1 -1
  46. package/dist/types/src/watchable.d.ts.map +1 -1
  47. package/dist/types/test/airtable_interface_mocks/fixture_data.d.ts +5 -7
  48. package/dist/types/test/airtable_interface_mocks/fixture_data.d.ts.map +1 -1
  49. package/dist/types/test/airtable_interface_mocks/mock_airtable_interface.d.ts +11 -18
  50. package/dist/types/test/airtable_interface_mocks/mock_airtable_interface.d.ts.map +1 -1
  51. package/dist/types/test/test_helpers.d.ts +0 -2
  52. package/dist/types/test/test_helpers.d.ts.map +1 -1
  53. package/package.json +2 -3
  54. package/dist/cjs/models/query_manager.js +0 -327
  55. package/dist/cjs/types/block_query_spec.js +0 -85
  56. package/dist/types/src/models/query_manager.d.ts +0 -2
  57. package/dist/types/src/models/query_manager.d.ts.map +0 -1
  58. package/dist/types/src/types/block_query_spec.d.ts +0 -139
  59. package/dist/types/src/types/block_query_spec.d.ts.map +0 -1
  60. package/dist/types/test/airtable_interface_mocks/mock_base_data_stores.d.ts +0 -51
  61. package/dist/types/test/airtable_interface_mocks/mock_base_data_stores.d.ts.map +0 -1
@@ -12,8 +12,6 @@ require("core-js/modules/es.array.concat");
12
12
 
13
13
  require("core-js/modules/es.array.filter");
14
14
 
15
- require("core-js/modules/es.array.includes");
16
-
17
15
  require("core-js/modules/es.array.iterator");
18
16
 
19
17
  require("core-js/modules/es.array.map");
@@ -26,8 +24,6 @@ require("core-js/modules/es.promise");
26
24
 
27
25
  require("core-js/modules/es.set");
28
26
 
29
- require("core-js/modules/es.string.includes");
30
-
31
27
  require("core-js/modules/es.string.starts-with");
32
28
 
33
29
  require("core-js/modules/web.dom-collections.for-each");
@@ -63,13 +59,9 @@ var _private_utils = require("../private_utils");
63
59
 
64
60
  var _error_utils = require("../error_utils");
65
61
 
66
- var _block_query_spec = require("../types/block_query_spec");
67
-
68
62
  var _table = _interopRequireWildcard(require("./table"));
69
63
 
70
- var _view = _interopRequireDefault(require("./view"));
71
-
72
- var _record_query_result = _interopRequireWildcard(require("./record_query_result"));
64
+ var _record_query_result = _interopRequireDefault(require("./record_query_result"));
73
65
 
74
66
  var _record_coloring = require("./record_coloring");
75
67
 
@@ -105,17 +97,6 @@ function (_RecordQueryResult) {
105
97
 
106
98
  /** @internal */
107
99
 
108
- /**
109
- * Tracks a unique ID for each model query that has been loaded.
110
- * Note that hyperbase may return a new model query ID even if the
111
- * query itself is identical to a previous one. Having these unique
112
- * IDs allows the SDK to maintain an accurate reference count of
113
- * all model loads. _loadedSourceModelQueryIds must only be updated
114
- * before unloading occurs.
115
- *
116
- * @internal
117
- */
118
-
119
100
  /** @internal */
120
101
 
121
102
  /** @internal */
@@ -143,6 +124,17 @@ function (_RecordQueryResult) {
143
124
  /** @internal */
144
125
  // lazily generated set of record ids
145
126
 
127
+ /** @internal */
128
+ // NOTE: when a cellValue key (cellValues or cellValuesInField:) is watched, we want
129
+ // to make sure we watch the associated key on the table. However, we need to make
130
+ // sure that we only watch the table once for each key. Otherwise, the callbacks
131
+ // for each key will get called more than once for each change event. This is because
132
+ // Watchable stores references to callbacks for each key, and on each _onChange event
133
+ // calls each callback for that key. If we watch the table more than once, then we'll
134
+ // call _onChange more than once, and each callback will be called more than once, which
135
+ // is undesirable. Instead, we'll store watch counts for each key to make sure we only
136
+ // watch the table once.
137
+
146
138
  /** @internal */
147
139
 
148
140
  /** @internal */
@@ -154,8 +146,7 @@ function (_RecordQueryResult) {
154
146
  (0, _classCallCheck2.default)(this, TableOrViewQueryResult);
155
147
  _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(TableOrViewQueryResult).call(this, sdk, normalizedOpts));
156
148
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_sourceModel", void 0);
157
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_mostRecentSourceModelLoadedPromise", void 0);
158
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_loadedSourceModelQueryIds", void 0);
149
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_mostRecentSourceModelLoadPromise", void 0);
159
150
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_table", void 0);
160
151
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_fieldIdsSetToLoadOrNullIfAllFields", void 0);
161
152
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_visList", void 0);
@@ -165,145 +156,10 @@ function (_RecordQueryResult) {
165
156
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_orderedGroups", void 0);
166
157
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_loadedGroupLevels", void 0);
167
158
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_recordIdsSet", null);
159
+ (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_cellValueKeyWatchCounts", void 0);
168
160
  (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "__groupedRecordQueryResultPool", void 0);
169
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_watchConfigByWatchableKey", {
170
- // Handles if this is a prefix
171
- [_record_query_result.default.WatchableCellValuesInFieldKeyPrefix]: (key, functionToCallWithKey) => {
172
- var fieldId = key.substring(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix.length);
173
-
174
- if (_this._fieldIdsSetToLoadOrNullIfAllFields && !(0, _private_utils.has)(_this._fieldIdsSetToLoadOrNullIfAllFields, fieldId)) {
175
- throw (0, _error_utils.spawnError)("Can't watch or unwatch field because it wasn't included in RecordQueryResult fields: %s", fieldId);
176
- }
177
-
178
- functionToCallWithKey(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix + fieldId);
179
- },
180
- [_record_query_result.default.WatchableKeys.cellValues]: (key, functionToCallWithKey) => {
181
- if (_this._fieldIdsSetToLoadOrNullIfAllFields) {
182
- for (var _i = 0, _Object$keys = Object.keys(_this._fieldIdsSetToLoadOrNullIfAllFields); _i < _Object$keys.length; _i++) {
183
- var fieldId = _Object$keys[_i];
184
- functionToCallWithKey(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix + fieldId);
185
- }
186
- } else {
187
- functionToCallWithKey(_record_query_result.default.WatchableKeys.cellValues);
188
- }
189
- }
190
- });
191
- (0, _defineProperty2.default)((0, _assertThisInitialized2.default)(_this), "_unloadDataAsync", () => {
192
- var viewDataStore, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, sort, field;
193
-
194
- return _regenerator.default.async(function _callee$(_context) {
195
- while (1) {
196
- switch (_context.prev = _context.next) {
197
- case 0:
198
- if (!_this._mostRecentSourceModelLoadedPromise) {
199
- _context.next = 3;
200
- break;
201
- }
202
-
203
- _context.next = 3;
204
- return _regenerator.default.awrap(_this._mostRecentSourceModelLoadedPromise);
205
-
206
- case 3:
207
- // Unload all queries that were subscribed to the entire query
208
- if (_this._sourceModel instanceof _table.default) {
209
- _this._recordStore.unloadCellValuesForQueryIdsThatAreSubscribedToAllCells(_this._loadedSourceModelQueryIds);
210
- } else {
211
- // Even if the view is deleted, we can still unsubscribe from the cells
212
- _this._recordStore.unloadViewDataStoreAndCellValuesWithQueryIds(_this._loadedSourceModelQueryIds);
213
- }
214
-
215
- _this._mostRecentSourceModelLoadedPromise = null;
216
-
217
- if (_this._sourceModel instanceof _table.default) {
218
- _this._recordStore.unwatch(_record_store.WatchableRecordStoreKeys.records, _this._onRecordsChanged, (0, _assertThisInitialized2.default)(_this));
219
- } else {
220
- if (!_this._sourceModel.isDeleted) {
221
- viewDataStore = _this._recordStore.getViewDataStore(_this._sourceModel.id);
222
- viewDataStore.unwatch(_view_data_store.WatchableViewDataStoreKeys.visibleRecords, _this._onRecordsChanged, (0, _assertThisInitialized2.default)(_this));
223
- viewDataStore.unwatch(_view_data_store.WatchableViewDataStoreKeys.groups, _this._onGroupsChanged, (0, _assertThisInitialized2.default)(_this));
224
- viewDataStore.unwatch(_view_data_store.WatchableViewDataStoreKeys.groupLevels, _this._onGroupLevelsChanged, (0, _assertThisInitialized2.default)(_this));
225
- }
226
- }
227
-
228
- _this._recordStore.unwatch(_this._cellValuesForSortWatchKeys, _this._onCellValuesForSortChanged, (0, _assertThisInitialized2.default)(_this));
229
-
230
- _this._recordStore.unwatch(_this._cellValuesForGroupWatchKeys, _this._onCellValuesForGroupChanged, (0, _assertThisInitialized2.default)(_this));
231
-
232
- _this._table.unwatch(_table.WatchableTableKeys.fields, _this._onTableFieldsChanged, (0, _assertThisInitialized2.default)(_this)); // If the table is deleted, can't call getFieldById on it below.
233
-
234
-
235
- if (!(!_this._table.isDeleted && _this._sorts)) {
236
- _context.next = 29;
237
- break;
238
- }
239
-
240
- _iteratorNormalCompletion = true;
241
- _didIteratorError = false;
242
- _iteratorError = undefined;
243
- _context.prev = 13;
244
-
245
- for (_iterator = _this._sorts[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
246
- sort = _step.value;
247
- field = _this._table.getFieldByIdIfExists(sort.fieldId);
248
-
249
- if (field) {
250
- field.unwatch('type', _this._onFieldConfigChanged, (0, _assertThisInitialized2.default)(_this));
251
- field.unwatch('options', _this._onFieldConfigChanged, (0, _assertThisInitialized2.default)(_this));
252
- }
253
- }
254
-
255
- _context.next = 21;
256
- break;
257
-
258
- case 17:
259
- _context.prev = 17;
260
- _context.t0 = _context["catch"](13);
261
- _didIteratorError = true;
262
- _iteratorError = _context.t0;
263
-
264
- case 21:
265
- _context.prev = 21;
266
- _context.prev = 22;
267
-
268
- if (!_iteratorNormalCompletion && _iterator.return != null) {
269
- _iterator.return();
270
- }
271
-
272
- case 24:
273
- _context.prev = 24;
274
-
275
- if (!_didIteratorError) {
276
- _context.next = 27;
277
- break;
278
- }
279
-
280
- throw _iteratorError;
281
-
282
- case 27:
283
- return _context.finish(24);
284
-
285
- case 28:
286
- return _context.finish(21);
287
-
288
- case 29:
289
- _this._unloadOrderedGroupsIfNeeded();
290
-
291
- _this._visList = null;
292
- _this._orderedRecordIds = null;
293
- _this._recordIdsSet = null;
294
-
295
- _this._table.__tableOrViewQueryResultPool.unregisterObjectForReuseStrong((0, _assertThisInitialized2.default)(_this));
296
-
297
- case 34:
298
- case "end":
299
- return _context.stop();
300
- }
301
- }
302
- }, null, null, [[13, 17, 21, 29], [22,, 24, 28]]);
303
- });
304
161
  _this._sourceModel = sourceModel;
305
- _this._mostRecentSourceModelLoadedPromise = null;
306
- _this._loadedSourceModelQueryIds = [];
162
+ _this._mostRecentSourceModelLoadPromise = null;
307
163
  _this._table = normalizedOpts.table;
308
164
  var sorts = _this._normalizedOpts.sorts;
309
165
  _this._sorts = sorts !== null && sorts !== void 0 ? sorts : null; // TODO (SeanKeenan): Placeholder until we support groups from normalizedOpts
@@ -313,57 +169,58 @@ function (_RecordQueryResult) {
313
169
  _this._orderedRecordIds = null;
314
170
  _this._orderedGroups = null;
315
171
  _this._loadedGroupLevels = null;
172
+ _this._cellValueKeyWatchCounts = {};
316
173
  var fieldIdsSetToLoadOrNullIfAllFields = null;
317
174
 
318
175
  if (_this._normalizedOpts.fieldIdsOrNullIfAllFields) {
319
176
  fieldIdsSetToLoadOrNullIfAllFields = {};
320
- var _iteratorNormalCompletion2 = true;
321
- var _didIteratorError2 = false;
322
- var _iteratorError2 = undefined;
177
+ var _iteratorNormalCompletion = true;
178
+ var _didIteratorError = false;
179
+ var _iteratorError = undefined;
323
180
 
324
181
  try {
325
- for (var _iterator2 = _this._normalizedOpts.fieldIdsOrNullIfAllFields[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
326
- var fieldId = _step2.value;
182
+ for (var _iterator = _this._normalizedOpts.fieldIdsOrNullIfAllFields[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
183
+ var fieldId = _step.value;
327
184
  fieldIdsSetToLoadOrNullIfAllFields[fieldId] = true;
328
185
  } // Need to load data for fields we're sorting by, even if
329
186
  // they're not explicitly requested in the `fields` opt.
330
187
 
331
188
  } catch (err) {
332
- _didIteratorError2 = true;
333
- _iteratorError2 = err;
189
+ _didIteratorError = true;
190
+ _iteratorError = err;
334
191
  } finally {
335
192
  try {
336
- if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
337
- _iterator2.return();
193
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
194
+ _iterator.return();
338
195
  }
339
196
  } finally {
340
- if (_didIteratorError2) {
341
- throw _iteratorError2;
197
+ if (_didIteratorError) {
198
+ throw _iteratorError;
342
199
  }
343
200
  }
344
201
  }
345
202
 
346
203
  if (_this._sorts !== null) {
347
- var _iteratorNormalCompletion3 = true;
348
- var _didIteratorError3 = false;
349
- var _iteratorError3 = undefined;
204
+ var _iteratorNormalCompletion2 = true;
205
+ var _didIteratorError2 = false;
206
+ var _iteratorError2 = undefined;
350
207
 
351
208
  try {
352
- for (var _iterator3 = _this._sorts[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
353
- var sort = _step3.value;
209
+ for (var _iterator2 = _this._sorts[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
210
+ var sort = _step2.value;
354
211
  fieldIdsSetToLoadOrNullIfAllFields[sort.fieldId] = true;
355
212
  }
356
213
  } catch (err) {
357
- _didIteratorError3 = true;
358
- _iteratorError3 = err;
214
+ _didIteratorError2 = true;
215
+ _iteratorError2 = err;
359
216
  } finally {
360
217
  try {
361
- if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
362
- _iterator3.return();
218
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
219
+ _iterator2.return();
363
220
  }
364
221
  } finally {
365
- if (_didIteratorError3) {
366
- throw _iteratorError3;
222
+ if (_didIteratorError2) {
223
+ throw _iteratorError2;
367
224
  }
368
225
  }
369
226
  }
@@ -403,26 +260,26 @@ function (_RecordQueryResult) {
403
260
  value: function _getOrGenerateRecordIdsSet() {
404
261
  if (!this._recordIdsSet) {
405
262
  var recordIdsSet = {};
406
- var _iteratorNormalCompletion4 = true;
407
- var _didIteratorError4 = false;
408
- var _iteratorError4 = undefined;
263
+ var _iteratorNormalCompletion3 = true;
264
+ var _didIteratorError3 = false;
265
+ var _iteratorError3 = undefined;
409
266
 
410
267
  try {
411
- for (var _iterator4 = this.recordIds[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
412
- var recordId = _step4.value;
268
+ for (var _iterator3 = this.recordIds[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
269
+ var recordId = _step3.value;
413
270
  recordIdsSet[recordId] = true;
414
271
  }
415
272
  } catch (err) {
416
- _didIteratorError4 = true;
417
- _iteratorError4 = err;
273
+ _didIteratorError3 = true;
274
+ _iteratorError3 = err;
418
275
  } finally {
419
276
  try {
420
- if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
421
- _iterator4.return();
277
+ if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
278
+ _iterator3.return();
422
279
  }
423
280
  } finally {
424
- if (_didIteratorError4) {
425
- throw _iteratorError4;
281
+ if (_didIteratorError3) {
282
+ throw _iteratorError3;
426
283
  }
427
284
  }
428
285
  }
@@ -439,59 +296,85 @@ function (_RecordQueryResult) {
439
296
  */
440
297
 
441
298
  }, {
442
- key: "watch",
299
+ key: "_incrementCellValueKeyWatchCountAndWatchIfNecessary",
300
+
301
+ /** @internal */
302
+ value: function _incrementCellValueKeyWatchCountAndWatchIfNecessary(key, watchCallback) {
303
+ if (!this._cellValueKeyWatchCounts[key]) {
304
+ this._cellValueKeyWatchCounts[key] = 0;
305
+
306
+ this._recordStore.watch(key, watchCallback, this);
307
+ }
308
+
309
+ this._cellValueKeyWatchCounts[key]++;
310
+ }
311
+ /** @internal */
312
+
313
+ }, {
314
+ key: "_decrementCellValueKeyWatchCountAndUnwatchIfPossible",
315
+ value: function _decrementCellValueKeyWatchCountAndUnwatchIfPossible(key, watchCallback) {
316
+ if (!this._cellValueKeyWatchCounts[key]) {
317
+ // Key isn't watched, so just skip it. This matches behavior of Watchable,
318
+ // where calling unwatch on a key that isn't watched just no-ops.
319
+ return;
320
+ }
321
+
322
+ this._cellValueKeyWatchCounts[key]--;
443
323
 
324
+ if (this._cellValueKeyWatchCounts[key] === 0) {
325
+ // We're down to zero watches for this key, so we can actually unwatch it now.
326
+ this._recordStore.unwatch(key, watchCallback, this);
327
+
328
+ delete this._cellValueKeyWatchCounts[key];
329
+ }
330
+ }
444
331
  /** @inheritdoc */
332
+
333
+ }, {
334
+ key: "watch",
445
335
  value: function watch(keys, callback, context) {
446
336
  var validKeys = (0, _get2.default)((0, _getPrototypeOf2.default)(TableOrViewQueryResult.prototype), "watch", this).call(this, keys, callback, context);
447
-
448
- var callWatchOnRecordStoreWithKeys = keysToWatch => {
449
- this._recordStore.watchWithRecordQueryResult({
450
- recordQueryResult: this,
451
- keys: keysToWatch,
452
- callbackForRegistration: callback,
453
- context
454
- });
455
- };
456
-
457
- var _iteratorNormalCompletion5 = true;
458
- var _didIteratorError5 = false;
459
- var _iteratorError5 = undefined;
337
+ var _iteratorNormalCompletion4 = true;
338
+ var _didIteratorError4 = false;
339
+ var _iteratorError4 = undefined;
460
340
 
461
341
  try {
462
- for (var _iterator5 = validKeys[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
463
- var _key2 = _step5.value;
464
-
465
- // There are two watch systems:
466
- // 1) the query-based watch
467
- // 2) the non-query-based watch (through super)
468
- // Depending on the key, we may use one or both of the systems.
469
- // The non-query based watch system is used when when:
470
- // - watching: records, recordIds
471
- // - _loadDataAsync calls all watch keys on initial load
472
- // - Support for _watchRecordColorsIfNeeded
473
- // For all changes to the remaining keys - the query-based system is used.
342
+ for (var _iterator4 = validKeys[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
343
+ var _key2 = _step4.value;
344
+
474
345
  if (_key2.startsWith(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix)) {
475
- this._watchConfigByWatchableKey[_record_query_result.default.WatchableCellValuesInFieldKeyPrefix](_key2, callWatchOnRecordStoreWithKeys);
476
- } else {
477
- var _functionToCallWithKey = this._watchConfigByWatchableKey[_key2];
346
+ var fieldId = _key2.substring(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix.length);
347
+
348
+ if (this._fieldIdsSetToLoadOrNullIfAllFields && !(0, _private_utils.has)(this._fieldIdsSetToLoadOrNullIfAllFields, fieldId)) {
349
+ throw (0, _error_utils.spawnError)("Can't watch field because it wasn't included in RecordQueryResult fields: %s", fieldId);
350
+ }
351
+
352
+ this._incrementCellValueKeyWatchCountAndWatchIfNecessary(_key2, this._onCellValuesInFieldChanged);
353
+ }
478
354
 
479
- if (_functionToCallWithKey) {
480
- _functionToCallWithKey(_key2, callWatchOnRecordStoreWithKeys);
355
+ if (_key2 === _record_query_result.default.WatchableKeys.cellValues) {
356
+ if (this._fieldIdsSetToLoadOrNullIfAllFields) {
357
+ for (var _i = 0, _Object$keys = Object.keys(this._fieldIdsSetToLoadOrNullIfAllFields); _i < _Object$keys.length; _i++) {
358
+ var _fieldId = _Object$keys[_i];
359
+
360
+ this._incrementCellValueKeyWatchCountAndWatchIfNecessary(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix + _fieldId, this._onCellValuesChanged);
361
+ }
362
+ } else {
363
+ this._incrementCellValueKeyWatchCountAndWatchIfNecessary(_key2, this._onCellValuesChanged);
481
364
  }
482
365
  }
483
366
  }
484
367
  } catch (err) {
485
- _didIteratorError5 = true;
486
- _iteratorError5 = err;
368
+ _didIteratorError4 = true;
369
+ _iteratorError4 = err;
487
370
  } finally {
488
371
  try {
489
- if (!_iteratorNormalCompletion5 && _iterator5.return != null) {
490
- _iterator5.return();
372
+ if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
373
+ _iterator4.return();
491
374
  }
492
375
  } finally {
493
- if (_didIteratorError5) {
494
- throw _iteratorError5;
376
+ if (_didIteratorError4) {
377
+ throw _iteratorError4;
495
378
  }
496
379
  }
497
380
  }
@@ -504,41 +387,41 @@ function (_RecordQueryResult) {
504
387
  key: "unwatch",
505
388
  value: function unwatch(keys, callback, context) {
506
389
  var validKeys = (0, _get2.default)((0, _getPrototypeOf2.default)(TableOrViewQueryResult.prototype), "unwatch", this).call(this, keys, callback, context);
507
-
508
- var callUnwatchOnRecordStoreWithKeys = keysToUnwatch => {
509
- this._recordStore.unwatch(keysToUnwatch, callback, context);
510
- };
511
-
512
- var _iteratorNormalCompletion6 = true;
513
- var _didIteratorError6 = false;
514
- var _iteratorError6 = undefined;
390
+ var _iteratorNormalCompletion5 = true;
391
+ var _didIteratorError5 = false;
392
+ var _iteratorError5 = undefined;
515
393
 
516
394
  try {
517
- for (var _iterator6 = validKeys[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
518
- var _key3 = _step6.value;
395
+ for (var _iterator5 = validKeys[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
396
+ var _key3 = _step5.value;
519
397
 
520
- // This is the inverse of the watch logic
521
398
  if (_key3.startsWith(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix)) {
522
- this._watchConfigByWatchableKey[_record_query_result.default.WatchableCellValuesInFieldKeyPrefix](_key3, callUnwatchOnRecordStoreWithKeys);
523
- } else {
524
- var _functionToCallWithKey2 = this._watchConfigByWatchableKey[_key3];
399
+ this._decrementCellValueKeyWatchCountAndUnwatchIfPossible(_key3, this._onCellValuesInFieldChanged);
400
+ }
525
401
 
526
- if (_functionToCallWithKey2) {
527
- _functionToCallWithKey2(_key3, callUnwatchOnRecordStoreWithKeys);
402
+ if (_key3 === _record_query_result.default.WatchableKeys.cellValues) {
403
+ if (this._fieldIdsSetToLoadOrNullIfAllFields) {
404
+ for (var _i2 = 0, _Object$keys2 = Object.keys(this._fieldIdsSetToLoadOrNullIfAllFields); _i2 < _Object$keys2.length; _i2++) {
405
+ var fieldId = _Object$keys2[_i2];
406
+
407
+ this._decrementCellValueKeyWatchCountAndUnwatchIfPossible(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix + fieldId, this._onCellValuesChanged);
408
+ }
409
+ } else {
410
+ this._decrementCellValueKeyWatchCountAndUnwatchIfPossible(_key3, this._onCellValuesChanged);
528
411
  }
529
412
  }
530
413
  }
531
414
  } catch (err) {
532
- _didIteratorError6 = true;
533
- _iteratorError6 = err;
415
+ _didIteratorError5 = true;
416
+ _iteratorError5 = err;
534
417
  } finally {
535
418
  try {
536
- if (!_iteratorNormalCompletion6 && _iterator6.return != null) {
537
- _iterator6.return();
419
+ if (!_iteratorNormalCompletion5 && _iterator5.return != null) {
420
+ _iterator5.return();
538
421
  }
539
422
  } finally {
540
- if (_didIteratorError6) {
541
- throw _iteratorError6;
423
+ if (_didIteratorError5) {
424
+ throw _iteratorError5;
542
425
  }
543
426
  }
544
427
  }
@@ -550,54 +433,45 @@ function (_RecordQueryResult) {
550
433
  }, {
551
434
  key: "loadDataAsync",
552
435
  value: function loadDataAsync() {
553
- var sourceModelLoadPromise, fieldIdsToLoadOrNullIfAllFields, allFieldsToQuery, metadataFieldId, sourceModelLoadedPromise;
554
- return _regenerator.default.async(function loadDataAsync$(_context2) {
436
+ var sourceModelLoadPromise, cellValuesInFieldsLoadPromise;
437
+ return _regenerator.default.async(function loadDataAsync$(_context) {
555
438
  while (1) {
556
- switch (_context2.prev = _context2.next) {
439
+ switch (_context.prev = _context.next) {
557
440
  case 0:
558
441
  if (!this._sourceModel.isDeleted) {
559
- _context2.next = 2;
442
+ _context.next = 2;
560
443
  break;
561
444
  }
562
445
 
563
446
  throw this._spawnErrorForDeletion();
564
447
 
565
448
  case 2:
566
- fieldIdsToLoadOrNullIfAllFields = this._fieldIdsSetToLoadOrNullIfAllFields ? Object.keys(this._fieldIdsSetToLoadOrNullIfAllFields) : null;
449
+ if (this._fieldIdsSetToLoadOrNullIfAllFields) {
450
+ cellValuesInFieldsLoadPromise = this._recordStore.loadCellValuesInFieldIdsAsync(Object.keys(this._fieldIdsSetToLoadOrNullIfAllFields));
451
+ } else {
452
+ // Load all fields.
453
+ cellValuesInFieldsLoadPromise = this._recordStore.loadDataAsync();
454
+ }
567
455
 
568
456
  if (this._sourceModel instanceof _table.default) {
569
- if (fieldIdsToLoadOrNullIfAllFields) {
570
- // TODO: (#proj-blocks-sdk-record-limits) Do we need to load the recordData metadata field
571
- // in order to load any record data? If we do - this should be encapsulated entirely
572
- // inside of the RecordStore in publicAirtableInterface.
573
- // (should the queryModel be able to specify metadata requirements?)
574
- allFieldsToQuery = fieldIdsToLoadOrNullIfAllFields;
575
- metadataFieldId = this._recordStore._getFieldIdForCausingRecordMetadataToLoad();
576
-
577
- if (!allFieldsToQuery.includes(metadataFieldId)) {
578
- allFieldsToQuery = [...allFieldsToQuery, metadataFieldId];
579
- }
580
-
581
- sourceModelLoadPromise = this._recordStore.loadAllCellValuesInFieldIdsAsync(allFieldsToQuery);
457
+ if (this._fieldIdsSetToLoadOrNullIfAllFields) {
458
+ sourceModelLoadPromise = this._recordStore.loadRecordMetadataAsync();
582
459
  } else {
583
- // Load all fields.
584
- sourceModelLoadPromise = this._recordStore.loadAllCellValuesAsync();
460
+ // table.loadDataAsync is a superset of loadRecordMetadataAsync,
461
+ // so no need to load record metadata again.
462
+ sourceModelLoadPromise = null;
585
463
  }
586
464
  } else {
587
- sourceModelLoadPromise = this._recordStore.loadViewDataStoreAndCellValuesForFieldIdsAsync(this._sourceModel.id, fieldIdsToLoadOrNullIfAllFields);
588
- } // See comment on _loadedSourceModelQueryIds declaration for details
589
-
465
+ sourceModelLoadPromise = this._recordStore.getViewDataStore(this._sourceModel.id).loadDataAsync();
466
+ }
590
467
 
591
- sourceModelLoadedPromise = sourceModelLoadPromise.then(sourceModelQueryId => {
592
- this._loadedSourceModelQueryIds.push(sourceModelQueryId);
593
- });
594
- this._mostRecentSourceModelLoadedPromise = Promise.all([sourceModelLoadedPromise, this._loadRecordColorsAsync()]);
595
- _context2.next = 8;
468
+ this._mostRecentSourceModelLoadPromise = Promise.all([sourceModelLoadPromise, cellValuesInFieldsLoadPromise, this._loadRecordColorsAsync()]);
469
+ _context.next = 7;
596
470
  return _regenerator.default.awrap((0, _get2.default)((0, _getPrototypeOf2.default)(TableOrViewQueryResult.prototype), "loadDataAsync", this).call(this));
597
471
 
598
- case 8:
472
+ case 7:
599
473
  case "end":
600
- return _context2.stop();
474
+ return _context.stop();
601
475
  }
602
476
  }
603
477
  }, null, this);
@@ -613,26 +487,26 @@ function (_RecordQueryResult) {
613
487
 
614
488
  var fieldIds = this._normalizedOpts.fieldIdsOrNullIfAllFields || this._table.fields.map(field => field.id);
615
489
 
616
- var _iteratorNormalCompletion7 = true;
617
- var _didIteratorError7 = false;
618
- var _iteratorError7 = undefined;
490
+ var _iteratorNormalCompletion6 = true;
491
+ var _didIteratorError6 = false;
492
+ var _iteratorError6 = undefined;
619
493
 
620
494
  try {
621
- for (var _iterator7 = fieldIds[Symbol.iterator](), _step7; !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
622
- var fieldId = _step7.value;
495
+ for (var _iterator6 = fieldIds[Symbol.iterator](), _step6; !(_iteratorNormalCompletion6 = (_step6 = _iterator6.next()).done); _iteratorNormalCompletion6 = true) {
496
+ var fieldId = _step6.value;
623
497
  changedKeys.push(_record_query_result.default.WatchableCellValuesInFieldKeyPrefix + fieldId);
624
498
  }
625
499
  } catch (err) {
626
- _didIteratorError7 = true;
627
- _iteratorError7 = err;
500
+ _didIteratorError6 = true;
501
+ _iteratorError6 = err;
628
502
  } finally {
629
503
  try {
630
- if (!_iteratorNormalCompletion7 && _iterator7.return != null) {
631
- _iterator7.return();
504
+ if (!_iteratorNormalCompletion6 && _iterator6.return != null) {
505
+ _iterator6.return();
632
506
  }
633
507
  } finally {
634
- if (_didIteratorError7) {
635
- throw _iteratorError7;
508
+ if (_didIteratorError6) {
509
+ throw _iteratorError6;
636
510
  }
637
511
  }
638
512
  }
@@ -644,72 +518,55 @@ function (_RecordQueryResult) {
644
518
  }, {
645
519
  key: "_loadDataAsync",
646
520
  value: function _loadDataAsync() {
647
- var viewDataStore, _iteratorNormalCompletion8, _didIteratorError8, _iteratorError8, _iterator8, _step8, sort, field;
521
+ var viewDataStore, _iteratorNormalCompletion7, _didIteratorError7, _iteratorError7, _iterator7, _step7, sort, field;
648
522
 
649
- return _regenerator.default.async(function _loadDataAsync$(_context3) {
523
+ return _regenerator.default.async(function _loadDataAsync$(_context2) {
650
524
  while (1) {
651
- switch (_context3.prev = _context3.next) {
525
+ switch (_context2.prev = _context2.next) {
652
526
  case 0:
653
527
  this._table.__tableOrViewQueryResultPool.registerObjectForReuseStrong(this);
654
528
 
655
- (0, _error_utils.invariant)(this._mostRecentSourceModelLoadedPromise, 'No source model load promises');
656
- _context3.next = 4;
657
- return _regenerator.default.awrap(this._mostRecentSourceModelLoadedPromise);
529
+ (0, _error_utils.invariant)(this._mostRecentSourceModelLoadPromise, 'No source model load promises');
530
+ _context2.next = 4;
531
+ return _regenerator.default.awrap(this._mostRecentSourceModelLoadPromise);
658
532
 
659
533
  case 4:
660
534
  if (this._sorts) {
661
535
  this._replaceVisList();
662
536
  }
663
537
 
664
- this._orderedRecordIds = this._generateOrderedRecordIds(); // Ensure that groups are not loaded
538
+ this._orderedRecordIds = this._generateOrderedRecordIds();
539
+ this._orderedGroups = this._generateAndLoadOrderedGroups(); // The server provided value and developer supplied value are the same, no conversion needed
665
540
 
666
- this._orderedGroups = null; // The server provided value and developer supplied value are the same, no conversion needed
667
-
668
- this._loadedGroupLevels = null;
541
+ this._loadedGroupLevels = this._sourceModelGroupLevels;
669
542
 
670
543
  if (this._sourceModel instanceof _table.default) {
671
- this._recordStore.watchWithRecordQueryResult({
672
- recordQueryResult: this,
673
- keys: _record_store.WatchableRecordStoreKeys.records,
674
- callbackForRegistration: this._onRecordsChanged,
675
- context: this
676
- });
544
+ this._recordStore.watch(_record_store.WatchableRecordStoreKeys.records, this._onRecordsChanged, this);
677
545
  } else {
678
546
  viewDataStore = this._recordStore.getViewDataStore(this._sourceModel.id);
679
547
  viewDataStore.watch(_view_data_store.WatchableViewDataStoreKeys.visibleRecords, this._onRecordsChanged, this);
680
548
  viewDataStore.watch(_view_data_store.WatchableViewDataStoreKeys.groups, this._onGroupsChanged, this);
681
549
  viewDataStore.watch(_view_data_store.WatchableViewDataStoreKeys.groupLevels, this._onGroupLevelsChanged, this);
682
- } // This only watches cell values if a sort is specified
683
-
550
+ }
684
551
 
685
- this._recordStore.watchWithRecordQueryResult({
686
- recordQueryResult: this,
687
- keys: this._cellValuesForSortWatchKeys,
688
- callbackForRegistration: this._onCellValuesForSortChanged,
689
- context: this
690
- });
552
+ this._recordStore.watch(this._cellValuesForSortWatchKeys, this._onCellValuesForSortChanged, this);
691
553
 
692
- this._recordStore.watchWithRecordQueryResult({
693
- recordQueryResult: this,
694
- keys: this._cellValuesForGroupWatchKeys,
695
- callbackForRegistration: this._onCellValuesForGroupChanged,
696
- context: this
697
- });
554
+ this._recordStore.watch(this._cellValuesForGroupWatchKeys, this._onCellValuesForGroupChanged, this);
698
555
 
699
556
  this._table.watch(_table.WatchableTableKeys.fields, this._onTableFieldsChanged, this);
700
557
 
701
558
  if (!this._sorts) {
702
- _context3.next = 32;
559
+ _context2.next = 32;
703
560
  break;
704
561
  }
705
562
 
706
- _iteratorNormalCompletion8 = true;
707
- _didIteratorError8 = false;
708
- _iteratorError8 = undefined;
709
- _context3.prev = 16;
563
+ _iteratorNormalCompletion7 = true;
564
+ _didIteratorError7 = false;
565
+ _iteratorError7 = undefined;
566
+ _context2.prev = 16;
710
567
 
711
- for (_iterator8 = this._sorts[Symbol.iterator](); !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
712
- sort = _step8.value;
568
+ for (_iterator7 = this._sorts[Symbol.iterator](); !(_iteratorNormalCompletion7 = (_step7 = _iterator7.next()).done); _iteratorNormalCompletion7 = true) {
569
+ sort = _step7.value;
713
570
  field = this._table.getFieldByIdIfExists(sort.fieldId);
714
571
 
715
572
  if (field) {
@@ -718,45 +575,45 @@ function (_RecordQueryResult) {
718
575
  }
719
576
  }
720
577
 
721
- _context3.next = 24;
578
+ _context2.next = 24;
722
579
  break;
723
580
 
724
581
  case 20:
725
- _context3.prev = 20;
726
- _context3.t0 = _context3["catch"](16);
727
- _didIteratorError8 = true;
728
- _iteratorError8 = _context3.t0;
582
+ _context2.prev = 20;
583
+ _context2.t0 = _context2["catch"](16);
584
+ _didIteratorError7 = true;
585
+ _iteratorError7 = _context2.t0;
729
586
 
730
587
  case 24:
731
- _context3.prev = 24;
732
- _context3.prev = 25;
588
+ _context2.prev = 24;
589
+ _context2.prev = 25;
733
590
 
734
- if (!_iteratorNormalCompletion8 && _iterator8.return != null) {
735
- _iterator8.return();
591
+ if (!_iteratorNormalCompletion7 && _iterator7.return != null) {
592
+ _iterator7.return();
736
593
  }
737
594
 
738
595
  case 27:
739
- _context3.prev = 27;
596
+ _context2.prev = 27;
740
597
 
741
- if (!_didIteratorError8) {
742
- _context3.next = 30;
598
+ if (!_didIteratorError7) {
599
+ _context2.next = 30;
743
600
  break;
744
601
  }
745
602
 
746
- throw _iteratorError8;
603
+ throw _iteratorError7;
747
604
 
748
605
  case 30:
749
- return _context3.finish(27);
606
+ return _context2.finish(27);
750
607
 
751
608
  case 31:
752
- return _context3.finish(24);
609
+ return _context2.finish(24);
753
610
 
754
611
  case 32:
755
- return _context3.abrupt("return", this._getChangedKeysOnLoad());
612
+ return _context2.abrupt("return", this._getChangedKeysOnLoad());
756
613
 
757
614
  case 33:
758
615
  case "end":
759
- return _context3.stop();
616
+ return _context2.stop();
760
617
  }
761
618
  }
762
619
  }, null, this, [[16, 20, 24, 32], [25,, 27, 31]]);
@@ -768,9 +625,21 @@ function (_RecordQueryResult) {
768
625
  value: function unloadData() {
769
626
  (0, _get2.default)((0, _getPrototypeOf2.default)(TableOrViewQueryResult.prototype), "unloadData", this).call(this);
770
627
 
771
- if (this._loadedSourceModelQueryIds.length === 0) {
772
- // Source model must have loaded a queryId to unload
773
- return;
628
+ if (this._sourceModel instanceof _table.default) {
629
+ if (this._fieldIdsSetToLoadOrNullIfAllFields) {
630
+ this._recordStore.unloadRecordMetadata();
631
+ } else {
632
+ this._recordStore.unloadData();
633
+ }
634
+ } else {
635
+ // If the view is deleted, we can't get a data store for it.
636
+ if (!this._sourceModel.isDeleted) {
637
+ this._recordStore.getViewDataStore(this._sourceModel.id).unloadData();
638
+ }
639
+ }
640
+
641
+ if (this._fieldIdsSetToLoadOrNullIfAllFields) {
642
+ this._recordStore.unloadCellValuesInFieldIds(Object.keys(this._fieldIdsSetToLoadOrNullIfAllFields));
774
643
  }
775
644
 
776
645
  this._unloadRecordColors();
@@ -780,34 +649,119 @@ function (_RecordQueryResult) {
780
649
  }, {
781
650
  key: "_unloadData",
782
651
  value: function _unloadData() {
783
- (0, _private_utils.fireAndForgetPromise)(this._unloadDataAsync);
652
+ this._mostRecentSourceModelLoadPromise = null;
653
+
654
+ if (this._sourceModel instanceof _table.default) {
655
+ this._recordStore.unwatch(_record_store.WatchableRecordStoreKeys.records, this._onRecordsChanged, this);
656
+ } else {
657
+ if (!this._sourceModel.isDeleted) {
658
+ var viewDataStore = this._recordStore.getViewDataStore(this._sourceModel.id);
659
+
660
+ viewDataStore.unwatch(_view_data_store.WatchableViewDataStoreKeys.visibleRecords, this._onRecordsChanged, this);
661
+ viewDataStore.unwatch(_view_data_store.WatchableViewDataStoreKeys.groups, this._onGroupsChanged, this);
662
+ viewDataStore.unwatch(_view_data_store.WatchableViewDataStoreKeys.groupLevels, this._onGroupLevelsChanged, this);
663
+ }
664
+ }
665
+
666
+ this._recordStore.unwatch(this._cellValuesForSortWatchKeys, this._onCellValuesForSortChanged, this);
667
+
668
+ this._recordStore.unwatch(this._cellValuesForGroupWatchKeys, this._onCellValuesForGroupChanged, this);
669
+
670
+ this._table.unwatch(_table.WatchableTableKeys.fields, this._onTableFieldsChanged, this); // If the table is deleted, can't call getFieldById on it below.
671
+
672
+
673
+ if (!this._table.isDeleted && this._sorts) {
674
+ var _iteratorNormalCompletion8 = true;
675
+ var _didIteratorError8 = false;
676
+ var _iteratorError8 = undefined;
677
+
678
+ try {
679
+ for (var _iterator8 = this._sorts[Symbol.iterator](), _step8; !(_iteratorNormalCompletion8 = (_step8 = _iterator8.next()).done); _iteratorNormalCompletion8 = true) {
680
+ var sort = _step8.value;
681
+
682
+ var field = this._table.getFieldByIdIfExists(sort.fieldId);
683
+
684
+ if (field) {
685
+ field.unwatch('type', this._onFieldConfigChanged, this);
686
+ field.unwatch('options', this._onFieldConfigChanged, this);
687
+ }
688
+ }
689
+ } catch (err) {
690
+ _didIteratorError8 = true;
691
+ _iteratorError8 = err;
692
+ } finally {
693
+ try {
694
+ if (!_iteratorNormalCompletion8 && _iterator8.return != null) {
695
+ _iterator8.return();
696
+ }
697
+ } finally {
698
+ if (_didIteratorError8) {
699
+ throw _iteratorError8;
700
+ }
701
+ }
702
+ }
703
+ }
704
+
705
+ this._unloadOrderedGroupsIfNeeded();
706
+
707
+ this._visList = null;
708
+ this._orderedRecordIds = null;
709
+ this._recordIdsSet = null;
710
+
711
+ this._table.__tableOrViewQueryResultPool.unregisterObjectForReuseStrong(this);
784
712
  }
713
+ /** @internal */
714
+
785
715
  }, {
786
716
  key: "_addRecordIdsToVisList",
787
-
788
- /** @internal */
789
717
  value: function _addRecordIdsToVisList(recordIds) {
790
718
  var visList = this._visList;
791
719
  (0, _error_utils.invariant)(visList, 'No vis list');
792
- visList.addRecordIds(recordIds);
720
+ var _iteratorNormalCompletion9 = true;
721
+ var _didIteratorError9 = false;
722
+ var _iteratorError9 = undefined;
723
+
724
+ try {
725
+ for (var _iterator9 = recordIds[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
726
+ var recordId = _step9.value;
727
+
728
+ var record = this._recordStore.getRecordByIdIfExists(recordId);
729
+
730
+ (0, _error_utils.invariant)(record, 'Record missing in table');
731
+ visList.addRecordData(record._data);
732
+ }
733
+ } catch (err) {
734
+ _didIteratorError9 = true;
735
+ _iteratorError9 = err;
736
+ } finally {
737
+ try {
738
+ if (!_iteratorNormalCompletion9 && _iterator9.return != null) {
739
+ _iterator9.return();
740
+ }
741
+ } finally {
742
+ if (_didIteratorError9) {
743
+ throw _iteratorError9;
744
+ }
745
+ }
746
+ }
793
747
  }
794
748
  /** @internal */
795
749
 
796
750
  }, {
797
751
  key: "_onGroupLevelsChanged",
798
752
  value: function _onGroupLevelsChanged(model, key, updates) {
799
- this._loadedGroupLevels = null;
753
+ this._loadedGroupLevels = this._sourceModelGroupLevels;
800
754
 
801
- this._unloadGroupsAndTriggerWatches();
755
+ this._unloadRemovedGroupsAndLoadNewGroupsAndTriggerWatches();
802
756
 
803
- this._onChange(_record_query_result.WatchableRecordQueryResultKeys.groupLevels);
757
+ this._onChange(_record_query_result.default.WatchableKeys.groupLevels);
804
758
  }
805
759
  /** @internal */
806
760
 
807
761
  }, {
808
762
  key: "_onGroupsChanged",
809
763
  value: function _onGroupsChanged(model, key, updates) {
810
- this._unloadGroupsAndTriggerWatches();
764
+ this._unloadRemovedGroupsAndLoadNewGroupsAndTriggerWatches();
811
765
  }
812
766
  /** @internal */
813
767
 
@@ -830,11 +784,7 @@ function (_RecordQueryResult) {
830
784
  addedRecordIds: _addedRecordIds,
831
785
  removedRecordIds: _removedRecordIds
832
786
  };
833
- } // We never need to handle an initial empty update coming from record_store
834
- // since it does not issue empty initial updates.
835
- // Keep the if statement for type safety. Invariant would also work, but is riskier.
836
- // istanbul ignore if
837
-
787
+ }
838
788
 
839
789
  if (!updates) {
840
790
  // If there are no updates, do nothing, since we'll handle the initial
@@ -861,50 +811,50 @@ function (_RecordQueryResult) {
861
811
  }
862
812
 
863
813
  if (this._recordIdsSet) {
864
- var _iteratorNormalCompletion9 = true;
865
- var _didIteratorError9 = false;
866
- var _iteratorError9 = undefined;
814
+ var _iteratorNormalCompletion10 = true;
815
+ var _didIteratorError10 = false;
816
+ var _iteratorError10 = undefined;
867
817
 
868
818
  try {
869
- for (var _iterator9 = addedRecordIds[Symbol.iterator](), _step9; !(_iteratorNormalCompletion9 = (_step9 = _iterator9.next()).done); _iteratorNormalCompletion9 = true) {
870
- var recordId = _step9.value;
819
+ for (var _iterator10 = addedRecordIds[Symbol.iterator](), _step10; !(_iteratorNormalCompletion10 = (_step10 = _iterator10.next()).done); _iteratorNormalCompletion10 = true) {
820
+ var recordId = _step10.value;
871
821
  this._recordIdsSet[recordId] = true;
872
822
  }
873
823
  } catch (err) {
874
- _didIteratorError9 = true;
875
- _iteratorError9 = err;
824
+ _didIteratorError10 = true;
825
+ _iteratorError10 = err;
876
826
  } finally {
877
827
  try {
878
- if (!_iteratorNormalCompletion9 && _iterator9.return != null) {
879
- _iterator9.return();
828
+ if (!_iteratorNormalCompletion10 && _iterator10.return != null) {
829
+ _iterator10.return();
880
830
  }
881
831
  } finally {
882
- if (_didIteratorError9) {
883
- throw _iteratorError9;
832
+ if (_didIteratorError10) {
833
+ throw _iteratorError10;
884
834
  }
885
835
  }
886
836
  }
887
837
 
888
- var _iteratorNormalCompletion10 = true;
889
- var _didIteratorError10 = false;
890
- var _iteratorError10 = undefined;
838
+ var _iteratorNormalCompletion11 = true;
839
+ var _didIteratorError11 = false;
840
+ var _iteratorError11 = undefined;
891
841
 
892
842
  try {
893
- for (var _iterator10 = removedRecordIds[Symbol.iterator](), _step10; !(_iteratorNormalCompletion10 = (_step10 = _iterator10.next()).done); _iteratorNormalCompletion10 = true) {
894
- var _recordId = _step10.value;
843
+ for (var _iterator11 = removedRecordIds[Symbol.iterator](), _step11; !(_iteratorNormalCompletion11 = (_step11 = _iterator11.next()).done); _iteratorNormalCompletion11 = true) {
844
+ var _recordId = _step11.value;
895
845
  this._recordIdsSet[_recordId] = undefined;
896
846
  }
897
847
  } catch (err) {
898
- _didIteratorError10 = true;
899
- _iteratorError10 = err;
848
+ _didIteratorError11 = true;
849
+ _iteratorError11 = err;
900
850
  } finally {
901
851
  try {
902
- if (!_iteratorNormalCompletion10 && _iterator10.return != null) {
903
- _iterator10.return();
852
+ if (!_iteratorNormalCompletion11 && _iterator11.return != null) {
853
+ _iterator11.return();
904
854
  }
905
855
  } finally {
906
- if (_didIteratorError10) {
907
- throw _iteratorError10;
856
+ if (_didIteratorError11) {
857
+ throw _iteratorError11;
908
858
  }
909
859
  }
910
860
  }
@@ -933,10 +883,6 @@ function (_RecordQueryResult) {
933
883
  }, {
934
884
  key: "_onCellValuesForSortChanged",
935
885
  value: function _onCellValuesForSortChanged(recordStore, key, recordIds, fieldId) {
936
- // We never need to handle an initial empty update coming from record_store
937
- // since it does not issue empty initial updates.
938
- // Keep the if statement for type safety. Invariant would also work, but is riskier.
939
- // istanbul ignore if
940
886
  if (!recordIds || !fieldId) {
941
887
  // If there are no updates, do nothing, since we'll handle the initial
942
888
  // callback when the record set is loaded (and we don't want to fire
@@ -1002,13 +948,13 @@ function (_RecordQueryResult) {
1002
948
  // deleted and then undeleted.
1003
949
 
1004
950
  var wereAnyFieldsCreatedOrDeleted = false;
1005
- var _iteratorNormalCompletion11 = true;
1006
- var _didIteratorError11 = false;
1007
- var _iteratorError11 = undefined;
951
+ var _iteratorNormalCompletion12 = true;
952
+ var _didIteratorError12 = false;
953
+ var _iteratorError12 = undefined;
1008
954
 
1009
955
  try {
1010
- for (var _iterator11 = addedFieldIds[Symbol.iterator](), _step11; !(_iteratorNormalCompletion11 = (_step11 = _iterator11.next()).done); _iteratorNormalCompletion11 = true) {
1011
- var fieldId = _step11.value;
956
+ for (var _iterator12 = addedFieldIds[Symbol.iterator](), _step12; !(_iteratorNormalCompletion12 = (_step12 = _iterator12.next()).done); _iteratorNormalCompletion12 = true) {
957
+ var fieldId = _step12.value;
1012
958
 
1013
959
  // If a field that we rely on was created (i.e. it was undeleted), we need to
1014
960
  // make sure we're watching it's config.
@@ -1025,16 +971,16 @@ function (_RecordQueryResult) {
1025
971
  // istanbul ignore else
1026
972
 
1027
973
  } catch (err) {
1028
- _didIteratorError11 = true;
1029
- _iteratorError11 = err;
974
+ _didIteratorError12 = true;
975
+ _iteratorError12 = err;
1030
976
  } finally {
1031
977
  try {
1032
- if (!_iteratorNormalCompletion11 && _iterator11.return != null) {
1033
- _iterator11.return();
978
+ if (!_iteratorNormalCompletion12 && _iterator12.return != null) {
979
+ _iterator12.return();
1034
980
  }
1035
981
  } finally {
1036
- if (_didIteratorError11) {
1037
- throw _iteratorError11;
982
+ if (_didIteratorError12) {
983
+ throw _iteratorError12;
1038
984
  }
1039
985
  }
1040
986
  }
@@ -1064,6 +1010,34 @@ function (_RecordQueryResult) {
1064
1010
  }
1065
1011
  /** @internal */
1066
1012
 
1013
+ }, {
1014
+ key: "_onCellValuesChanged",
1015
+ value: function _onCellValuesChanged(table, key, updates) {
1016
+ if (!updates) {
1017
+ // If there are no updates, do nothing, since we'll handle the initial
1018
+ // callback when the record set is loaded (and we don't want to fire
1019
+ // a cellValues change twice with no data).
1020
+ return;
1021
+ }
1022
+
1023
+ this._onChange(_record_query_result.default.WatchableKeys.cellValues, updates);
1024
+ }
1025
+ /** @internal */
1026
+
1027
+ }, {
1028
+ key: "_onCellValuesInFieldChanged",
1029
+ value: function _onCellValuesInFieldChanged(table, key, recordIds, fieldId) {
1030
+ if (!recordIds && !fieldId) {
1031
+ // If there are no updates, do nothing, since we'll handle the initial
1032
+ // callback when the record set is loaded (and we don't want to fire
1033
+ // a cellValuesInField change twice with no data).
1034
+ return;
1035
+ }
1036
+
1037
+ this._onChange(key, recordIds, fieldId);
1038
+ }
1039
+ /** @internal */
1040
+
1067
1041
  }, {
1068
1042
  key: "_generateOrderedRecordIds",
1069
1043
  value: function _generateOrderedRecordIds() {
@@ -1081,6 +1055,7 @@ function (_RecordQueryResult) {
1081
1055
  value: function _generateAndLoadOrderedGroups() {
1082
1056
  // istanbul ignore next
1083
1057
  if (this._groupLevels) {
1058
+ (0, _error_utils.invariant)(this._visList, 'Cannot generate record ids without a vis list');
1084
1059
  throw (0, _error_utils.spawnError)('custom group configs not supported');
1085
1060
  } else {
1086
1061
  // Get the group from the view itself
@@ -1106,26 +1081,26 @@ function (_RecordQueryResult) {
1106
1081
  key: "_unloadOrderedGroupsIfNeeded",
1107
1082
  value: function _unloadOrderedGroupsIfNeeded() {
1108
1083
  if (this._orderedGroups) {
1109
- var _iteratorNormalCompletion12 = true;
1110
- var _didIteratorError12 = false;
1111
- var _iteratorError12 = undefined;
1084
+ var _iteratorNormalCompletion13 = true;
1085
+ var _didIteratorError13 = false;
1086
+ var _iteratorError13 = undefined;
1112
1087
 
1113
1088
  try {
1114
- for (var _iterator12 = this._orderedGroups[Symbol.iterator](), _step12; !(_iteratorNormalCompletion12 = (_step12 = _iterator12.next()).done); _iteratorNormalCompletion12 = true) {
1115
- var group = _step12.value;
1089
+ for (var _iterator13 = this._orderedGroups[Symbol.iterator](), _step13; !(_iteratorNormalCompletion13 = (_step13 = _iterator13.next()).done); _iteratorNormalCompletion13 = true) {
1090
+ var group = _step13.value;
1116
1091
  group.unloadData();
1117
1092
  }
1118
1093
  } catch (err) {
1119
- _didIteratorError12 = true;
1120
- _iteratorError12 = err;
1094
+ _didIteratorError13 = true;
1095
+ _iteratorError13 = err;
1121
1096
  } finally {
1122
1097
  try {
1123
- if (!_iteratorNormalCompletion12 && _iterator12.return != null) {
1124
- _iterator12.return();
1098
+ if (!_iteratorNormalCompletion13 && _iterator13.return != null) {
1099
+ _iterator13.return();
1125
1100
  }
1126
1101
  } finally {
1127
- if (_didIteratorError12) {
1128
- throw _iteratorError12;
1102
+ if (_didIteratorError13) {
1103
+ throw _iteratorError13;
1129
1104
  }
1130
1105
  }
1131
1106
  }
@@ -1133,21 +1108,22 @@ function (_RecordQueryResult) {
1133
1108
  }
1134
1109
  /**
1135
1110
  * If groupings change then some groups will need to be created, and some removed
1136
- * Triggers the WatchableKeys.group, as by necessity this will have changed.
1137
- * TODO: This _should_ handle the diffing necessary to unload, and reload only the changed groups.
1111
+ * This (TODO: will) handle the diffing necessary to unload, and reload only the changed groups.
1112
+ * Also triggers the WatchableKeys.group, as by necessity this will have changed.
1138
1113
  *
1139
1114
  * @internal
1140
1115
  */
1141
1116
  // TODO: (SeanKeenan) Properly diff and only unload groups that need to be unloaded
1142
1117
  // In the meantime it's not too expensive to just unload everything, and GroupedRecordQueryResults
1143
1118
  // don't yet properly watch changes relevant to updating, so this isn't too bad.
1119
+ // (They do invalidate their recordId caches, which is some very cheap work that we could remove)
1144
1120
 
1145
1121
  }, {
1146
- key: "_unloadGroupsAndTriggerWatches",
1147
- value: function _unloadGroupsAndTriggerWatches() {
1122
+ key: "_unloadRemovedGroupsAndLoadNewGroupsAndTriggerWatches",
1123
+ value: function _unloadRemovedGroupsAndLoadNewGroupsAndTriggerWatches() {
1148
1124
  this._unloadOrderedGroupsIfNeeded();
1149
1125
 
1150
- this._orderedGroups = null;
1126
+ this._orderedGroups = this._generateAndLoadOrderedGroups();
1151
1127
 
1152
1128
  this._onChange(_record_query_result.default.WatchableKeys.groups);
1153
1129
  }
@@ -1157,14 +1133,15 @@ function (_RecordQueryResult) {
1157
1133
  key: "_replaceVisList",
1158
1134
  value: function _replaceVisList() {
1159
1135
  var airtableInterface = this._sdk.__airtableInterface;
1136
+ var appInterface = this._sdk.__appInterface;
1160
1137
 
1161
- var recordIds = this._sourceModelRecords.map(record => record.id);
1138
+ var recordDatas = this._sourceModelRecords.map(record => record._data);
1162
1139
 
1163
1140
  var fieldDatas = this._table.fields.map(field => field._data);
1164
1141
 
1165
1142
  var filteredSorts = this._getSortsWithDeletedFieldsFiltered();
1166
1143
 
1167
- this._visList = airtableInterface.createVisListWithRecordIds(recordIds, fieldDatas, filteredSorts, this._table.id);
1144
+ this._visList = airtableInterface.createVisList(appInterface, recordDatas, fieldDatas, filteredSorts);
1168
1145
  }
1169
1146
  /** @internal */
1170
1147
 
@@ -1190,31 +1167,6 @@ function (_RecordQueryResult) {
1190
1167
  var sourceModelName = this._sourceModel instanceof _table.default ? 'table' : 'view';
1191
1168
  return (0, _error_utils.spawnError)("RecordQueryResult's underlying %s has been deleted", sourceModelName);
1192
1169
  }
1193
- /** @internal */
1194
-
1195
- }, {
1196
- key: "__constructQuerySpecForBlockFieldSelectionSpec",
1197
- value: function __constructQuerySpecForBlockFieldSelectionSpec(fieldSelection) {
1198
- if (this._sourceModel instanceof _view.default) {
1199
- return {
1200
- sourceType: _block_query_spec.BlockQuerySourceType.VIEW,
1201
- sourceTableId: this._table.id,
1202
- sourceViewId: this._sourceModel.id,
1203
- recordSelection: {
1204
- fieldSelection
1205
- },
1206
- viewMetadataSelection: {}
1207
- };
1208
- } else {
1209
- return {
1210
- sourceType: _block_query_spec.BlockQuerySourceType.TABLE,
1211
- sourceTableId: this._table.id,
1212
- recordSelection: {
1213
- fieldSelection
1214
- }
1215
- };
1216
- }
1217
- }
1218
1170
  }, {
1219
1171
  key: "_dataOrNullIfDeleted",
1220
1172
  get: function get() {
@@ -1223,7 +1175,9 @@ function (_RecordQueryResult) {
1223
1175
  }
1224
1176
 
1225
1177
  return {
1226
- recordIds: this._orderedRecordIds
1178
+ recordIds: this._orderedRecordIds,
1179
+ groups: this._orderedGroups,
1180
+ groupLevels: this._loadedGroupLevels
1227
1181
  };
1228
1182
  }
1229
1183
  /** @internal */
@@ -1277,10 +1231,8 @@ function (_RecordQueryResult) {
1277
1231
  var recordIds = this._data.recordIds; // Throws when the model has been deleted.
1278
1232
 
1279
1233
  (0, _error_utils.invariant)(this.isDataLoaded, 'RecordQueryResult data is not loaded');
1280
- (0, _error_utils.invariant)(recordIds, 'No recordIds'); // TODO: (#proj-blocks-sdk-record-limits) Change this to readonly upon the
1281
- // next breaking change.
1282
-
1283
- return [...recordIds];
1234
+ (0, _error_utils.invariant)(recordIds, 'No recordIds');
1235
+ return recordIds;
1284
1236
  }
1285
1237
  /**
1286
1238
  * The ordered GroupedRecordQueryResult's in this RecordQueryResult.
@@ -1293,19 +1245,12 @@ function (_RecordQueryResult) {
1293
1245
  }, {
1294
1246
  key: "groups",
1295
1247
  get: function get() {
1296
- var _this$_orderedGroups;
1297
-
1298
1248
  // The following statements have been carefully sequenced to ensure
1299
1249
  // that when this method fails, it reports the most salient error.
1300
- (0, _error_utils.invariant)(this._data, 'RecordQueryResult must not be deleted'); // Throws when the model has been deleted.
1250
+ var groups = this._data.groups; // Throws when the model has been deleted.
1301
1251
 
1302
1252
  (0, _error_utils.invariant)(this.isDataLoaded, 'RecordQueryResult data is not loaded');
1303
-
1304
- if (!this._orderedGroups) {
1305
- this._orderedGroups = this._generateAndLoadOrderedGroups();
1306
- }
1307
-
1308
- return (_this$_orderedGroups = this._orderedGroups) !== null && _this$_orderedGroups !== void 0 ? _this$_orderedGroups : null;
1253
+ return groups !== null && groups !== void 0 ? groups : null;
1309
1254
  }
1310
1255
  /**
1311
1256
  * The GroupLevels in this RecordQueryResult.
@@ -1318,15 +1263,10 @@ function (_RecordQueryResult) {
1318
1263
  }, {
1319
1264
  key: "groupLevels",
1320
1265
  get: function get() {
1321
- (0, _error_utils.invariant)(this._data, 'RecordQueryResult must not be deleted'); // Throws when the model has been deleted.
1266
+ var groupLevels = this._data.groupLevels; // Throws when the model has been deleted.
1322
1267
 
1323
1268
  (0, _error_utils.invariant)(this.isDataLoaded, 'RecordQueryResult data is not loaded');
1324
-
1325
- if (!this._loadedGroupLevels) {
1326
- this._loadedGroupLevels = this._sourceModelGroupLevels;
1327
- }
1328
-
1329
- return this._loadedGroupLevels ? this._loadedGroupLevels.map(singleLevel => _objectSpread({}, singleLevel, {
1269
+ return groupLevels ? groupLevels.map(singleLevel => _objectSpread({}, singleLevel, {
1330
1270
  field: this.parentTable.getFieldById(singleLevel.fieldId)
1331
1271
  })) : null;
1332
1272
  }
@@ -1341,13 +1281,13 @@ function (_RecordQueryResult) {
1341
1281
  // using RecordQueryResult) if the user had to manually watch for deletion
1342
1282
  // on all the fields and recreate the RecordQueryResult.
1343
1283
 
1344
- var _iteratorNormalCompletion13 = true;
1345
- var _didIteratorError13 = false;
1346
- var _iteratorError13 = undefined;
1284
+ var _iteratorNormalCompletion14 = true;
1285
+ var _didIteratorError14 = false;
1286
+ var _iteratorError14 = undefined;
1347
1287
 
1348
1288
  try {
1349
- for (var _iterator13 = fieldIdsOrNullIfAllFields[Symbol.iterator](), _step13; !(_iteratorNormalCompletion13 = (_step13 = _iterator13.next()).done); _iteratorNormalCompletion13 = true) {
1350
- var fieldId = _step13.value;
1289
+ for (var _iterator14 = fieldIdsOrNullIfAllFields[Symbol.iterator](), _step14; !(_iteratorNormalCompletion14 = (_step14 = _iterator14.next()).done); _iteratorNormalCompletion14 = true) {
1290
+ var fieldId = _step14.value;
1351
1291
 
1352
1292
  var field = this._table.getFieldByIdIfExists(fieldId);
1353
1293
 
@@ -1356,16 +1296,16 @@ function (_RecordQueryResult) {
1356
1296
  }
1357
1297
  }
1358
1298
  } catch (err) {
1359
- _didIteratorError13 = true;
1360
- _iteratorError13 = err;
1299
+ _didIteratorError14 = true;
1300
+ _iteratorError14 = err;
1361
1301
  } finally {
1362
1302
  try {
1363
- if (!_iteratorNormalCompletion13 && _iterator13.return != null) {
1364
- _iterator13.return();
1303
+ if (!_iteratorNormalCompletion14 && _iterator14.return != null) {
1304
+ _iterator14.return();
1365
1305
  }
1366
1306
  } finally {
1367
- if (_didIteratorError13) {
1368
- throw _iteratorError13;
1307
+ if (_didIteratorError14) {
1308
+ throw _iteratorError14;
1369
1309
  }
1370
1310
  }
1371
1311
  }
@@ -1419,12 +1359,6 @@ function (_RecordQueryResult) {
1419
1359
  get: function get() {
1420
1360
  return this._sourceModel instanceof _table.default ? this._recordStore.records : this._recordStore.getViewDataStore(this._sourceModel.id).visibleRecords;
1421
1361
  }
1422
- /**
1423
- * This is just a helper structure to better colocate watch and unwatch logic
1424
- *
1425
- * @internal
1426
- */
1427
-
1428
1362
  }]);
1429
1363
  return TableOrViewQueryResult;
1430
1364
  }(_record_query_result.default);