@atlaskit/editor-synced-block-provider 2.2.3 → 2.3.0

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 (29) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cjs/hooks/useFetchSyncBlockData.js +5 -30
  3. package/dist/cjs/providers/syncBlockProvider.js +23 -4
  4. package/dist/cjs/store-manager/referenceSyncBlockStoreManager.js +319 -63
  5. package/dist/cjs/store-manager/syncBlockStoreManager.js +27 -4
  6. package/dist/cjs/utils/mergeFetchSyncBlockDataResult.js +38 -0
  7. package/dist/es2019/hooks/useFetchSyncBlockData.js +6 -31
  8. package/dist/es2019/providers/syncBlockProvider.js +17 -3
  9. package/dist/es2019/store-manager/referenceSyncBlockStoreManager.js +172 -44
  10. package/dist/es2019/store-manager/syncBlockStoreManager.js +21 -4
  11. package/dist/es2019/utils/mergeFetchSyncBlockDataResult.js +30 -0
  12. package/dist/esm/hooks/useFetchSyncBlockData.js +6 -31
  13. package/dist/esm/providers/syncBlockProvider.js +21 -2
  14. package/dist/esm/store-manager/referenceSyncBlockStoreManager.js +320 -64
  15. package/dist/esm/store-manager/syncBlockStoreManager.js +27 -4
  16. package/dist/esm/utils/mergeFetchSyncBlockDataResult.js +31 -0
  17. package/dist/types/common/schema.d.ts +1 -1
  18. package/dist/types/providers/syncBlockProvider.d.ts +1 -1
  19. package/dist/types/providers/types.d.ts +1 -0
  20. package/dist/types/store-manager/referenceSyncBlockStoreManager.d.ts +24 -8
  21. package/dist/types/store-manager/syncBlockStoreManager.d.ts +12 -4
  22. package/dist/types/utils/mergeFetchSyncBlockDataResult.d.ts +12 -0
  23. package/dist/types-ts4.5/common/schema.d.ts +1 -1
  24. package/dist/types-ts4.5/providers/syncBlockProvider.d.ts +1 -1
  25. package/dist/types-ts4.5/providers/types.d.ts +1 -0
  26. package/dist/types-ts4.5/store-manager/referenceSyncBlockStoreManager.d.ts +24 -8
  27. package/dist/types-ts4.5/store-manager/syncBlockStoreManager.d.ts +12 -4
  28. package/dist/types-ts4.5/utils/mergeFetchSyncBlockDataResult.d.ts +12 -0
  29. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,12 @@
1
1
  # @atlaskit/editor-synced-block-provider
2
2
 
3
+ ## 2.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [`6f9f13ab4687d`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/6f9f13ab4687d) -
8
+ EDITOR-2451 update sync-block refreshed logic based on experience principles
9
+
3
10
  ## 2.2.3
4
11
 
5
12
  ### Patch Changes
@@ -7,44 +7,19 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.useFetchSyncBlockData = exports.SYNC_BLOCK_FETCH_INTERVAL = void 0;
8
8
  var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
9
9
  var _react = require("react");
10
- var _types = require("../common/types");
11
10
  var SYNC_BLOCK_FETCH_INTERVAL = exports.SYNC_BLOCK_FETCH_INTERVAL = 3000;
