@airtable/blocks 1.9.0 → 1.10.2-experimental-640bd10-20220211

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 (96) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/dist/cjs/error_utils.js +44 -2
  3. package/dist/cjs/models/base.js +2 -1
  4. package/dist/cjs/models/cursor.js +2 -0
  5. package/dist/cjs/models/field.js +3 -13
  6. package/dist/cjs/models/grouped_record_query_result.js +5 -14
  7. package/dist/cjs/models/linked_records_query_result.js +78 -27
  8. package/dist/cjs/models/mutations.js +18 -162
  9. package/dist/cjs/models/query_manager.js +328 -0
  10. package/dist/cjs/models/record.js +311 -56
  11. package/dist/cjs/models/record_query_result.js +4 -1
  12. package/dist/cjs/models/record_store.js +557 -765
  13. package/dist/cjs/models/table.js +9 -8
  14. package/dist/cjs/models/table_or_view_query_result.js +526 -419
  15. package/dist/cjs/models/view.js +1 -1
  16. package/dist/cjs/models/view_data_store.js +255 -295
  17. package/dist/cjs/private_utils.js +40 -0
  18. package/dist/cjs/sdk.js +12 -2
  19. package/dist/cjs/testing/abstract_mock_airtable_interface.js +57 -11
  20. package/dist/cjs/testing/fixture_data.js +268 -0
  21. package/dist/cjs/testing/mock_base_data_stores.js +876 -0
  22. package/dist/cjs/types/airtable_interface.js +17 -2
  23. package/dist/cjs/types/block_query_spec.js +85 -0
  24. package/dist/cjs/ui/icon_config.js +4 -2
  25. package/dist/cjs/ui/use_global_config.js +1 -1
  26. package/dist/cjs/ui/use_records.js +5 -1
  27. package/dist/cjs/unstable_testing_utils.js +55 -1
  28. package/dist/cjs/watchable.js +15 -1
  29. package/dist/types/src/error_utils.d.ts +6 -0
  30. package/dist/types/src/error_utils.d.ts.map +1 -1
  31. package/dist/types/src/models/base.d.ts +2 -1
  32. package/dist/types/src/models/base.d.ts.map +1 -1
  33. package/dist/types/src/models/cursor.d.ts +2 -0
  34. package/dist/types/src/models/cursor.d.ts.map +1 -1
  35. package/dist/types/src/models/field.d.ts +3 -13
  36. package/dist/types/src/models/field.d.ts.map +1 -1
  37. package/dist/types/src/models/grouped_record_query_result.d.ts +3 -3
  38. package/dist/types/src/models/grouped_record_query_result.d.ts.map +1 -1
  39. package/dist/types/src/models/linked_records_query_result.d.ts.map +1 -1
  40. package/dist/types/src/models/mutations.d.ts.map +1 -1
  41. package/dist/types/src/models/query_manager.d.ts +2 -0
  42. package/dist/types/src/models/query_manager.d.ts.map +1 -0
  43. package/dist/types/src/models/record.d.ts +13 -4
  44. package/dist/types/src/models/record.d.ts.map +1 -1
  45. package/dist/types/src/models/record_query_result.d.ts +3 -2
  46. package/dist/types/src/models/record_query_result.d.ts.map +1 -1
  47. package/dist/types/src/models/record_store.d.ts.map +1 -1
  48. package/dist/types/src/models/table.d.ts +3 -2
  49. package/dist/types/src/models/table.d.ts.map +1 -1
  50. package/dist/types/src/models/table_or_view_query_result.d.ts +3 -5
  51. package/dist/types/src/models/table_or_view_query_result.d.ts.map +1 -1
  52. package/dist/types/src/models/view.d.ts +1 -1
  53. package/dist/types/src/models/view_data_store.d.ts +0 -1
  54. package/dist/types/src/models/view_data_store.d.ts.map +1 -1
  55. package/dist/types/src/models/view_metadata_query_result.d.ts +1 -1
  56. package/dist/types/src/models/view_metadata_query_result.d.ts.map +1 -1
  57. package/dist/types/src/private_utils.d.ts +26 -3
  58. package/dist/types/src/private_utils.d.ts.map +1 -1
  59. package/dist/types/src/sdk.d.ts.map +1 -1
  60. package/dist/types/src/testing/abstract_mock_airtable_interface.d.ts +11 -11
  61. package/dist/types/src/testing/abstract_mock_airtable_interface.d.ts.map +1 -1
  62. package/dist/types/{test/airtable_interface_mocks → src/testing}/fixture_data.d.ts +42 -19
  63. package/dist/types/src/testing/fixture_data.d.ts.map +1 -0
  64. package/dist/types/src/testing/mock_base_data_stores.d.ts +55 -0
  65. package/dist/types/src/testing/mock_base_data_stores.d.ts.map +1 -0
  66. package/dist/types/src/types/airtable_interface.d.ts +59 -20
  67. package/dist/types/src/types/airtable_interface.d.ts.map +1 -1
  68. package/dist/types/src/types/block_query_spec.d.ts +139 -0
  69. package/dist/types/src/types/block_query_spec.d.ts.map +1 -0
  70. package/dist/types/src/types/field.d.ts +7 -1
  71. package/dist/types/src/types/field.d.ts.map +1 -1
  72. package/dist/types/src/types/table.d.ts +0 -2
  73. package/dist/types/src/types/table.d.ts.map +1 -1
  74. package/dist/types/src/types/view.d.ts +3 -8
  75. package/dist/types/src/types/view.d.ts.map +1 -1
  76. package/dist/types/src/ui/icon_config.d.ts +5 -3
  77. package/dist/types/src/ui/icon_config.d.ts.map +1 -1
  78. package/dist/types/src/ui/link.d.ts +1 -1
  79. package/dist/types/src/ui/link.d.ts.map +1 -1
  80. package/dist/types/src/ui/record_card.d.ts +1 -1
  81. package/dist/types/src/ui/use_global_config.d.ts +1 -1
  82. package/dist/types/src/unstable_testing_utils.d.ts +4 -1
  83. package/dist/types/src/unstable_testing_utils.d.ts.map +1 -1
  84. package/dist/types/src/watchable.d.ts.map +1 -1
  85. package/dist/types/test/airtable_interface_mocks/linked_records.d.ts +1 -1
  86. package/dist/types/test/airtable_interface_mocks/linked_records.d.ts.map +1 -1
  87. package/dist/types/test/airtable_interface_mocks/mock_airtable_interface.d.ts +19 -12
  88. package/dist/types/test/airtable_interface_mocks/mock_airtable_interface.d.ts.map +1 -1
  89. package/dist/types/test/airtable_interface_mocks/project_tracker.d.ts +1 -1
  90. package/dist/types/test/airtable_interface_mocks/project_tracker.d.ts.map +1 -1
  91. package/dist/types/test/test_helpers.d.ts +2 -0
  92. package/dist/types/test/test_helpers.d.ts.map +1 -1
  93. package/dist/types/test/testing/fixture_data.test.d.ts +2 -0
  94. package/dist/types/test/testing/fixture_data.test.d.ts.map +1 -0
  95. package/package.json +3 -2
  96. package/dist/types/test/airtable_interface_mocks/fixture_data.d.ts.map +0 -1
@@ -6,18 +6,12 @@ require("core-js/modules/es.symbol");
6
6
 
7
7
  require("core-js/modules/es.symbol.description");
8
8
 
9
- require("core-js/modules/es.array.filter");
10
-
11
- require("core-js/modules/es.array.flat-map");
12
-
13
9
  require("core-js/modules/es.array.includes");
14
10
 
15
11
  require("core-js/modules/es.array.iterator");
16
12
 
17
13
  require("core-js/modules/es.array.map");
18
14
 
19
- require("core-js/modules/es.array.unscopables.flat-map");
20
-
21
15
  require("core-js/modules/es.object.to-string");
22
16
 
23
17
  require("core-js/modules/es.promise");
@@ -28,15 +22,11 @@ require("core-js/modules/es.string.includes");
28
22
 
29
23
  require("core-js/modules/web.dom-collections.iterator");
30
24
 
31
- require("core-js/modules/web.url.to-json");
32
-
33
25
  Object.defineProperty(exports, "__esModule", {
34
26
  value: true
35
27
  });
36
28
  exports.default = void 0;
37
29
 
38
- var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
39
-
40
30
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
41
31
 
42
32
  require("regenerator-runtime/runtime");
@@ -51,8 +41,6 @@ var _airtable_interface = require("../types/airtable_interface");
51
41
 