12
11
  var useFetchSyncBlockData = exports.useFetchSyncBlockData = function useFetchSyncBlockData(manager, syncBlockNode) {
13
12
  var _useState = (0, _react.useState)(null),
14
13
  _useState2 = (0, _slicedToArray2.default)(_useState, 2),
15
14
  fetchSyncBlockDataResult = _useState2[0],
16
15
  setFetchSyncBlockDataResult = _useState2[1];
17
- var fetchSyncBlockNode = (0, _react.useCallback)(function () {
18
- manager.fetchSyncBlockData(syncBlockNode).then(function (data) {
19
- if (data !== null && data !== void 0 && data.error) {
20
- // if there is an error, we don't want to replace real existing data with the error data
21
- setFetchSyncBlockDataResult(function (prev) {
22
- if (!prev || prev.error) {
23
- return data;
24
- }
25
- return prev;
26
- });
27
- } else {
28
- setFetchSyncBlockDataResult(data !== null && data !== void 0 ? data : null);
29
- }
30
- }).catch(function () {
31
- //TODO: EDITOR-1921 - add error analytics
32
- setFetchSyncBlockDataResult(function (prev) {
33
- if (!prev || prev.error) {
34
- return {
35
- error: _types.SyncBlockError.Errored
36
- };
37
- }
38
- return prev;
39
- });
40
- });
41
- }, [manager, syncBlockNode]);
42
16
  (0, _react.useEffect)(function () {
43
- fetchSyncBlockNode();
44
- var interval = window.setInterval(fetchSyncBlockNode, SYNC_BLOCK_FETCH_INTERVAL);
17
+ var unsubscribe = manager.subscribeToSyncBlockData(syncBlockNode, function (data) {
18
+ setFetchSyncBlockDataResult(data);
19
+ });
45
20
  return function () {
46
- window.clearInterval(interval);
21
+ unsubscribe();
47
22
  };
48
- }, [fetchSyncBlockNode]);
23
+ }, [manager, setFetchSyncBlockDataResult, syncBlockNode]);
49
24
  return fetchSyncBlockDataResult;
50
25
  };
@@ -7,6 +7,7 @@ Object.defineProperty(exports, "__esModule", {
7
7
  exports.useMemoizedSyncedBlockProvider = exports.SyncBlockProvider = void 0;
8
8
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
9
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
10
+ var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
10
11
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
11
12
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
12
13
  var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
@@ -14,7 +15,8 @@ var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/ge
14
15
  var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
15
16
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
16
17
  var _react = require("react");
17
- var _types = require("../providers/types");
18
+ var _types = require("../common/types");
19
+ var _types2 = require("../providers/types");
18
20
  var _ari = require("../utils/ari");
19
21
  function _callSuper(t, o, e) { return o = (0, _getPrototypeOf2.default)(o), (0, _possibleConstructorReturn2.default)(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], (0, _getPrototypeOf2.default)(t).constructor) : o.apply(t, e)); }
20
22
  function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function _isNativeReflectConstruct() { return !!t; })(); }
@@ -44,9 +46,26 @@ var SyncBlockProvider = exports.SyncBlockProvider = /*#__PURE__*/function (_Sync
44
46
  key: "fetchNodesData",
45
47
  value: function fetchNodesData(nodes) {
46
48
  var _this2 = this;
47
- return Promise.all(nodes.map(function (node) {
48
- return _this2.fetchProvider.fetchData(node.attrs.resourceId);
49
+ var resourceIdSet = new Set(nodes.map(function (node) {
50
+ return node.attrs.resourceId;
49
51
  }));
52
+ var resourceIds = (0, _toConsumableArray2.default)(resourceIdSet);
53
+ return Promise.allSettled(resourceIds.map(function (resourceId) {
54
+ return _this2.fetchProvider.fetchData(resourceId).then(function (data) {
55
+ return data;
56
+ }, function () {
57
+ return {
58
+ status: _types.SyncBlockError.Errored,
59
+ resourceId: resourceId
60
+ };
61
+ });
62
+ })).then(function (results) {
63
+ return results.filter(function (result) {
64
+ return result.status === 'fulfilled';
65
+ }).map(function (result) {
66
+ return result.value;
67
+ });
68
+ });
50
69
  }
51
70
 
52
71
  /**
@@ -97,7 +116,7 @@ var SyncBlockProvider = exports.SyncBlockProvider = /*#__PURE__*/function (_Sync
97
116
  return pageARI ? fetchURLfromARI(pageARI, sourceLocalId) : Promise.resolve(undefined);
98
117
  }
99
118
  }]);
100
- }(_types.SyncBlockDataProvider);
119
+ }(_types2.SyncBlockDataProvider);
101
120
  var useMemoizedSyncedBlockProvider = exports.useMemoizedSyncedBlockProvider = function useMemoizedSyncedBlockProvider(fetchProvider, writeProvider, sourceId) {
102
121
  return (0, _react.useMemo)(function () {
103
122
  return new SyncBlockProvider(fetchProvider, writeProvider, sourceId);
@@ -6,12 +6,17 @@ Object.defineProperty(exports, "__esModule", {
6
6
  });
7
7
  exports.ReferenceSyncBlockStoreManager = void 0;
8
8
  var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));
9
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
10
10
  var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));