52
42
  var _mutations = require("../types/mutations");
53
43
 
54
- var _private_utils = require("../private_utils");
55
-
56
44
  var _error_utils = require("../error_utils");
57
45
 
58
46
  var _mutation_constants = require("./mutation_constants");
@@ -81,22 +69,18 @@ function () {
81
69
 
82
70
  /** @internal */
83
71
 
84
- /** @internal */
85
-
86
72
  /** @hidden */
87
- function Mutations(sdk, session, base, applyModelChanges, applyGlobalConfigUpdates) {
73
+ function Mutations(sdk, session, base, applyGlobalConfigUpdates) {
88
74
  (0, _classCallCheck2.default)(this, Mutations);
89
75
  (0, _defineProperty2.default)(this, "_airtableInterface", void 0);
90
76
  (0, _defineProperty2.default)(this, "_session", void 0);
91
77
  (0, _defineProperty2.default)(this, "_sdk", void 0);
92
78
  (0, _defineProperty2.default)(this, "_base", void 0);
93
- (0, _defineProperty2.default)(this, "_applyModelChanges", void 0);
94
79
  (0, _defineProperty2.default)(this, "_applyGlobalConfigUpdates", void 0);
95
80
  this._airtableInterface = sdk.__airtableInterface;
96
81
  this._session = session;
97
82
  this._sdk = sdk;
98
83
  this._base = base;
99
- this._applyModelChanges = applyModelChanges;
100
84
  this._applyGlobalConfigUpdates = applyGlobalConfigUpdates;
101
85
  }
102
86
  /** @hidden */
@@ -270,9 +254,12 @@ function () {
270
254
  try {
271
255
  for (var _iterator = records[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
272
256
  var record = _step.value;
273
- var existingRecord = null;
257
+ var existingRecord = null; // Note - if isSubscribedToAllRecordsInTable is true then we know the record
258
+ // doesn't exist (or is deleted) but otherwise we can issue a mutation to a record
259
+ // that does not exist, having to deal with the error thrown by PublicAirtableInterfaceFrontend
260
+ // TODO: (#proj-blocks-sdk-record-limits) Write tests around this behavior
274
261
 
275
- if (recordStore.isRecordMetadataLoaded) {
262
+ if (recordStore.isSubscribedToAllRecordsInTable || recordStore.isRecordLoaded(record.id)) {
276
263
  existingRecord = recordStore.getRecordByIdIfExists(record.id);
277
264
 
278
265
  if (!existingRecord) {
@@ -294,7 +281,7 @@ function () {
294
281
  checkedFieldIds.add(fieldId);
295
282
  }
296
283
 
297
- if (existingRecord && recordStore.areCellValuesLoadedForFieldId(fieldId)) {
284
+ if (existingRecord && recordStore.isRecordCellValueLoadedForFieldId(record.id, fieldId)) {
298
285
  var validationResult = this._airtableInterface.fieldTypeProvider.validateCellValueForUpdate(appInterface, record.cellValuesByFieldId[fieldId], existingRecord._getRawCellValue(field), field._data);
299
286
 
300
287
  if (!validationResult.isValid) {
@@ -332,9 +319,15 @@ function () {
332
319
  throw (0, _error_utils.spawnError)("Can't delete records: No table with id %s exists", _tableId);
333
320
  }
334
321
 
335
- var _recordStore = this._base.__getRecordStore(_tableId);
322
+ var _recordStore = this._base.__getRecordStore(_tableId); // TODO: (#proj-blocks-sdk-record-limits) We substantially weaken our check here
323
+ // because we can only be sure if the record exists inside the SDK if we've loaded
324
+ // all records. It is possible to change this logic to ensure we don't double-delete
325
+ // records by caching previously deleted records (if the delete is undone, but
326
+ // we're not watching the record be re-created from the undo we would have to fail
327
+ // the delete if we detect double deletion). TODO to add tests around this behavior.
328
+
336
329
 
337
- if (_recordStore.isRecordMetadataLoaded) {
330
+ if (_recordStore.isSubscribedToAllRecordsInTable) {
338
331
  var _iteratorNormalCompletion2 = true;
339
332
  var _didIteratorError2 = false;
340
333
  var _iteratorError2 = undefined;
@@ -672,150 +665,13 @@ function () {
672
665
  this._applyGlobalConfigUpdates(mutation.updates);
673
666
 
674
667
  return true;
675
- }
676
-
677
- var modelChanges = this._getOptimisticModelChangesForMutation(mutation);
668
+ } // https://airtable.com/appahuJQw7qfQTBBU/tblSbpimM8Spp9txp/viweAFIus3MhU9Gxa/recHOckY4V9QmM6JV?blocks=hide
669
+ // TODO: (#proj-blocks-sdk-record-limits) Fix this in a future stacked diff where optimistic
670
+ // updates are re-enabled https://docs.google.com/document/d/1CVZ4ZoPWEtpzKq_9cRLASGwgY71WI4nl6RVvs8S7Ubw/edit#
678
671
 
679
- if (modelChanges.length > 0) {
680
- this._applyModelChanges(modelChanges);
681
-
682
- return true;
683
- }
684
672
 
685
673
  return false;
686
674
  }
687
- /** @internal */
688
-
689
- }, {
690
- key: "_getOptimisticModelChangesForMutation",
691
- value: function _getOptimisticModelChangesForMutation(mutation) {
692
- switch (mutation.type) {
693
- case _mutations.MutationTypes.SET_MULTIPLE_RECORDS_CELL_VALUES:
694
- {
695
- var tableId = mutation.tableId,
696
- records = mutation.records;
697
-
698
- var recordStore = this._base.__getRecordStore(tableId);
699
-
700
- return records.flatMap(record => Object.keys(record.cellValuesByFieldId).filter(fieldId => recordStore.areCellValuesLoadedForFieldId(fieldId)).map(fieldId => ({
701
- path: ['tablesById', tableId, 'recordsById', record.id, 'cellValuesByFieldId', fieldId],
702
- value: record.cellValuesByFieldId[fieldId]
703
- })));
704
- }
705
-
706
- case _mutations.MutationTypes.DELETE_MULTIPLE_RECORDS:
707
- {
708
- var _tableId7 = mutation.tableId,
709
- recordIds = mutation.recordIds;
710
-
711
- var _recordStore2 = this._base.__getRecordStore(_tableId7);
712
-
713
- if (!_recordStore2.isRecordMetadataLoaded) {
714
- return [];
715
- }
716
-
717
- return [...recordIds.map(recordId => ({
718
- path: ['tablesById', _tableId7, 'recordsById', recordId],
719
- value: undefined
720
- })), ...this._base.getTableById(_tableId7).views.flatMap(view => {
721
- var viewDataStore = _recordStore2.getViewDataStore(view.id);
722
-
723
- if (!viewDataStore.isDataLoaded) {
724
- return [];
725
- }
726
-
727
- return viewDataStore.__generateChangesForParentTableDeleteMultipleRecords(recordIds);
728
- })];
729
- }
730
-
731
- case _mutations.MutationTypes.CREATE_MULTIPLE_RECORDS:
732
- {
733
- var _tableId8 = mutation.tableId,
734
- _records2 = mutation.records;
735
-
736
- var _recordStore3 = this._base.__getRecordStore(_tableId8);
737
-
738
- if (!_recordStore3.isRecordMetadataLoaded) {
739
- return [];
740
- }
741
-
742
- return [..._records2.map(record => {
743
- // Only apply optimistic changes for fields that are loaded
744
- var filteredCellValuesByFieldId = {};
745
- var _iteratorNormalCompletion5 = true;
746
- var _didIteratorError5 = false;
747
- var _iteratorError5 = undefined;
748
-
749
- try {
750
- for (var _iterator5 = (0, _private_utils.entries)(record.cellValuesByFieldId)[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
751
- var _step5$value = (0, _slicedToArray2.default)(_step5.value, 2),
752
- fieldId = _step5$value[0],
753
- cellValue = _step5$value[1];
754
-
755
- if (_recordStore3.areCellValuesLoadedForFieldId(fieldId)) {
756
- filteredCellValuesByFieldId[fieldId] = cellValue;
757
- }
758
- }
759
- } catch (err) {
760
- _didIteratorError5 = true;
761
- _iteratorError5 = err;
762
- } finally {
763
- try {
764
- if (!_iteratorNormalCompletion5 && _iterator5.return != null) {
765
- _iterator5.return();
766
- }
767
- } finally {
768
- if (_didIteratorError5) {
769
- throw _iteratorError5;
770
- }
771
- }
772
- }
773
-
774
- return {
775
- path: ['tablesById', _tableId8, 'recordsById', record.id],
776
- value: {
777
- id: record.id,
778
- cellValuesByFieldId: filteredCellValuesByFieldId,
779
- commentCount: 0,
780
- createdTime: new Date().toJSON()
781
- }
782
- };
783
- }), ...this._base.getTableById(_tableId8).views.flatMap(view => {
784
- var viewDataStore = _recordStore3.getViewDataStore(view.id);
785
-
786
- if (!viewDataStore.isDataLoaded) {
787
- return [];
788
- }
789
-
790
- return viewDataStore.__generateChangesForParentTableAddMultipleRecords(_records2.map(record => record.id));
791
- })];
792
- }
793
- // The following branch is unreachable because this method's only
794
- // call site is preceded by an explicit guard for this condition.
795
- // istanbul ignore next
796
-
797
- case _mutations.MutationTypes.SET_MULTIPLE_GLOBAL_CONFIG_PATHS:
798
- {
799
- throw (0, _error_utils.spawnError)('attempting to generate model updates for SET_MULTIPLE_GLOBAL_CONFIG_PATH');
800
- }
801
-
802
- case _mutations.MutationTypes.CREATE_SINGLE_FIELD:
803
- case _mutations.MutationTypes.UPDATE_SINGLE_FIELD_CONFIG:
804
- case _mutations.MutationTypes.UPDATE_SINGLE_FIELD_DESCRIPTION:
805
- case _mutations.MutationTypes.UPDATE_VIEW_METADATA:
806
- case _mutations.MutationTypes.CREATE_SINGLE_TABLE:
807
- {
808
- // No optimistic updates for field, view metadata, or table mutations.
809
- return [];
810
- }
811
- // The following branch is unreachable because this method's only
812
- // call site is preceded by an explicit guard for this condition.
813
- // istanbul ignore next
814
-
815
- default:
816
- throw (0, _error_utils.spawnUnknownSwitchCaseError)('mutation type', mutation, 'type');
817
- }
818
- }
819
675
  }]);
820
676
  return Mutations;
821
677
  }();
@@ -0,0 +1,328 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ require("core-js/modules/es.symbol");
6
+
7
+ require("core-js/modules/es.symbol.description");
8
+
9
+ require("core-js/modules/es.array.iterator");
10
+
11
+ require("core-js/modules/es.map");
12
+
13
+ require("core-js/modules/es.object.to-string");
14
+
15
+ require("core-js/modules/es.promise");
16
+
17
+ require("core-js/modules/web.dom-collections.for-each");
18
+
19
+ require("core-js/modules/web.dom-collections.iterator");
20
+
21
+ Object.defineProperty(exports, "__esModule", {
22
+ value: true
23
+ });
24
+ exports.QueryManager = void 0;
25
+
26
+ var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
27
+
28
+ require("regenerator-runtime/runtime");
29
+
30
+ var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
31
+
32
+ var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
33
+
34
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
35
+
36
+ var _error_utils = require("../error_utils");
37
+
38
+ var _private_utils = require("../private_utils");
39
+
40
+ /**
41
+ * This provides a singleton instance which is responsible for all queries sent
42
+ * to PublicAirtableInterface.
43
+ *
44
+ * This can allow looking up queries by queryId.
45
+ *
46
+ * @internal
47
+ */
48
+ var QueryManager =
49
+ /*#__PURE__*/
50
+ function () {
51
+ function QueryManager(sdk) {
52
+ (0, _classCallCheck2.default)(this, QueryManager);
53
+ (0, _defineProperty2.default)(this, "_airtableInterface", void 0);
54
+ (0, _defineProperty2.default)(this, "_sdk", void 0);
55
+ (0, _defineProperty2.default)(this, "_queryRegistrationInfosByWatchKey", new Map());
56
+ (0, _defineProperty2.default)(this, "fetchAndSubscribeToQueryAsync", (query, querySubscriptionOptions) => {
57
+ var queryResponse;
58
+ return _regenerator.default.async(function _callee$(_context) {
59
+ while (1) {
60
+ switch (_context.prev = _context.next) {
61
+ case 0:
62
+ _context.next = 2;
63
+ return _regenerator.default.awrap(this._airtableInterface.subscribeToQueryAndPopulateStoresAsync(query, querySubscriptionOptions !== null && querySubscriptionOptions !== void 0 ? querySubscriptionOptions : {}));
64
+
65
+ case 2:
66
+ queryResponse = _context.sent;
67
+
68
+ if (queryResponse.success) {
69
+ _context.next = 5;
70
+ break;
71
+ }
72
+
73
+ throw (0, _error_utils.spawnError)('Subscription to query failed - unhandled error');
74
+
75
+ case 5:
76
+ return _context.abrupt("return", queryResponse.blockQueryId);
77
+
78
+ case 6:
79
+ case "end":
80
+ return _context.stop();
81
+ }
82
+ }
83
+ }, null, this);
84
+ });
85
+ (0, _defineProperty2.default)(this, "unsubscribeFromQueryIds", queryIds => {
86
+ var _iteratorNormalCompletion = true;
87
+ var _didIteratorError = false;
88
+ var _iteratorError = undefined;
89
+
90
+ try {
91
+ for (var _iterator = queryIds[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
92
+ var queryId = _step.value;
93
+ // TODO: (#proj-blocks-sdk-record-limits) Add support for unsubscribing multiple at the same time
94
+ this.unsubscribeFromQueryId(queryId);
95
+ }
96
+ } catch (err) {
97
+ _didIteratorError = true;
98
+ _iteratorError = err;
99
+ } finally {
100
+ try {
101
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
102
+ _iterator.return();
103
+ }
104
+ } finally {
105
+ if (_didIteratorError) {
106
+ throw _iteratorError;
107
+ }
108
+ }
109
+ }
110
+ });
111
+ (0, _defineProperty2.default)(this, "unsubscribeFromQueryId", queryId => {
112
+ (0, _private_utils.fireAndForgetPromise)(() => this._airtableInterface.unsubscribeFromQueryIdsAsync([queryId]));
113
+ });
114
+ (0, _defineProperty2.default)(this, "watchWithQuerySpec", (callbackPriority, query, watchableModel, watchKey, watchOnChangeCallback, callbackForRegistration, context) => {
115
+ var isOnChangeCallbackCanceled = false;
116
+
117
+ var callbackForCancelOnChange = () => isOnChangeCallbackCanceled = true;
118
+
119
+ var onChangeCallback = changes => {
120
+ if (isOnChangeCallbackCanceled) {
121
+ return;
122
+ }
123
+
124
+ watchableModel.__incrementOnChangeCount();
125
+
126
+ watchOnChangeCallback(changes);
127
+ };
128
+
129
+ var blockQueryIdPromise = this.fetchAndSubscribeToQueryAsync(query, {
130
+ onChangeCallback,
131
+ callbackPriority
132
+ });
133
+
134
+ var queryRegistrationInfosForKey = this._queryRegistrationInfosByWatchKey.get(watchKey);
135
+
136
+ if (!queryRegistrationInfosForKey) {
137
+ queryRegistrationInfosForKey = [];
138
+
139
+ this._queryRegistrationInfosByWatchKey.set(watchKey, queryRegistrationInfosForKey);
140
+ } // It is safe to push onto queryRegistrationInfosForKey (vs copying) as long
141
+ // as we don't release the event loop when we iterate over it, which only
142
+ // happens in unwatchFromQueryKeys
143
+
144
+
145
+ queryRegistrationInfosForKey.push({
146
+ blockQueryIdPromise,
147
+ callbackForCancelOnChange,
148
+ callbackForRegistration,
149
+ context,
150
+ onChangeCallback,
151
+ watchableModel
152
+ });
153
+ });
154
+ (0, _defineProperty2.default)(this, "_unwatchFromQueryRegistrationInfoAsync", queryRegistrationInfo => {
155
+ var blockQueryIdPromise, callbackForCancelOnChange, queryId;
156
+ return _regenerator.default.async(function _callee2$(_context2) {
157
+ while (1) {
158
+ switch (_context2.prev = _context2.next) {
159
+ case 0:
160
+ blockQueryIdPromise = queryRegistrationInfo.blockQueryIdPromise, callbackForCancelOnChange = queryRegistrationInfo.callbackForCancelOnChange;
161
+ callbackForCancelOnChange();
162
+ _context2.next = 4;
163
+ return _regenerator.default.awrap(blockQueryIdPromise);
164
+
165
+ case 4:
166
+ queryId = _context2.sent;
167
+ this.unsubscribeFromQueryId(queryId);
168
+
169
+ case 6:
170
+ case "end":
171
+ return _context2.stop();
172
+ }
173
+ }
174
+ }, null, this);
175
+ });
176
+ (0, _defineProperty2.default)(this, "unwatchFromQueryKey", (watchKey, callbackForRegistration, context) => {
177
+ var queryRegistrationInfos = this._queryRegistrationInfosByWatchKey.get(watchKey);
178
+
179
+ if (queryRegistrationInfos) {
180
+ var watchersToUnwatch = [];
181
+ var watchersToKeep = [];
182
+ queryRegistrationInfos.forEach(watcher => {
183
+ var isCallbackToUnwatch = watcher.callbackForRegistration === callbackForRegistration && watcher.context === context;
184
+
185
+ if (isCallbackToUnwatch) {
186
+ watchersToUnwatch.push(watcher);
187
+ } else {
188
+ watchersToKeep.push(watcher);
189
+ }
190
+ });
191
+
192
+ if (watchersToKeep.length > 0) {
193
+ this._queryRegistrationInfosByWatchKey.set(watchKey, watchersToKeep);
194
+ } else {
195
+ this._queryRegistrationInfosByWatchKey.delete(watchKey);
196
+ }
197
+
198
+ if (watchersToUnwatch.length > 0) {
199
+ // Do not await the response since unwatch is not async
200
+ watchersToUnwatch.forEach(this._unwatchFromQueryRegistrationInfoAsync);
201
+ }
202
+ }
203
+ });
204
+ this._airtableInterface = sdk.__airtableInterface;
205
+ this._sdk = sdk;
206
+ }
207
+
208
+ (0, _createClass2.default)(QueryManager, [{
209
+ key: "_onChange",
210
+ value: function _onChange(watchableModel, watchKey) {
211
+ var queryRegistrationInfos = this._queryRegistrationInfosByWatchKey.get(watchKey);
212
+
213
+ if (!queryRegistrationInfos) {
214
+ return;
215
+ }
216
+
217
+ var _iteratorNormalCompletion2 = true;
218
+ var _didIteratorError2 = false;
219
+ var _iteratorError2 = undefined;
220
+
221
+ try {
222
+ for (var _iterator2 = queryRegistrationInfos[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
223
+ var watcher = _step2.value;
224
+
225
+ if (watcher.watchableModel === watchableModel) {
226
+ watcher.onChangeCallback([]);
227
+ }
228
+ }
229
+ } catch (err) {
230
+ _didIteratorError2 = true;
231
+ _iteratorError2 = err;
232
+ } finally {
233
+ try {
234
+ if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
235
+ _iterator2.return();
236
+ }
237
+ } finally {
238
+ if (_didIteratorError2) {
239
+ throw _iteratorError2;
240
+ }
241
+ }
242
+ }
243
+ }
244
+ }, {
245
+ key: "onLoadKeys",
246
+ value: function onLoadKeys(watchableModel, watchKeys) {
247
+ var _iteratorNormalCompletion3 = true;
248
+ var _didIteratorError3 = false;
249
+ var _iteratorError3 = undefined;
250
+
251
+ try {
252
+ for (var _iterator3 = watchKeys[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
253
+ var key = _step3.value;
254
+
255
+ this._onChange(watchableModel, key);
256
+ }
257
+ } catch (err) {
258
+ _didIteratorError3 = true;
259
+ _iteratorError3 = err;
260
+ } finally {
261
+ try {
262
+ if (!_iteratorNormalCompletion3 && _iterator3.return != null) {
263
+ _iterator3.return();
264
+ }
265
+ } finally {
266
+ if (_didIteratorError3) {
267
+ throw _iteratorError3;
268
+ }
269
+ }
270
+ }
271
+ }
272
+ }, {
273
+ key: "forceUnloadWatchesOnWatchableModel",
274
+ value: function forceUnloadWatchesOnWatchableModel(watchableModel) {
275
+ var _iteratorNormalCompletion4 = true;
276
+ var _didIteratorError4 = false;
277
+ var _iteratorError4 = undefined;
278
+
279
+ try {
280
+ for (var _iterator4 = this._queryRegistrationInfosByWatchKey.values()[Symbol.iterator](), _step4; !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) {
281
+ var queryRegistrationInfos = _step4.value;
282
+ var _iteratorNormalCompletion5 = true;
283
+ var _didIteratorError5 = false;
284
+ var _iteratorError5 = undefined;
285
+
286
+ try {
287
+ for (var _iterator5 = queryRegistrationInfos[Symbol.iterator](), _step5; !(_iteratorNormalCompletion5 = (_step5 = _iterator5.next()).done); _iteratorNormalCompletion5 = true) {
288
+ var watcher = _step5.value;
289
+
290
+ if (watcher.watchableModel === watchableModel) {
291
+ this._unwatchFromQueryRegistrationInfoAsync(watcher);
292
+ }
293
+ }
294
+ } catch (err) {
295
+ _didIteratorError5 = true;
296
+ _iteratorError5 = err;
297
+ } finally {
298
+ try {
299
+ if (!_iteratorNormalCompletion5 && _iterator5.return != null) {
300
+ _iterator5.return();
301
+ }
302
+ } finally {
303
+ if (_didIteratorError5) {
304
+ throw _iteratorError5;
305
+ }
306
+ }
307
+ }
308
+ }
309
+ } catch (err) {
310
+ _didIteratorError4 = true;
311
+ _iteratorError4 = err;
312
+ } finally {
313
+ try {
314
+ if (!_iteratorNormalCompletion4 && _iterator4.return != null) {
315
+ _iterator4.return();
316
+ }
317
+ } finally {
318
+ if (_didIteratorError4) {
319
+ throw _iteratorError4;
320
+ }
321
+ }
322
+ }
323
+ }
324
+ }]);
325
+ return QueryManager;
326
+ }();
327
+
328
+ exports.QueryManager = QueryManager;