11
11
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
12
12
  var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
13
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
14
+ var _mergeFetchSyncBlockDataResult = require("../utils/mergeFetchSyncBlockDataResult");
13
15
  function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
14
16
  function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
17
+ function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t.return || t.return(); } finally { if (u) throw o; } } }; }
18
+ function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
19
+ function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
15
20
  var createSyncBlockNode = function createSyncBlockNode(localId, resourceId) {
16
21
  return {
17
22
  type: 'syncBlock',
@@ -24,108 +29,359 @@ var createSyncBlockNode = function createSyncBlockNode(localId, resourceId) {
24
29
  var ReferenceSyncBlockStoreManager = exports.ReferenceSyncBlockStoreManager = /*#__PURE__*/function () {
25
30
  function ReferenceSyncBlockStoreManager(dataProvider) {
26
31
  (0, _classCallCheck2.default)(this, ReferenceSyncBlockStoreManager);
27
- this.dataProvider = dataProvider;
32
+ (0, _defineProperty2.default)(this, "isInitialized", false);
33
+ (0, _defineProperty2.default)(this, "isRefreshingSubscriptions", false);
28
34
  this.syncBlockCache = new Map();
35
+ this.subscriptions = new Map();
36
+ this.dataProvider = dataProvider;
29
37
  this.syncBlockURLRequests = new Map();
30
38
  }
31
-
32
- /**
33
- *
34
- * @param localId - The local ID of the sync block to get the source URL for
35
- * @param resourceId - The resource ID of the sync block to get the source URL for
36
- * Fetches source URl for a sync block and updates sync block data with the source URL asynchronously.
37
- */
38
39
  return (0, _createClass2.default)(ReferenceSyncBlockStoreManager, [{
40
+ key: "init",
41
+ value: function () {
42
+ var _init = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(editorView) {
43
+ var _this = this;
44
+ var syncBlockNodes, dataResults;
45
+ return _regenerator.default.wrap(function _callee$(_context) {
46
+ while (1) switch (_context.prev = _context.next) {
47
+ case 0:
48
+ if (!(!this.editorView && !this.isInitialized)) {
49
+ _context.next = 15;
50
+ break;
51
+ }
52
+ this.editorView = editorView;
53
+ syncBlockNodes = editorView.state.doc.children.filter(function (node) {
54
+ return node.type.name === 'syncBlock';
55
+ }).map(function (node) {
56
+ return node.toJSON();
57
+ }) || [];
58
+ if (!(syncBlockNodes.length > 0)) {
59
+ _context.next = 15;
60
+ break;
61
+ }
62
+ _context.prev = 4;
63
+ _context.next = 7;
64
+ return this.fetchSyncBlocksData(syncBlockNodes);
65
+ case 7:
66
+ dataResults = _context.sent;
67
+ if (dataResults) {
68
+ _context.next = 10;
69
+ break;
70
+ }
71
+ throw new Error('No data results returned when initializing sync block store manager');
72
+ case 10:
73
+ dataResults.forEach(function (dataResult) {
74
+ _this.updateCache(dataResult);
75
+ });
76
+ _context.next = 15;
77
+ break;
78
+ case 13:
79
+ _context.prev = 13;
80
+ _context.t0 = _context["catch"](4);
81
+ case 15:
82
+ this.isInitialized = true;
83
+ case 16:
84
+ case "end":
85
+ return _context.stop();
86
+ }
87
+ }, _callee, this, [[4, 13]]);
88
+ }));
89
+ function init(_x) {
90
+ return _init.apply(this, arguments);
91
+ }
92
+ return init;
93
+ }()
94
+ /**
95
+ * Refreshes the subscriptions for all sync blocks.
96
+ * @returns {Promise<void>}
97
+ */
98
+ }, {
99
+ key: "refreshSubscriptions",
100
+ value: (function () {
101
+ var _refreshSubscriptions = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() {
102
+ var syncBlocks, _iterator, _step, _loop;
103
+ return _regenerator.default.wrap(function _callee2$(_context3) {
104
+ while (1) switch (_context3.prev = _context3.next) {
105
+ case 0:
106
+ if (!(this.isRefreshingSubscriptions || !this.isInitialized)) {
107
+ _context3.next = 2;
108
+ break;
109
+ }
110
+ return _context3.abrupt("return");
111
+ case 2:
112
+ this.isRefreshingSubscriptions = true;
113
+ syncBlocks = [];
114
+ _iterator = _createForOfIteratorHelper(this.subscriptions.entries());
115
+ _context3.prev = 5;
116
+ _loop = /*#__PURE__*/_regenerator.default.mark(function _loop() {
117
+ var _step$value, resourceId, callbacks;
118
+ return _regenerator.default.wrap(function _loop$(_context2) {
119
+ while (1) switch (_context2.prev = _context2.next) {
120
+ case 0:
121
+ _step$value = (0, _slicedToArray2.default)(_step.value, 2), resourceId = _step$value[0], callbacks = _step$value[1];
122
+ Object.keys(callbacks).forEach(function (localId) {
123
+ syncBlocks.push(createSyncBlockNode(localId, resourceId));
124
+ });
125
+ case 2:
126
+ case "end":
127
+ return _context2.stop();
128
+ }
129
+ }, _loop);
130
+ });
131
+ _iterator.s();
132
+ case 8:
133
+ if ((_step = _iterator.n()).done) {
134
+ _context3.next = 12;
135
+ break;
136
+ }
137
+ return _context3.delegateYield(_loop(), "t0", 10);
138
+ case 10:
139
+ _context3.next = 8;
140
+ break;
141
+ case 12:
142
+ _context3.next = 17;
143
+ break;
144
+ case 14:
145
+ _context3.prev = 14;
146
+ _context3.t1 = _context3["catch"](5);
147
+ _iterator.e(_context3.t1);
148
+ case 17:
149
+ _context3.prev = 17;
150
+ _iterator.f();
151
+ return _context3.finish(17);
152
+ case 20:
153
+ _context3.prev = 20;
154
+ _context3.next = 23;
155
+ return this.fetchSyncBlocksData(syncBlocks);
156
+ case 23:
157
+ _context3.next = 27;
158
+ break;
159
+ case 25:
160
+ _context3.prev = 25;
161
+ _context3.t2 = _context3["catch"](20);
162
+ case 27:
163
+ _context3.prev = 27;
164
+ this.isRefreshingSubscriptions = false;
165
+ return _context3.finish(27);
166
+ case 30:
167
+ case "end":
168
+ return _context3.stop();
169
+ }
170
+ }, _callee2, this, [[5, 14, 17, 20], [20, 25, 27, 30]]);
171
+ }));
172
+ function refreshSubscriptions() {
173
+ return _refreshSubscriptions.apply(this, arguments);
174
+ }
175
+ return refreshSubscriptions;
176
+ }())
177
+ }, {
39
178
  key: "fetchSyncBlockSourceURL",
40
- value: function fetchSyncBlockSourceURL(_ref) {
41
- var _this = this;
42
- var localId = _ref.localId,
43
- resourceId = _ref.resourceId;
44
- if (!localId || !resourceId || !this.dataProvider) {
179
+ value: function fetchSyncBlockSourceURL(resourceId) {
180
+ var _this2 = this;
181
+ if (!resourceId || !this.dataProvider) {
45
182
  return;
46
183
  }
47
184
 
48
185
  // if the sync block is a reference block, we need to fetch the URL to the source
49
186
  // we could optimise this further by checking if the sync block is on the same page as the source
50
- if (!this.syncBlockURLRequests.get(localId)) {
51
- this.syncBlockURLRequests.set(localId, true);
52
- this.dataProvider.retrieveSyncBlockSourceUrl(createSyncBlockNode(localId, resourceId)).then(function (sourceURL) {
53
- var existingSyncBlock = _this.syncBlockCache.get(localId);
54
- if (existingSyncBlock) {
55
- existingSyncBlock.sourceURL = sourceURL;
187
+ if (!this.syncBlockURLRequests.get(resourceId)) {
188
+ this.syncBlockURLRequests.set(resourceId, true);
189
+ this.dataProvider.retrieveSyncBlockSourceUrl(createSyncBlockNode('', resourceId)).then(function (sourceURL) {
190
+ var existingSyncBlock = _this2.getFromCache(resourceId);
191
+ if (existingSyncBlock && existingSyncBlock.data) {
192
+ existingSyncBlock.data = _objectSpread(_objectSpread({}, existingSyncBlock.data), {}, {
193
+ sourceURL: sourceURL
194
+ });
195
+ _this2.updateCache(existingSyncBlock);
56
196
  }
57
197
  }).finally(function () {
58
- _this.syncBlockURLRequests.set(localId, false);
198
+ _this2.syncBlockURLRequests.set(resourceId, false);
59
199
  });
60
200
  }
61
201
  }
202
+
203
+ /**
204
+ * Fetch sync block data for a given sync block node.
205
+ * @param syncBlockNode - The sync block node to fetch data for
206
+ * @returns The fetched sync block data result
207
+ */
62
208
  }, {
63
209
  key: "fetchSyncBlockData",
64
- value: function () {
65
- var _fetchSyncBlockData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee(syncBlockNode) {
66
- var syncNode, existingSyncBlock, data, fetchSyncBlockDataResult;
67
- return _regenerator.default.wrap(function _callee$(_context) {
68
- while (1) switch (_context.prev = _context.next) {
210
+ value: (function () {
211
+ var _fetchSyncBlockData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3(syncBlockNode) {
212
+ var syncNode, data;
213
+ return _regenerator.default.wrap(function _callee3$(_context4) {
214
+ while (1) switch (_context4.prev = _context4.next) {
69
215
  case 0:
70
216
  if (this.dataProvider) {
71
- _context.next = 2;
217
+ _context4.next = 2;
72
218
  break;
73
219
  }
74
220
  throw new Error('Data provider not set');
75
221
  case 2:
76
- syncNode = createSyncBlockNode(syncBlockNode.attrs.localId, syncBlockNode.attrs.resourceId); // async fetch source URL if it is not already fetched
77
- existingSyncBlock = this.syncBlockCache.get(syncBlockNode.attrs.localId);
78
- if (!(existingSyncBlock !== null && existingSyncBlock !== void 0 && existingSyncBlock.sourceURL)) {
79
- this.fetchSyncBlockSourceURL({
80
- localId: syncBlockNode.attrs.localId,
81
- resourceId: syncBlockNode.attrs.resourceId
82
- });
222
+ syncNode = createSyncBlockNode(syncBlockNode.attrs.localId, syncBlockNode.attrs.resourceId);
223
+ _context4.next = 5;
224
+ return this.fetchSyncBlocksData([syncNode]);
225
+ case 5:
226
+ data = _context4.sent;
227
+ if (!(!data || data.length === 0)) {
228
+ _context4.next = 8;
229
+ break;
83
230
  }
84
- _context.prev = 5;
85
- _context.next = 8;
86
- return this.dataProvider.fetchNodesData([syncNode]);
231
+ throw new Error('Failed to fetch sync block data');
87
232
  case 8:
88
- data = _context.sent;
233
+ return _context4.abrupt("return", data[0]);
234
+ case 9:
235
+ case "end":
236
+ return _context4.stop();
237
+ }
238
+ }, _callee3, this);
239
+ }));
240
+ function fetchSyncBlockData(_x2) {
241
+ return _fetchSyncBlockData.apply(this, arguments);
242
+ }
243
+ return fetchSyncBlockData;
244
+ }())
245
+ }, {
246
+ key: "fetchSyncBlocksData",
247
+ value: function () {
248
+ var _fetchSyncBlocksData = (0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee4(syncBlockNodes) {
249
+ var _this3 = this;
250
+ var data, resolvedData;
251
+ return _regenerator.default.wrap(function _callee4$(_context5) {
252
+ while (1) switch (_context5.prev = _context5.next) {
253
+ case 0:
254
+ if (this.dataProvider) {
255
+ _context5.next = 2;
256
+ break;
257
+ }
258
+ throw new Error('Data provider not set');
259
+ case 2:
260
+ _context5.next = 4;
261
+ return this.dataProvider.fetchNodesData(syncBlockNodes);
262
+ case 4:
263
+ data = _context5.sent;
89
264
  if (data) {
90
- _context.next = 11;
265
+ _context5.next = 7;
91
266
  break;
92
267
  }
93
268
  throw new Error('Failed to fetch sync block node data');
94
- case 11:
95
- _context.next = 16;
96
- break;
97
- case 13:
98
- _context.prev = 13;
99
- _context.t0 = _context["catch"](5);
100
- throw new Error('Failed to fetch sync block node data');
101
- case 16:
102
- fetchSyncBlockDataResult = data[0];
103
- if (!fetchSyncBlockDataResult.error && fetchSyncBlockDataResult.data) {
104
- // only adds it to the map if it did not error out
105
- this.syncBlockCache.set(syncBlockNode.attrs.localId, _objectSpread(_objectSpread({}, existingSyncBlock), fetchSyncBlockDataResult.data));
106
- }
107
- return _context.abrupt("return", fetchSyncBlockDataResult);
108
- case 19:
269
+ case 7:
270
+ resolvedData = [];
271
+ data.forEach(function (fetchSyncBlockDataResult) {
272
+ var _resolvedFetchSyncBlo;
273
+ if (!fetchSyncBlockDataResult.resourceId) {
274
+ return;
275
+ }
276
+ var existingSyncBlock = _this3.getFromCache(fetchSyncBlockDataResult.resourceId);
277
+ var resolvedFetchSyncBlockDataResult = existingSyncBlock ? (0, _mergeFetchSyncBlockDataResult.resolveFetchSyncBlockDataResult)(existingSyncBlock, fetchSyncBlockDataResult) : fetchSyncBlockDataResult;
278
+ _this3.updateCache(resolvedFetchSyncBlockDataResult);
279
+ resolvedData.push(resolvedFetchSyncBlockDataResult);
280
+
281
+ // fetch source URL if not already present
282
+ if (!((_resolvedFetchSyncBlo = resolvedFetchSyncBlockDataResult.data) !== null && _resolvedFetchSyncBlo !== void 0 && _resolvedFetchSyncBlo.sourceURL) && resolvedFetchSyncBlockDataResult.resourceId) {
283
+ _this3.fetchSyncBlockSourceURL(resolvedFetchSyncBlockDataResult.resourceId);
284
+ }
285
+ });
286
+ return _context5.abrupt("return", resolvedData);
287
+ case 10:
109
288
  case "end":
110
- return _context.stop();
289
+ return _context5.stop();
111
290
  }
112
- }, _callee, this, [[5, 13]]);
291
+ }, _callee4, this);
113
292
  }));
114
- function fetchSyncBlockData(_x) {
115
- return _fetchSyncBlockData.apply(this, arguments);
293
+ function fetchSyncBlocksData(_x3) {
294
+ return _fetchSyncBlocksData.apply(this, arguments);
116
295
  }
117
- return fetchSyncBlockData;
296
+ return fetchSyncBlocksData;
118
297
  }()
298
+ }, {
299
+ key: "updateCache",
300
+ value: function updateCache(syncBlock) {
301
+ var resourceId = syncBlock.resourceId;
302
+ if (resourceId) {
303
+ this.syncBlockCache.set(resourceId, syncBlock);
304
+ var callbacks = this.subscriptions.get(resourceId);
305
+ if (callbacks) {
306
+ Object.values(callbacks).forEach(function (callback) {
307
+ callback(syncBlock);
308
+ });
309
+ }
310
+ }
311
+ }
312
+ }, {
313
+ key: "getFromCache",
314
+ value: function getFromCache(resourceId) {
315
+ return this.syncBlockCache.get(resourceId);
316
+ }
317
+ }, {
318
+ key: "deleteFromCache",
319
+ value: function deleteFromCache(resourceId) {
320
+ this.syncBlockCache.delete(resourceId);
321
+ }
322
+ }, {
323
+ key: "subscribe",
324
+ value: function subscribe(node, callback) {
325
+ var _this4 = this;
326
+ // check node is a sync block, as we only support sync block subscriptions
327
+ if (node.type.name !== 'syncBlock') {
328
+ return function () {};
329
+ }
330
+ var _node$attrs = node.attrs,
331
+ resourceId = _node$attrs.resourceId,
332
+ localId = _node$attrs.localId;
333
+ if (!localId || !resourceId) {
334
+ return function () {};
335
+ }
336
+
337
+ // add to subscriptions map
338
+ var resourceSubscriptions = this.subscriptions.get(resourceId) || {};
339
+ this.subscriptions.set(resourceId, _objectSpread(_objectSpread({}, resourceSubscriptions), {}, (0, _defineProperty2.default)({}, localId, callback)));
340
+
341
+ // call the callback immediately if we have cached data
342
+ var cachedData = this.getFromCache(resourceId);
343
+ if (cachedData) {
344
+ callback(cachedData);
345
+ } else {
346
+ this.fetchSyncBlockData(node).catch(function () {});
347
+ }
348
+ return function () {
349
+ var resourceSubscriptions = _this4.subscriptions.get(resourceId);
350
+ if (resourceSubscriptions) {
351
+ delete resourceSubscriptions[localId];
352
+ if (Object.keys(resourceSubscriptions).length === 0) {
353
+ _this4.subscriptions.delete(resourceId);
354
+ _this4.deleteFromCache(resourceId);
355
+ } else {
356
+ _this4.subscriptions.set(resourceId, resourceSubscriptions);
357
+ }
358
+ }
359
+ };
360
+ }
361
+
119
362
  /**
120
363
  * Get the URL for a sync block.
121
- * @param localId - The local ID of the sync block to get the URL for
364
+ * @param resourceId - The resource ID of the sync block
122
365
  * @returns
123
366
  */
124
367
  }, {
125
368
  key: "getSyncBlockURL",
126
- value: function getSyncBlockURL(localId) {
127
- var syncBlock = this.syncBlockCache.get(localId);
128
- return syncBlock === null || syncBlock === void 0 ? void 0 : syncBlock.sourceURL;
369
+ value: function getSyncBlockURL(resourceId) {
370
+ var _syncBlock$data;
371
+ var syncBlock = this.getFromCache(resourceId);
372
+ if (!syncBlock) {
373
+ return undefined;
374
+ }
375
+ return (_syncBlock$data = syncBlock.data) === null || _syncBlock$data === void 0 ? void 0 : _syncBlock$data.sourceURL;
376
+ }
377
+ }, {
378
+ key: "destroy",
379
+ value: function destroy() {
380
+ this.syncBlockCache.clear();
381
+ this.subscriptions.clear();
382
+ this.syncBlockURLRequests.clear();
383
+ this.editorView = undefined;
384
+ this.isInitialized = false;
129
385
  }
130
386
  }]);
131
387
  }();
@@ -20,6 +20,12 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
20
20
  this.referenceSyncBlockStoreManager = new _referenceSyncBlockStoreManager.ReferenceSyncBlockStoreManager(dataProvider);
21
21
  this.sourceSyncBlockStoreManager = new _sourceSyncBlockStoreManager.SourceSyncBlockStoreManager(dataProvider);
22
22
  }
23
+
24
+ /**
25
+ * Fetch sync block data for a given sync block node.
26
+ * @param syncBlockNode - The sync block node to fetch data for
27
+ * @returns The fetched sync block data result
28
+ */
23
29
  return (0, _createClass2.default)(SyncBlockStoreManager, [{
24
30
  key: "fetchSyncBlockData",
25
31
  value: function fetchSyncBlockData(syncBlockNode) {
@@ -60,20 +66,22 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
60
66
 
61
67
  /**
62
68
  * Get the URL for a sync block.
63
- * @param localId - The local ID of the sync block to get the URL for
69
+ * @param resourceId - The resource ID of the sync block to get the URL for
64
70
  * @returns
65
71
  */
66
72
  }, {
67
73
  key: "getSyncBlockURL",
68
- value: function getSyncBlockURL(localId) {
74
+ value: function getSyncBlockURL(resourceId) {
69
75
  // only applicable to reference sync block, for now (will be refactored further)
70
- return this.referenceSyncBlockStoreManager.getSyncBlockURL(localId);
76
+ return this.referenceSyncBlockStoreManager.getSyncBlockURL(resourceId);
71
77
  }
72
78
  }, {
73
79
  key: "setEditorView",
74
80
  value: function setEditorView(editorView) {
75
- // only applicable to source sync block, for now (will be refactored further)
76
81
  this.sourceSyncBlockStoreManager.setEditorView(editorView);
82
+ if (editorView) {
83
+ this.referenceSyncBlockStoreManager.init(editorView);
84
+ }
77
85
  }
78
86
  }, {
79
87
  key: "isSourceBlock",
@@ -98,6 +106,16 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
98
106
  // only applicable to source sync block, for now (will be refactored further)
99
107
  return this.sourceSyncBlockStoreManager.createSyncBlockNode();
100
108
  }
109
+ }, {
110
+ key: "subscribeToSyncBlockData",
111
+ value: function subscribeToSyncBlockData(node, callback) {
112
+ return this.referenceSyncBlockStoreManager.subscribe(node, callback);
113
+ }
114
+ }, {
115
+ key: "refreshSubscriptions",
116
+ value: function refreshSubscriptions() {
117
+ this.referenceSyncBlockStoreManager.refreshSubscriptions();
118
+ }
101
119
  }, {
102
120
  key: "deleteSyncBlocksWithConfirmation",
103
121
  value: function deleteSyncBlocksWithConfirmation(tr, syncBlockIds) {
@@ -110,5 +128,10 @@ var SyncBlockStoreManager = exports.SyncBlockStoreManager = /*#__PURE__*/functio
110
128
  // only applicable to source sync block, for now (will be refactored further)
111
129
  this.sourceSyncBlockStoreManager.rebaseTransaction(incomingTr, state);
112
130
  }
131
+ }, {
132
+ key: "destroy",
133
+ value: function destroy() {
134
+ this.referenceSyncBlockStoreManager.destroy();
135
+ }
113
136
  }]);
114
137
  }();
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.resolveFetchSyncBlockDataResult = void 0;
8
+ var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
9
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
10
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
11
+ /**
12
+ * Merges two FetchSyncBlockDataResult objects,
13
+ * currently it only preserves the sourceURL from the old result,
14
+ * but this can be extended in the future to preserve other fields and resolve conflicts as needed.
15
+ * e.g. compare timestamps or version numbers to determine which data is more recent.
16
+ *
17
+ * @param oldResult - The existing FetchSyncBlockDataResult object.
18
+ * @param newResult - The new FetchSyncBlockDataResult object to merge.
19
+ * @returns A merged FetchSyncBlockDataResult object.
20
+ */
21
+ var resolveFetchSyncBlockDataResult = exports.resolveFetchSyncBlockDataResult = function resolveFetchSyncBlockDataResult(oldResult, newResult) {
22
+ var _newResult$data, _oldResult$data;
23
+ // if the old result has no data, we simple return the new result
24
+ if (!oldResult.data) {
25
+ return newResult;
26
+ } else if (!newResult.data) {
27
+ // if the new result has no data, we simply return the old result
28
+ // TODO: EDITOR-2533 - handle this case based on the error type and whether we should keep old data or not
29
+ return oldResult;
30
+ }
31
+
32
+ // otherwise, we merge the two results, preserving the sourceURL from the old result if it exists
33
+ return _objectSpread(_objectSpread({}, newResult), {}, {
34
+ data: _objectSpread(_objectSpread({}, newResult.data), {}, {
35
+ sourceURL: ((_newResult$data = newResult.data) === null || _newResult$data === void 0 ? void 0 : _newResult$data.sourceURL) || ((_oldResult$data = oldResult.data) === null || _oldResult$data === void 0 ? void 0 : _oldResult$data.sourceURL) || undefined
36
+ })
37
+ });
38
+ };