@khanacademy/wonder-blocks-data 2.3.0 → 2.3.4

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.
package/dist/es/index.js CHANGED
@@ -1,211 +1,6 @@
1
1
  import { Server } from '@khanacademy/wonder-blocks-core';
2
- import { createContext, createElement, Component } from 'react';
3
-
4
- function _classCallCheck(instance, Constructor) {
5
- if (!(instance instanceof Constructor)) {
6
- throw new TypeError("Cannot call a class as a function");
7
- }
8
- }
9
-
10
- function _defineProperties(target, props) {
11
- for (var i = 0; i < props.length; i++) {
12
- var descriptor = props[i];
13
- descriptor.enumerable = descriptor.enumerable || false;
14
- descriptor.configurable = true;
15
- if ("value" in descriptor) descriptor.writable = true;
16
- Object.defineProperty(target, descriptor.key, descriptor);
17
- }
18
- }
19
-
20
- function _createClass(Constructor, protoProps, staticProps) {
21
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
22
- if (staticProps) _defineProperties(Constructor, staticProps);
23
- return Constructor;
24
- }
25
-
26
- function _defineProperty(obj, key, value) {
27
- if (key in obj) {
28
- Object.defineProperty(obj, key, {
29
- value: value,
30
- enumerable: true,
31
- configurable: true,
32
- writable: true
33
- });
34
- } else {
35
- obj[key] = value;
36
- }
37
-
38
- return obj;
39
- }
40
-
41
- function ownKeys(object, enumerableOnly) {
42
- var keys = Object.keys(object);
43
-
44
- if (Object.getOwnPropertySymbols) {
45
- var symbols = Object.getOwnPropertySymbols(object);
46
- if (enumerableOnly) symbols = symbols.filter(function (sym) {
47
- return Object.getOwnPropertyDescriptor(object, sym).enumerable;
48
- });
49
- keys.push.apply(keys, symbols);
50
- }
51
-
52
- return keys;
53
- }
54
-
55
- function _objectSpread2(target) {
56
- for (var i = 1; i < arguments.length; i++) {
57
- var source = arguments[i] != null ? arguments[i] : {};
58
-
59
- if (i % 2) {
60
- ownKeys(Object(source), true).forEach(function (key) {
61
- _defineProperty(target, key, source[key]);
62
- });
63
- } else if (Object.getOwnPropertyDescriptors) {
64
- Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
65
- } else {
66
- ownKeys(Object(source)).forEach(function (key) {
67
- Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
68
- });
69
- }
70
- }
71
-
72
- return target;
73
- }
74
-
75
- function _inherits(subClass, superClass) {
76
- if (typeof superClass !== "function" && superClass !== null) {
77
- throw new TypeError("Super expression must either be null or a function");
78
- }
79
-
80
- subClass.prototype = Object.create(superClass && superClass.prototype, {
81
- constructor: {
82
- value: subClass,
83
- writable: true,
84
- configurable: true
85
- }
86
- });
87
- if (superClass) _setPrototypeOf(subClass, superClass);
88
- }
89
-
90
- function _getPrototypeOf(o) {
91
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
92
- return o.__proto__ || Object.getPrototypeOf(o);
93
- };
94
- return _getPrototypeOf(o);
95
- }
96
-
97
- function _setPrototypeOf(o, p) {
98
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
99
- o.__proto__ = p;
100
- return o;
101
- };
102
-
103
- return _setPrototypeOf(o, p);
104
- }
105
-
106
- function _isNativeReflectConstruct() {
107
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
108
- if (Reflect.construct.sham) return false;
109
- if (typeof Proxy === "function") return true;
110
-
111
- try {
112
- Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
113
- return true;
114
- } catch (e) {
115
- return false;
116
- }
117
- }
118
-
119
- function _assertThisInitialized(self) {
120
- if (self === void 0) {
121
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
122
- }
123
-
124
- return self;
125
- }
126
-
127
- function _possibleConstructorReturn(self, call) {
128
- if (call && (typeof call === "object" || typeof call === "function")) {
129
- return call;
130
- }
131
-
132
- return _assertThisInitialized(self);
133
- }
134
-
135
- function _createSuper(Derived) {
136
- var hasNativeReflectConstruct = _isNativeReflectConstruct();
137
-
138
- return function _createSuperInternal() {
139
- var Super = _getPrototypeOf(Derived),
140
- result;
141
-
142
- if (hasNativeReflectConstruct) {
143
- var NewTarget = _getPrototypeOf(this).constructor;
144
-
145
- result = Reflect.construct(Super, arguments, NewTarget);
146
- } else {
147
- result = Super.apply(this, arguments);
148
- }
149
-
150
- return _possibleConstructorReturn(this, result);
151
- };
152
- }
153
-
154
- function _slicedToArray(arr, i) {
155
- return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
156
- }
157
-
158
- function _arrayWithHoles(arr) {
159
- if (Array.isArray(arr)) return arr;
160
- }
161
-
162
- function _iterableToArrayLimit(arr, i) {
163
- if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
164
- var _arr = [];
165
- var _n = true;
166
- var _d = false;
167
- var _e = undefined;
168
-
169
- try {
170
- for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
171
- _arr.push(_s.value);
172
-
173
- if (i && _arr.length === i) break;
174
- }
175
- } catch (err) {
176
- _d = true;
177
- _e = err;
178
- } finally {
179
- try {
180
- if (!_n && _i["return"] != null) _i["return"]();
181
- } finally {
182
- if (_d) throw _e;
183
- }
184
- }
185
-
186
- return _arr;
187
- }
188
-
189
- function _unsupportedIterableToArray(o, minLen) {
190
- if (!o) return;
191
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
192
- var n = Object.prototype.toString.call(o).slice(8, -1);
193
- if (n === "Object" && o.constructor) n = o.constructor.name;
194
- if (n === "Map" || n === "Set") return Array.from(o);
195
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
196
- }
197
-
198
- function _arrayLikeToArray(arr, len) {
199
- if (len == null || len > arr.length) len = arr.length;
200
-
201
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
202
-
203
- return arr2;
204
- }
205
-
206
- function _nonIterableRest() {
207
- throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
208
- }
2
+ import { createContext, Component, createElement } from 'react';
3
+ import _extends from '@babel/runtime/helpers/extends';
209
4
 
210
5
  function deepClone(source) {
211
6
  /**
@@ -215,8 +10,8 @@ function deepClone(source) {
215
10
  *
216
11
  * NOTE: This doesn't work for get/set property accessors.
217
12
  */
218
- var serializedInitCache = JSON.stringify(source);
219
- var cloneInitCache = JSON.parse(serializedInitCache);
13
+ const serializedInitCache = JSON.stringify(source);
14
+ const cloneInitCache = JSON.parse(serializedInitCache);
220
15
  return Object.freeze(cloneInitCache);
221
16
  }
222
17
  /**
@@ -230,61 +25,53 @@ function deepClone(source) {
230
25
  */
231
26
 
232
27
 
233
- var MemoryCache = /*#__PURE__*/function () {
234
- function MemoryCache() {
235
- var _this = this;
236
-
237
- var source = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
238
-
239
- _classCallCheck(this, MemoryCache);
28
+ class MemoryCache {
29
+ constructor(source = null) {
30
+ this.store = (handler, options, entry) => {
31
+ const requestType = handler.type;
32
+ const frozenEntry = Object.isFrozen(entry) ? entry : Object.freeze(entry); // Ensure we have a cache location for this handler type.
240
33
 
241
- _defineProperty(this, "_cache", void 0);
34
+ this._cache[requestType] = this._cache[requestType] || {}; // Cache the data.
242
35
 
243
- _defineProperty(this, "store", function (handler, options, entry) {
244
- var requestType = handler.type;
245
- var frozenEntry = Object.isFrozen(entry) ? entry : Object.freeze(entry); // Ensure we have a cache location for this handler type.
36
+ const key = handler.getKey(options);
37
+ this._cache[requestType][key] = frozenEntry;
38
+ };
246
39
 
247
- _this._cache[requestType] = _this._cache[requestType] || {}; // Cache the data.
248
-
249
- var key = handler.getKey(options);
250
- _this._cache[requestType][key] = frozenEntry;
251
- });
40
+ this.retrieve = (handler, options) => {
41
+ const requestType = handler.type; // Get the internal subcache for the handler.
252
42
 
253
- _defineProperty(this, "retrieve", function (handler, options) {
254
- var requestType = handler.type; // Get the internal subcache for the handler.
255
-
256
- var handlerCache = _this._cache[requestType];
43
+ const handlerCache = this._cache[requestType];
257
44
 
258
45
  if (!handlerCache) {
259
46
  return null;
260
47
  } // Get the response.
261
48
 
262
49
 
263
- var key = handler.getKey(options);
264
- var internalEntry = handlerCache[key];
50
+ const key = handler.getKey(options);
51
+ const internalEntry = handlerCache[key];
265
52
 
266
53
  if (internalEntry == null) {
267
54
  return null;
268
55
  }
269
56
 
270
57
  return internalEntry;
271
- });
58
+ };
272
59
 
273
- _defineProperty(this, "remove", function (handler, options) {
274
- var requestType = handler.type; // NOTE(somewhatabstract): We could invoke removeAll with a predicate
60
+ this.remove = (handler, options) => {
61
+ const requestType = handler.type; // NOTE(somewhatabstract): We could invoke removeAll with a predicate
275
62
  // to match the key of the entry we're removing, but that's an
276
63
  // inefficient way to remove a single item, so let's not do that.
277
64
  // Get the internal subcache for the handler.
278
65
 
279
- var handlerCache = _this._cache[requestType];
66
+ const handlerCache = this._cache[requestType];
280
67
 
281
68
  if (!handlerCache) {
282
69
  return false;
283
70
  } // Get the entry.
284
71
 
285
72
 
286
- var key = handler.getKey(options);
287
- var internalEntry = handlerCache[key];
73
+ const key = handler.getKey(options);
74
+ const internalEntry = handlerCache[key];
288
75
 
289
76
  if (internalEntry == null) {
290
77
  return false;
@@ -293,41 +80,37 @@ var MemoryCache = /*#__PURE__*/function () {
293
80
 
294
81
  delete handlerCache[key];
295
82
  return true;
296
- });
83
+ };
297
84
 
298
- _defineProperty(this, "removeAll", function (handler, predicate) {
299
- var requestType = handler.type; // Get the internal subcache for the handler.
85
+ this.removeAll = (handler, predicate) => {
86
+ const requestType = handler.type; // Get the internal subcache for the handler.
300
87
 
301
- var handlerCache = _this._cache[requestType];
88
+ const handlerCache = this._cache[requestType];
302
89
 
303
90
  if (!handlerCache) {
304
91
  return 0;
305
92
  } // Apply the predicate to what we have cached.
306
93
 
307
94
 
308
- var removedCount = 0;
95
+ let removedCount = 0;
309
96
 
310
- for (var _i = 0, _Object$entries = Object.entries(handlerCache); _i < _Object$entries.length; _i++) {
311
- var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
312
- _key = _Object$entries$_i[0],
313
- _entry = _Object$entries$_i[1];
314
-
315
- if (typeof predicate !== "function" || predicate(_key, _entry)) {
97
+ for (const [key, entry] of Object.entries(handlerCache)) {
98
+ if (typeof predicate !== "function" || predicate(key, entry)) {
316
99
  removedCount++;
317
- delete handlerCache[_key];
100
+ delete handlerCache[key];
318
101
  }
319
102
  }
320
103
 
321
104
  return removedCount;
322
- });
105
+ };
323
106
 
324
- _defineProperty(this, "cloneData", function () {
107
+ this.cloneData = () => {
325
108
  try {
326
- return deepClone(_this._cache);
109
+ return deepClone(this._cache);
327
110
  } catch (e) {
328
- throw new Error("An error occurred while trying to clone the cache: ".concat(e));
111
+ throw new Error(`An error occurred while trying to clone the cache: ${e}`);
329
112
  }
330
- });
113
+ };
331
114
 
332
115
  this._cache = {};
333
116
 
@@ -338,25 +121,21 @@ var MemoryCache = /*#__PURE__*/function () {
338
121
  * So we deep clone it and then assign the clone values to our
339
122
  * internal cache.
340
123
  */
341
- var cloneInitCache = deepClone(source);
124
+ const cloneInitCache = deepClone(source);
342
125
  Object.assign(this._cache, cloneInitCache);
343
126
  } catch (e) {
344
- throw new Error("An error occurred trying to initialize from a response cache snapshot: ".concat(e));
127
+ throw new Error(`An error occurred trying to initialize from a response cache snapshot: ${e}`);
345
128
  }
346
129
  }
347
130
  }
348
131
 
349
- _createClass(MemoryCache, [{
350
- key: "inUse",
351
- get: function get() {
352
- return Object.keys(this._cache).length > 0;
353
- }
354
- }]);
132
+ get inUse() {
133
+ return Object.keys(this._cache).length > 0;
134
+ }
355
135
 
356
- return MemoryCache;
357
- }();
136
+ }
358
137
 
359
- var defaultInstance = null;
138
+ let defaultInstance = null;
360
139
  /**
361
140
  * This is a cache implementation to use when no caching is wanted.
362
141
  *
@@ -369,46 +148,34 @@ var defaultInstance = null;
369
148
  * requested data after hydration has finished.
370
149
  */
371
150
 
372
- var NoCache = /*#__PURE__*/function () {
373
- function NoCache() {
374
- _classCallCheck(this, NoCache);
375
-
376
- _defineProperty(this, "store", function (handler, options, entry) {
151
+ class NoCache {
152
+ constructor() {
153
+ this.store = (handler, options, entry) => {
377
154
  /* empty */
378
- });
155
+ };
379
156
 
380
- _defineProperty(this, "retrieve", function (handler, options) {
381
- return null;
382
- });
157
+ this.retrieve = (handler, options) => null;
383
158
 
384
- _defineProperty(this, "remove", function (handler, options) {
385
- return false;
386
- });
159
+ this.remove = (handler, options) => false;
387
160
 
388
- _defineProperty(this, "removeAll", function (handler, predicate) {
389
- return 0;
390
- });
161
+ this.removeAll = (handler, predicate) => 0;
391
162
  }
392
163
 
393
- _createClass(NoCache, null, [{
394
- key: "Default",
395
- get: function get() {
396
- if (defaultInstance == null) {
397
- defaultInstance = new NoCache();
398
- }
399
-
400
- return defaultInstance;
164
+ static get Default() {
165
+ if (defaultInstance == null) {
166
+ defaultInstance = new NoCache();
401
167
  }
402
- }]);
403
168
 
404
- return NoCache;
405
- }();
169
+ return defaultInstance;
170
+ }
171
+
172
+ }
406
173
 
407
174
  /**
408
175
  * The default instance is stored here.
409
176
  * It's created below in the Default() static property.
410
177
  */
411
- var _default;
178
+ let _default;
412
179
  /**
413
180
  * Implements the response cache.
414
181
  *
@@ -416,60 +183,46 @@ var _default;
416
183
  */
417
184
 
418
185
 
419
- var ResponseCache = /*#__PURE__*/function () {
420
- _createClass(ResponseCache, null, [{
421
- key: "Default",
422
- get: function get() {
423
- if (!_default) {
424
- _default = new ResponseCache();
425
- }
426
-
427
- return _default;
186
+ class ResponseCache {
187
+ static get Default() {
188
+ if (!_default) {
189
+ _default = new ResponseCache();
428
190
  }
429
- }]);
430
191
 
431
- function ResponseCache() {
432
- var _this = this;
433
-
434
- var memoryCache = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
435
- var ssrOnlyCache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
436
-
437
- _classCallCheck(this, ResponseCache);
438
-
439
- _defineProperty(this, "_hydrationAndDefaultCache", void 0);
440
-
441
- _defineProperty(this, "_ssrOnlyCache", void 0);
192
+ return _default;
193
+ }
442
194
 
443
- _defineProperty(this, "initialize", function (source) {
444
- if (_this._hydrationAndDefaultCache.inUse) {
195
+ constructor(memoryCache = null, ssrOnlyCache = null) {
196
+ this.initialize = source => {
197
+ if (this._hydrationAndDefaultCache.inUse) {
445
198
  throw new Error("Cannot initialize data response cache more than once");
446
199
  }
447
200
 
448
201
  try {
449
- _this._hydrationAndDefaultCache = new MemoryCache(source);
202
+ this._hydrationAndDefaultCache = new MemoryCache(source);
450
203
  } catch (e) {
451
- throw new Error("An error occurred trying to initialize the data response cache: ".concat(e));
204
+ throw new Error(`An error occurred trying to initialize the data response cache: ${e}`);
452
205
  }
453
- });
206
+ };
454
207
 
455
- _defineProperty(this, "cacheData", function (handler, options, data) {
456
- return _this._setCacheEntry(handler, options, {
457
- data: data
208
+ this.cacheData = (handler, options, data) => {
209
+ return this._setCacheEntry(handler, options, {
210
+ data
458
211
  });
459
- });
212
+ };
460
213
 
461
- _defineProperty(this, "cacheError", function (handler, options, error) {
462
- var errorMessage = typeof error === "string" ? error : error.message;
463
- return _this._setCacheEntry(handler, options, {
214
+ this.cacheError = (handler, options, error) => {
215
+ const errorMessage = typeof error === "string" ? error : error.message;
216
+ return this._setCacheEntry(handler, options, {
464
217
  error: errorMessage
465
218
  });
466
- });
219
+ };
467
220
 
468
- _defineProperty(this, "getEntry", function (handler, options) {
221
+ this.getEntry = (handler, options) => {
469
222
  // If we're not server-side, and the handler has a custom cache
470
223
  // let's try to use it.
471
- if (_this._ssrOnlyCache == null && handler.cache != null) {
472
- var entry = handler.cache.retrieve(handler, options);
224
+ if (this._ssrOnlyCache == null && handler.cache != null) {
225
+ const entry = handler.cache.retrieve(handler, options);
473
226
 
474
227
  if (entry != null) {
475
228
  // Custom cache has an entry, so use it.
@@ -481,12 +234,12 @@ var ResponseCache = /*#__PURE__*/function () {
481
234
  // we would never hydrate properly.
482
235
 
483
236
 
484
- var internalEntry = _this._defaultCache(handler).retrieve(handler, options); // If we are not server-side and we hydrated something that the custom
237
+ const internalEntry = this._defaultCache(handler).retrieve(handler, options); // If we are not server-side and we hydrated something that the custom
485
238
  // cache didn't have, we need to make sure the custom cache contains
486
239
  // that value.
487
240
 
488
241
 
489
- if (_this._ssrOnlyCache == null && handler.cache != null && internalEntry != null) {
242
+ if (this._ssrOnlyCache == null && handler.cache != null && internalEntry != null) {
490
243
  // Yes, if this throws, we will have a problem. We want that.
491
244
  // Bad cache implementations should be overt.
492
245
  handler.cache.store(handler, options, internalEntry); // We now delete this from our in-memory cache as we don't need it.
@@ -495,51 +248,51 @@ var ResponseCache = /*#__PURE__*/function () {
495
248
  // expected valid usage of this framework - two handlers with
496
249
  // different caching options shouldn't be using the same type name.
497
250
 
498
- _this._hydrationAndDefaultCache.remove(handler, options);
251
+ this._hydrationAndDefaultCache.remove(handler, options);
499
252
  }
500
253
 
501
254
  return internalEntry;
502
- });
255
+ };
503
256
 
504
- _defineProperty(this, "remove", function (handler, options) {
257
+ this.remove = (handler, options) => {
505
258
  // NOTE(somewhatabstract): We could invoke removeAll with a predicate
506
259
  // to match the key of the entry we're removing, but that's an
507
260
  // inefficient way to remove a single item, so let's not do that.
508
261
  // If we're not server-side, and the handler has a custom cache
509
262
  // let's try to use it.
510
- var customCache = _this._ssrOnlyCache == null ? handler.cache : null;
511
- var removedCustom = !!(customCache === null || customCache === void 0 ? void 0 : customCache.remove(handler, options)); // Delete the entry from our internal cache.
263
+ const customCache = this._ssrOnlyCache == null ? handler.cache : null;
264
+ const removedCustom = !!(customCache != null && customCache.remove(handler, options)); // Delete the entry from our internal cache.
512
265
  // Even if we have a custom cache, we want to make sure we still
513
266
  // removed the same value from internal cache since this could be
514
267
  // getting called before hydration for some complex advanced usage
515
268
  // reason.
516
269
 
517
- return _this._defaultCache(handler).remove(handler, options) || removedCustom;
518
- });
270
+ return this._defaultCache(handler).remove(handler, options) || removedCustom;
271
+ };
519
272
 
520
- _defineProperty(this, "removeAll", function (handler, predicate) {
273
+ this.removeAll = (handler, predicate) => {
521
274
  // If we're not server-side, and the handler has a custom cache
522
275
  // let's try to use it.
523
- var customCache = _this._ssrOnlyCache == null ? handler.cache : null;
524
- var removedCountCustom = (customCache === null || customCache === void 0 ? void 0 : customCache.removeAll(handler, predicate)) || 0; // Apply the predicate to what we have in our internal cached.
276
+ const customCache = this._ssrOnlyCache == null ? handler.cache : null;
277
+ const removedCountCustom = (customCache == null ? void 0 : customCache.removeAll(handler, predicate)) || 0; // Apply the predicate to what we have in our internal cached.
525
278
  // Even if we have a custom cache, we want to make sure we still
526
279
  // removed the same value from internal cache since this could be
527
280
  // getting called before hydration for some complex advanced usage
528
281
  // reason.
529
282
 
530
- var removedCount = _this._defaultCache(handler).removeAll(handler, predicate); // We have no idea which keys were removed from which caches,
283
+ const removedCount = this._defaultCache(handler).removeAll(handler, predicate); // We have no idea which keys were removed from which caches,
531
284
  // so we can't dedupe the remove counts based on keys.
532
285
  // That's why we return the total records deleted rather than the
533
286
  // total keys deleted.
534
287
 
535
288
 
536
289
  return removedCount + removedCountCustom;
537
- });
290
+ };
538
291
 
539
- _defineProperty(this, "cloneHydratableData", function () {
292
+ this.cloneHydratableData = () => {
540
293
  // We return our hydration cache only.
541
- return _this._hydrationAndDefaultCache.cloneData();
542
- });
294
+ return this._hydrationAndDefaultCache.cloneData();
295
+ };
543
296
 
544
297
  this._ssrOnlyCache = Server.isServerSide() ? ssrOnlyCache || new MemoryCache() : undefined;
545
298
  this._hydrationAndDefaultCache = memoryCache || new MemoryCache();
@@ -549,87 +302,71 @@ var ResponseCache = /*#__PURE__*/function () {
549
302
  */
550
303
 
551
304
 
552
- _createClass(ResponseCache, [{
553
- key: "_defaultCache",
554
- value: function _defaultCache(handler) {
555
- if (handler.hydrate) {
556
- return this._hydrationAndDefaultCache;
557
- } // If the handler doesn't want to hydrate, we return the SSR-only cache.
558
- // If we are client-side, we return our non-caching implementation.
305
+ _defaultCache(handler) {
306
+ if (handler.hydrate) {
307
+ return this._hydrationAndDefaultCache;
308
+ } // If the handler doesn't want to hydrate, we return the SSR-only cache.
309
+ // If we are client-side, we return our non-caching implementation.
559
310
 
560
311
 
561
- return this._ssrOnlyCache || NoCache.Default;
562
- }
563
- }, {
564
- key: "_setCacheEntry",
565
- value: function _setCacheEntry(handler, options, entry) {
566
- var frozenEntry = Object.freeze(entry);
312
+ return this._ssrOnlyCache || NoCache.Default;
313
+ }
567
314
 
568
- if (this._ssrOnlyCache == null && handler.cache != null) {
569
- // We are not server-side, and our handler has its own cache,
570
- // so we use that to store values.
571
- handler.cache.store(handler, options, frozenEntry);
572
- } else {
573
- // We are either server-side, or our handler doesn't provide
574
- // a caching override.
575
- this._defaultCache(handler).store(handler, options, frozenEntry);
576
- }
315
+ _setCacheEntry(handler, options, entry) {
316
+ const frozenEntry = Object.freeze(entry);
577
317
 
578
- return frozenEntry;
318
+ if (this._ssrOnlyCache == null && handler.cache != null) {
319
+ // We are not server-side, and our handler has its own cache,
320
+ // so we use that to store values.
321
+ handler.cache.store(handler, options, frozenEntry);
322
+ } else {
323
+ // We are either server-side, or our handler doesn't provide
324
+ // a caching override.
325
+ this._defaultCache(handler).store(handler, options, frozenEntry);
579
326
  }
580
- /**
581
- * Initialize the cache from a given cache state.
582
- *
583
- * This can only be called if the cache is not already in use.
584
- */
585
327
 
586
- }]);
328
+ return frozenEntry;
329
+ }
330
+ /**
331
+ * Initialize the cache from a given cache state.
332
+ *
333
+ * This can only be called if the cache is not already in use.
334
+ */
587
335
 
588
- return ResponseCache;
589
- }();
590
336
 
591
- var _default$1;
337
+ }
592
338
 
593
- var RequestFulfillment = /*#__PURE__*/function () {
594
- _createClass(RequestFulfillment, null, [{
595
- key: "Default",
596
- get: function get() {
597
- if (!_default$1) {
598
- _default$1 = new RequestFulfillment();
599
- }
339
+ let _default$1;
600
340
 
601
- return _default$1;
341
+ class RequestFulfillment {
342
+ static get Default() {
343
+ if (!_default$1) {
344
+ _default$1 = new RequestFulfillment();
602
345
  }
603
- }]);
604
-
605
- function RequestFulfillment() {
606
- var _this = this;
607
-
608
- var responseCache = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
609
346
 
610
- _classCallCheck(this, RequestFulfillment);
611
-
612
- _defineProperty(this, "_responseCache", void 0);
347
+ return _default$1;
348
+ }
613
349
 
614
- _defineProperty(this, "_requests", {});
350
+ constructor(responseCache = undefined) {
351
+ this._requests = {};
615
352
 
616
- _defineProperty(this, "_getHandlerSubcache", function (handler) {
617
- if (!_this._requests[handler.type]) {
618
- _this._requests[handler.type] = {};
353
+ this._getHandlerSubcache = handler => {
354
+ if (!this._requests[handler.type]) {
355
+ this._requests[handler.type] = {};
619
356
  }
620
357
 
621
- return _this._requests[handler.type];
622
- });
358
+ return this._requests[handler.type];
359
+ };
623
360
 
624
- _defineProperty(this, "fulfill", function (handler, options) {
625
- var handlerRequests = _this._getHandlerSubcache(handler);
361
+ this.fulfill = (handler, options) => {
362
+ const handlerRequests = this._getHandlerSubcache(handler);
626
363
 
627
- var key = handler.getKey(options);
364
+ const key = handler.getKey(options);
628
365
  /**
629
366
  * If we have an inflight request, we'll provide that.
630
367
  */
631
368
 
632
- var inflight = handlerRequests[key];
369
+ const inflight = handlerRequests[key];
633
370
 
634
371
  if (inflight) {
635
372
  return inflight;
@@ -639,19 +376,20 @@ var RequestFulfillment = /*#__PURE__*/function () {
639
376
  */
640
377
 
641
378
 
642
- var _this$_responseCache = _this._responseCache,
643
- cacheData = _this$_responseCache.cacheData,
644
- cacheError = _this$_responseCache.cacheError;
379
+ const {
380
+ cacheData,
381
+ cacheError
382
+ } = this._responseCache;
645
383
 
646
384
  try {
647
- var request = handler.fulfillRequest(options).then(function (data) {
385
+ const request = handler.fulfillRequest(options).then(data => {
648
386
  delete handlerRequests[key];
649
387
  /**
650
388
  * Let's cache the data!
651
389
  */
652
390
 
653
391
  return cacheData(handler, options, data);
654
- }).catch(function (error) {
392
+ }).catch(error => {
655
393
  delete handlerRequests[key];
656
394
  /**
657
395
  * Let's cache the error!
@@ -668,26 +406,25 @@ var RequestFulfillment = /*#__PURE__*/function () {
668
406
  */
669
407
  return Promise.resolve(cacheError(handler, options, e));
670
408
  }
671
- });
409
+ };
672
410
 
673
411
  this._responseCache = responseCache || ResponseCache.Default;
674
412
  }
675
413
 
676
- return RequestFulfillment;
677
- }();
414
+ }
678
415
 
679
416
  /**
680
417
  * Used to inject our tracking function into the render framework.
681
418
  *
682
419
  * INTERNAL USE ONLY
683
420
  */
684
- var TrackerContext = new createContext(null);
421
+ const TrackerContext = new createContext(null);
685
422
  /**
686
423
  * The default instance is stored here.
687
424
  * It's created below in the Default() static property.
688
425
  */
689
426
 
690
- var _default$2;
427
+ let _default$2;
691
428
  /**
692
429
  * Implements request tracking and fulfillment.
693
430
  *
@@ -695,76 +432,59 @@ var _default$2;
695
432
  */
696
433
 
697
434
 
698
- var RequestTracker = /*#__PURE__*/function () {
699
- _createClass(RequestTracker, null, [{
700
- key: "Default",
701
- get: function get() {
702
- if (!_default$2) {
703
- _default$2 = new RequestTracker();
704
- }
705
-
706
- return _default$2;
435
+ class RequestTracker {
436
+ static get Default() {
437
+ if (!_default$2) {
438
+ _default$2 = new RequestTracker();
707
439
  }
708
- /**
709
- * These are the caches for tracked requests, their handlers, and responses.
710
- */
711
-
712
- }]);
713
-
714
- function RequestTracker() {
715
- var _this = this;
716
-
717
- var responseCache = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : undefined;
718
440
 
719
- _classCallCheck(this, RequestTracker);
720
-
721
- _defineProperty(this, "_trackedHandlers", {});
722
-
723
- _defineProperty(this, "_trackedRequests", {});
441
+ return _default$2;
442
+ }
443
+ /**
444
+ * These are the caches for tracked requests, their handlers, and responses.
445
+ */
724
446
 
725
- _defineProperty(this, "_responseCache", void 0);
726
447
 
727
- _defineProperty(this, "_requestFulfillment", void 0);
448
+ constructor(responseCache = undefined) {
449
+ this._trackedHandlers = {};
450
+ this._trackedRequests = {};
728
451
 
729
- _defineProperty(this, "trackDataRequest", function (handler, options) {
730
- var key = handler.getKey(options);
731
- var type = handler.type;
452
+ this.trackDataRequest = (handler, options) => {
453
+ const key = handler.getKey(options);
454
+ const type = handler.type;
732
455
  /**
733
456
  * Make sure we have stored the handler for use when fulfilling requests.
734
457
  */
735
458
 
736
- if (_this._trackedHandlers[type] == null) {
737
- _this._trackedHandlers[type] = handler;
738
- _this._trackedRequests[type] = {};
459
+ if (this._trackedHandlers[type] == null) {
460
+ this._trackedHandlers[type] = handler;
461
+ this._trackedRequests[type] = {};
739
462
  }
740
463
  /**
741
464
  * If we don't already have this tracked, then let's track it.
742
465
  */
743
466
 
744
467
 
745
- if (_this._trackedRequests[type][key] == null) {
746
- _this._trackedRequests[type][key] = options;
468
+ if (this._trackedRequests[type][key] == null) {
469
+ this._trackedRequests[type][key] = options;
747
470
  }
748
- });
749
-
750
- _defineProperty(this, "reset", function () {
751
- _this._trackedHandlers = {};
752
- _this._trackedRequests = {};
753
- });
471
+ };
754
472
 
755
- _defineProperty(this, "fulfillTrackedRequests", function () {
756
- var promises = [];
473
+ this.reset = () => {
474
+ this._trackedHandlers = {};
475
+ this._trackedRequests = {};
476
+ };
757
477
 
758
- for (var _i = 0, _Object$keys = Object.keys(_this._trackedHandlers); _i < _Object$keys.length; _i++) {
759
- var _handlerType = _Object$keys[_i];
760
- var _handler = _this._trackedHandlers[_handlerType]; // For each handler, we will perform the request fulfillments!
478
+ this.fulfillTrackedRequests = () => {
479
+ const promises = [];
761
480
 
762
- var requests = _this._trackedRequests[_handlerType];
481
+ for (const handlerType of Object.keys(this._trackedHandlers)) {
482
+ const handler = this._trackedHandlers[handlerType]; // For each handler, we will perform the request fulfillments!
763
483
 
764
- for (var _i2 = 0, _Object$keys2 = Object.keys(requests); _i2 < _Object$keys2.length; _i2++) {
765
- var requestKey = _Object$keys2[_i2];
484
+ const requests = this._trackedRequests[handlerType];
766
485
 
767
- var promise = _this._requestFulfillment.fulfill(_handler, requests[requestKey]);
486
+ for (const requestKey of Object.keys(requests)) {
487
+ const promise = this._requestFulfillment.fulfill(handler, requests[requestKey]);
768
488
 
769
489
  promises.push(promise);
770
490
  }
@@ -788,16 +508,13 @@ var RequestTracker = /*#__PURE__*/function () {
788
508
  */
789
509
 
790
510
 
791
- _this.reset();
511
+ this.reset();
792
512
  /**
793
513
  * Let's wait for everything to fulfill, and then clone the cached data.
794
514
  */
795
515
 
796
-
797
- return Promise.all(promises).then(function () {
798
- return _this._responseCache.cloneHydratableData();
799
- });
800
- });
516
+ return Promise.all(promises).then(() => this._responseCache.cloneHydratableData());
517
+ };
801
518
 
802
519
  this._responseCache = responseCache || ResponseCache.Default;
803
520
  this._requestFulfillment = new RequestFulfillment(responseCache);
@@ -810,33 +527,28 @@ var RequestTracker = /*#__PURE__*/function () {
810
527
  */
811
528
 
812
529
 
813
- _createClass(RequestTracker, [{
814
- key: "hasUnfulfilledRequests",
815
-
816
- /**
817
- * Indicates if we have requests waiting to be fulfilled.
818
- */
819
- get: function get() {
820
- return Object.keys(this._trackedRequests).length > 0;
821
- }
822
- /**
823
- * Initiate fulfillment of all tracked requests.
824
- *
825
- * This loops over the requests that were tracked using TrackData, and asks
826
- * the respective handlers to fulfill those requests in the order they were
827
- * tracked.
828
- *
829
- * Calling this method marks tracked requests as fulfilled; requests are
830
- * removed from the list of tracked requests by calling this method.
831
- *
832
- * @returns {Promise<Cache>} A frozen cache of the data that was cached
833
- * as a result of fulfilling the tracked requests.
834
- */
530
+ /**
531
+ * Indicates if we have requests waiting to be fulfilled.
532
+ */
533
+ get hasUnfulfilledRequests() {
534
+ return Object.keys(this._trackedRequests).length > 0;
535
+ }
536
+ /**
537
+ * Initiate fulfillment of all tracked requests.
538
+ *
539
+ * This loops over the requests that were tracked using TrackData, and asks
540
+ * the respective handlers to fulfill those requests in the order they were
541
+ * tracked.
542
+ *
543
+ * Calling this method marks tracked requests as fulfilled; requests are
544
+ * removed from the list of tracked requests by calling this method.
545
+ *
546
+ * @returns {Promise<Cache>} A frozen cache of the data that was cached
547
+ * as a result of fulfilling the tracked requests.
548
+ */
835
549
 
836
- }]);
837
550
 
838
- return RequestTracker;
839
- }();
551
+ }
840
552
 
841
553
  /**
842
554
  * Base implementation for creating a request handler.
@@ -844,99 +556,66 @@ var RequestTracker = /*#__PURE__*/function () {
844
556
  * Provides a base implementation of the `IRequestHandler` base class for
845
557
  * use with the Wonder Blocks Data framework.
846
558
  */
847
- var RequestHandler = /*#__PURE__*/function () {
848
- function RequestHandler(type, cache) {
849
- var hydrate = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
850
-
851
- _classCallCheck(this, RequestHandler);
852
-
853
- _defineProperty(this, "_type", void 0);
854
-
855
- _defineProperty(this, "_cache", void 0);
856
-
857
- _defineProperty(this, "_hydrate", void 0);
858
-
559
+ class RequestHandler {
560
+ constructor(type, cache, hydrate = true) {
859
561
  this._type = type;
860
562
  this._cache = cache || null;
861
563
  this._hydrate = !!hydrate;
862
564
  }
863
565
 
864
- _createClass(RequestHandler, [{
865
- key: "shouldRefreshCache",
866
- value: function shouldRefreshCache(options, cachedEntry) {
867
- /**
868
- * By default, the cache needs a refresh if the current entry is an
869
- * error.
870
- *
871
- * This means that an error will cause a re-request on render.
872
- * Useful if the server rendered an error, as it means the client
873
- * will update after rehydration.
874
- */
875
- return cachedEntry == null || cachedEntry.error != null;
876
- }
877
- }, {
878
- key: "getKey",
879
- value: function getKey(options) {
880
- try {
881
- return options === undefined ? "undefined" : JSON.stringify(options);
882
- } catch (e) {
883
- throw new Error("Failed to auto-generate key: ".concat(e));
884
- }
885
- }
886
- }, {
887
- key: "fulfillRequest",
888
- value: function fulfillRequest(options) {
889
- throw new Error("Not implemented");
890
- }
891
- }, {
892
- key: "type",
893
- get: function get() {
894
- return this._type;
895
- }
896
- }, {
897
- key: "cache",
898
- get: function get() {
899
- return this._cache;
900
- }
901
- }, {
902
- key: "hydrate",
903
- get: function get() {
904
- return this._hydrate;
905
- }
906
- }]);
566
+ get type() {
567
+ return this._type;
568
+ }
907
569
 
908
- return RequestHandler;
909
- }();
570
+ get cache() {
571
+ return this._cache;
572
+ }
910
573
 
911
- /**
912
- * Component to enable data request tracking when server-side rendering.
913
- */
914
- var TrackData = /*#__PURE__*/function (_React$Component) {
915
- _inherits(TrackData, _React$Component);
574
+ get hydrate() {
575
+ return this._hydrate;
576
+ }
916
577
 
917
- var _super = _createSuper(TrackData);
578
+ shouldRefreshCache(options, cachedEntry) {
579
+ /**
580
+ * By default, the cache needs a refresh if the current entry is an
581
+ * error.
582
+ *
583
+ * This means that an error will cause a re-request on render.
584
+ * Useful if the server rendered an error, as it means the client
585
+ * will update after rehydration.
586
+ */
587
+ return cachedEntry == null || cachedEntry.error != null;
588
+ }
918
589
 
919
- function TrackData() {
920
- _classCallCheck(this, TrackData);
590
+ getKey(options) {
591
+ try {
592
+ return options === undefined ? "undefined" : JSON.stringify(options);
593
+ } catch (e) {
594
+ throw new Error(`Failed to auto-generate key: ${e}`);
595
+ }
596
+ }
921
597
 
922
- return _super.apply(this, arguments);
598
+ fulfillRequest(options) {
599
+ throw new Error("Not implemented");
923
600
  }
924
601
 
925
- _createClass(TrackData, [{
926
- key: "render",
927
- value: function render() {
928
- if (!Server.isServerSide()) {
929
- throw new Error("This component is not for use during client-side rendering");
930
- }
602
+ }
931
603
 
932
- return /*#__PURE__*/createElement(TrackerContext.Provider, {
933
- value: RequestTracker.Default.trackDataRequest
934
- }, this.props.children);
604
+ /**
605
+ * Component to enable data request tracking when server-side rendering.
606
+ */
607
+ class TrackData extends Component {
608
+ render() {
609
+ if (!Server.isServerSide()) {
610
+ throw new Error("This component is not for use during client-side rendering");
935
611
  }
936
- }]);
937
612
 
938
- return TrackData;
939
- }(Component);
613
+ return /*#__PURE__*/createElement(TrackerContext.Provider, {
614
+ value: RequestTracker.Default.trackDataRequest
615
+ }, this.props.children);
616
+ }
617
+
618
+ }
940
619
 
941
620
  /**
942
621
  * This component is responsible for actually handling the data request.
@@ -944,314 +623,272 @@ var TrackData = /*#__PURE__*/function (_React$Component) {
944
623
  *
945
624
  * INTERNAL USE ONLY
946
625
  */
947
- var InternalData = /*#__PURE__*/function (_React$Component) {
948
- _inherits(InternalData, _React$Component);
626
+ class InternalData extends Component {
627
+ constructor(props) {
628
+ super(props);
629
+ this.state = this._buildStateAndfulfillNeeds(props);
630
+ }
949
631
 
950
- var _super = _createSuper(InternalData);
632
+ componentDidMount() {
633
+ this._mounted = true;
634
+ }
951
635
 
952
- function InternalData(props) {
953
- var _this;
636
+ shouldComponentUpdate(nextProps, nextState) {
637
+ /**
638
+ * We only bother updating if our state changed.
639
+ *
640
+ * And we only update the state if props changed
641
+ * or we got new data/error.
642
+ */
643
+ if (!this._propsMatch(nextProps)) {
644
+ const newState = this._buildStateAndfulfillNeeds(nextProps);
954
645
 
955
- _classCallCheck(this, InternalData);
646
+ this.setState(newState);
647
+ }
956
648
 
957
- _this = _super.call(this, props);
649
+ return this.state.loading !== nextState.loading || this.state.data !== nextState.data || this.state.error !== nextState.error;
650
+ }
958
651
 
959
- _defineProperty(_assertThisInitialized(_this), "_mounted", void 0);
652
+ componentWillUnmount() {
653
+ this._mounted = false;
654
+ }
960
655
 
961
- _this.state = _this._buildStateAndfulfillNeeds(props);
962
- return _this;
656
+ _propsMatch(otherProps) {
657
+ const {
658
+ handler,
659
+ options
660
+ } = this.props;
661
+ const {
662
+ handler: prevHandler,
663
+ options: prevOptions
664
+ } = otherProps;
665
+ return handler === prevHandler && handler.getKey(options) === prevHandler.getKey(prevOptions);
963
666
  }
964
667
 
965
- _createClass(InternalData, [{
966
- key: "componentDidMount",
967
- value: function componentDidMount() {
968
- this._mounted = true;
969
- }
970
- }, {
971
- key: "shouldComponentUpdate",
972
- value: function shouldComponentUpdate(nextProps, nextState) {
668
+ _buildStateAndfulfillNeeds(propsAtFulfillment) {
669
+ const {
670
+ getEntry,
671
+ handler,
672
+ options
673
+ } = propsAtFulfillment;
674
+ const cachedData = getEntry(handler, options);
675
+
676
+ if (!Server.isServerSide() && (cachedData == null || handler.shouldRefreshCache(options, cachedData))) {
973
677
  /**
974
- * We only bother updating if our state changed.
678
+ * We're not on the server, the cache missed, or our handler says
679
+ * we should refresh the cache.
680
+ *
681
+ * Therefore, we need to request data.
975
682
  *
976
- * And we only update the state if props changed
977
- * or we got new data/error.
683
+ * We have to do this here from the constructor so that this
684
+ * data request is tracked when performing server-side rendering.
978
685
  */
979
- if (!this._propsMatch(nextProps)) {
980
- var newState = this._buildStateAndfulfillNeeds(nextProps);
686
+ RequestFulfillment.Default.fulfill(handler, options).then(cacheEntry => {
687
+ /**
688
+ * We get here, we should have updated the cache.
689
+ * However, we need to update the component, but we
690
+ * should only do that if the props are the same as they
691
+ * were when this was called.
692
+ */
693
+ if (this._mounted && this._propsMatch(propsAtFulfillment)) {
694
+ this.setState({
695
+ loading: false,
696
+ data: cacheEntry.data,
697
+ error: cacheEntry.error
698
+ });
699
+ }
981
700
 
982
- this.setState(newState);
983
- }
701
+ return null;
702
+ }).catch(e => {
703
+ /**
704
+ * We should never get here, but if we do.
705
+ */
706
+ // eslint-disable-next-line no-console
707
+ console.error(`Unexpected error occurred during data fulfillment: ${e}`);
708
+
709
+ if (this._mounted && this._propsMatch(propsAtFulfillment)) {
710
+ this.setState({
711
+ loading: false,
712
+ data: null,
713
+ error: typeof e === "string" ? e : e.message
714
+ });
715
+ }
984
716
 
985
- return this.state.loading !== nextState.loading || this.state.data !== nextState.data || this.state.error !== nextState.error;
986
- }
987
- }, {
988
- key: "componentWillUnmount",
989
- value: function componentWillUnmount() {
990
- this._mounted = false;
991
- }
992
- }, {
993
- key: "_propsMatch",
994
- value: function _propsMatch(otherProps) {
995
- var _this$props = this.props,
996
- handler = _this$props.handler,
997
- options = _this$props.options;
998
- var prevHandler = otherProps.handler,
999
- prevOptions = otherProps.options;
1000
- return handler === prevHandler && handler.getKey(options) === prevHandler.getKey(prevOptions);
717
+ return null;
718
+ });
1001
719
  }
1002
- }, {
1003
- key: "_buildStateAndfulfillNeeds",
1004
- value: function _buildStateAndfulfillNeeds(propsAtFulfillment) {
1005
- var _this2 = this;
720
+ /**
721
+ * This is the default response for the server and for the initial
722
+ * client-side render if we have cachedData.
723
+ *
724
+ * This ensures we don't make promises we don't want when doing
725
+ * server-side rendering. Instead, we either have data from the cache
726
+ * or we don't.
727
+ */
1006
728
 
1007
- var getEntry = propsAtFulfillment.getEntry,
1008
- handler = propsAtFulfillment.handler,
1009
- options = propsAtFulfillment.options;
1010
- var cachedData = getEntry(handler, options);
1011
729
 
1012
- if (!Server.isServerSide() && (cachedData == null || handler.shouldRefreshCache(options, cachedData))) {
1013
- /**
1014
- * We're not on the server, the cache missed, or our handler says
1015
- * we should refresh the cache.
1016
- *
1017
- * Therefore, we need to request data.
1018
- *
1019
- * We have to do this here from the constructor so that this
1020
- * data request is tracked when performing server-side rendering.
1021
- */
1022
- RequestFulfillment.Default.fulfill(handler, options).then(function (cacheEntry) {
1023
- /**
1024
- * We get here, we should have updated the cache.
1025
- * However, we need to update the component, but we
1026
- * should only do that if the props are the same as they
1027
- * were when this was called.
1028
- */
1029
- if (_this2._mounted && _this2._propsMatch(propsAtFulfillment)) {
1030
- _this2.setState({
1031
- loading: false,
1032
- data: cacheEntry.data,
1033
- error: cacheEntry.error
1034
- });
1035
- }
1036
-
1037
- return null;
1038
- }).catch(function (e) {
1039
- /**
1040
- * We should never get here, but if we do.
1041
- */
1042
- // eslint-disable-next-line no-console
1043
- console.error("Unexpected error occurred during data fulfillment: ".concat(e));
1044
-
1045
- if (_this2._mounted && _this2._propsMatch(propsAtFulfillment)) {
1046
- _this2.setState({
1047
- loading: false,
1048
- data: null,
1049
- error: typeof e === "string" ? e : e.message
1050
- });
1051
- }
1052
-
1053
- return null;
1054
- });
1055
- }
1056
- /**
1057
- * This is the default response for the server and for the initial
1058
- * client-side render if we have cachedData.
1059
- *
1060
- * This ensures we don't make promises we don't want when doing
1061
- * server-side rendering. Instead, we either have data from the cache
1062
- * or we don't.
1063
- */
730
+ return {
731
+ loading: cachedData == null,
732
+ data: cachedData && cachedData.data,
733
+ error: cachedData && cachedData.error
734
+ };
735
+ }
1064
736
 
737
+ _resultFromState() {
738
+ const {
739
+ loading,
740
+ data,
741
+ error
742
+ } = this.state;
1065
743
 
744
+ if (loading) {
1066
745
  return {
1067
- loading: cachedData == null,
1068
- data: cachedData && cachedData.data,
1069
- error: cachedData && cachedData.error
746
+ loading: true
1070
747
  };
1071
748
  }
1072
- }, {
1073
- key: "_resultFromState",
1074
- value: function _resultFromState() {
1075
- var _this$state = this.state,
1076
- loading = _this$state.loading,
1077
- data = _this$state.data,
1078
- error = _this$state.error;
1079
-
1080
- if (loading) {
1081
- return {
1082
- loading: true
1083
- };
1084
- }
1085
-
1086
- if (data != null) {
1087
- return {
1088
- loading: false,
1089
- data: data
1090
- };
1091
- }
1092
-
1093
- if (error == null) {
1094
- // We should never get here ever.
1095
- throw new Error("Loaded result has invalid state where data and error are missing");
1096
- }
1097
749
 
750
+ if (data != null) {
1098
751
  return {
1099
752
  loading: false,
1100
- error: error
753
+ data
1101
754
  };
1102
755
  }
1103
- }, {
1104
- key: "_renderContent",
1105
- value: function _renderContent(result) {
1106
- var children = this.props.children;
1107
- return children(result);
1108
- }
1109
- }, {
1110
- key: "_renderWithTrackingContext",
1111
- value: function _renderWithTrackingContext(result) {
1112
- var _this3 = this;
1113
756
 
1114
- return /*#__PURE__*/createElement(TrackerContext.Consumer, null, function (track) {
1115
- /**
1116
- * If data tracking wasn't enabled, don't do it.
1117
- */
1118
- if (track != null) {
1119
- track(_this3.props.handler, _this3.props.options);
1120
- }
1121
-
1122
- return _this3._renderContent(result);
1123
- });
757
+ if (error == null) {
758
+ // We should never get here ever.
759
+ throw new Error("Loaded result has invalid state where data and error are missing");
1124
760
  }
1125
- }, {
1126
- key: "render",
1127
- value: function render() {
1128
- var result = this._resultFromState(); // We only track data requests when we are server-side and we don't
1129
- // already have a result. The existence of a result is indicated by the
1130
- // loading flag being false.
1131
761
 
762
+ return {
763
+ loading: false,
764
+ error
765
+ };
766
+ }
767
+
768
+ _renderContent(result) {
769
+ const {
770
+ children
771
+ } = this.props;
772
+ return children(result);
773
+ }
1132
774
 
1133
- if (result.loading && Server.isServerSide()) {
1134
- return this._renderWithTrackingContext(result);
775
+ _renderWithTrackingContext(result) {
776
+ return /*#__PURE__*/createElement(TrackerContext.Consumer, null, track => {
777
+ /**
778
+ * If data tracking wasn't enabled, don't do it.
779
+ */
780
+ if (track != null) {
781
+ track(this.props.handler, this.props.options);
1135
782
  }
1136
783
 
1137
784
  return this._renderContent(result);
785
+ });
786
+ }
787
+
788
+ render() {
789
+ const result = this._resultFromState(); // We only track data requests when we are server-side and we don't
790
+ // already have a result. The existence of a result is indicated by the
791
+ // loading flag being false.
792
+
793
+
794
+ if (result.loading && Server.isServerSide()) {
795
+ return this._renderWithTrackingContext(result);
1138
796
  }
1139
- }]);
1140
797
 
1141
- return InternalData;
1142
- }(Component);
798
+ return this._renderContent(result);
799
+ }
800
+
801
+ }
1143
802
 
1144
803
  /**
1145
804
  * InterceptContext defines a map from handler type to interception methods.
1146
805
  *
1147
806
  * INTERNAL USE ONLY
1148
807
  */
1149
- var InterceptContext = createContext({});
808
+ const InterceptContext = /*#__PURE__*/createContext({});
1150
809
 
1151
810
  /**
1152
811
  * This component is the main component of Wonder Blocks Data. With this, data
1153
812
  * requirements can be placed in a React application in a manner that will
1154
813
  * support server-side rendering and efficient caching.
1155
814
  */
1156
- var Data = /*#__PURE__*/function (_React$Component) {
1157
- _inherits(Data, _React$Component);
1158
-
1159
- var _super = _createSuper(Data);
1160
-
1161
- function Data() {
1162
- _classCallCheck(this, Data);
1163
-
1164
- return _super.apply(this, arguments);
815
+ class Data extends Component {
816
+ _getHandlerFromInterceptor(interceptor) {
817
+ const {
818
+ handler
819
+ } = this.props;
820
+
821
+ if (!interceptor) {
822
+ return handler;
823
+ }
824
+
825
+ const {
826
+ fulfillRequest,
827
+ shouldRefreshCache
828
+ } = interceptor;
829
+ const fulfillRequestFn = fulfillRequest ? options => {
830
+ const interceptedResult = fulfillRequest(options);
831
+ return interceptedResult != null ? interceptedResult : handler.fulfillRequest(options);
832
+ } : options => handler.fulfillRequest(options);
833
+ const shouldRefreshCacheFn = shouldRefreshCache ? (options, cacheEntry) => {
834
+ const interceptedResult = shouldRefreshCache(options, cacheEntry);
835
+ return interceptedResult != null ? interceptedResult : handler.shouldRefreshCache(options, cacheEntry);
836
+ } : (options, cacheEntry) => handler.shouldRefreshCache(options, cacheEntry);
837
+ return {
838
+ fulfillRequest: fulfillRequestFn,
839
+ shouldRefreshCache: shouldRefreshCacheFn,
840
+ getKey: options => handler.getKey(options),
841
+ type: handler.type,
842
+ cache: handler.cache,
843
+ hydrate: handler.hydrate
844
+ };
1165
845
  }
1166
846
 
1167
- _createClass(Data, [{
1168
- key: "_getHandlerFromInterceptor",
1169
- value: function _getHandlerFromInterceptor(interceptor) {
1170
- var handler = this.props.handler;
847
+ _getCacheLookupFnFromInterceptor(interceptor) {
848
+ const getEntry = interceptor && interceptor.getEntry;
1171
849
 
1172
- if (!interceptor) {
1173
- return handler;
1174
- }
1175
-
1176
- var fulfillRequest = interceptor.fulfillRequest,
1177
- shouldRefreshCache = interceptor.shouldRefreshCache;
1178
- var fulfillRequestFn = fulfillRequest ? function (options) {
1179
- var interceptedResult = fulfillRequest(options);
1180
- return interceptedResult != null ? interceptedResult : handler.fulfillRequest(options);
1181
- } : function (options) {
1182
- return handler.fulfillRequest(options);
1183
- };
1184
- var shouldRefreshCacheFn = shouldRefreshCache ? function (options, cacheEntry) {
1185
- var interceptedResult = shouldRefreshCache(options, cacheEntry);
1186
- return interceptedResult != null ? interceptedResult : handler.shouldRefreshCache(options, cacheEntry);
1187
- } : function (options, cacheEntry) {
1188
- return handler.shouldRefreshCache(options, cacheEntry);
1189
- };
1190
- return {
1191
- fulfillRequest: fulfillRequestFn,
1192
- shouldRefreshCache: shouldRefreshCacheFn,
1193
- getKey: function getKey(options) {
1194
- return handler.getKey(options);
1195
- },
1196
- type: handler.type,
1197
- cache: handler.cache,
1198
- hydrate: handler.hydrate
1199
- };
850
+ if (!getEntry) {
851
+ return ResponseCache.Default.getEntry;
1200
852
  }
1201
- }, {
1202
- key: "_getCacheLookupFnFromInterceptor",
1203
- value: function _getCacheLookupFnFromInterceptor(interceptor) {
1204
- var getEntry = interceptor && interceptor.getEntry;
1205
-
1206
- if (!getEntry) {
1207
- return ResponseCache.Default.getEntry;
1208
- }
1209
853
 
1210
- return function (handler, options) {
1211
- // 1. Lookup the current cache value.
1212
- var cacheEntry = ResponseCache.Default.getEntry(handler, options); // 2. See if our interceptor wants to override it.
854
+ return (handler, options) => {
855
+ // 1. Lookup the current cache value.
856
+ const cacheEntry = ResponseCache.Default.getEntry(handler, options); // 2. See if our interceptor wants to override it.
1213
857
 
1214
- var interceptedData = getEntry(options, cacheEntry); // 3. Return the appropriate response.
858
+ const interceptedData = getEntry(options, cacheEntry); // 3. Return the appropriate response.
1215
859
 
1216
- return interceptedData != null ? interceptedData : cacheEntry;
1217
- };
1218
- }
1219
- }, {
1220
- key: "render",
1221
- value: function render() {
1222
- var _this = this;
860
+ return interceptedData != null ? interceptedData : cacheEntry;
861
+ };
862
+ }
1223
863
 
1224
- return /*#__PURE__*/createElement(InterceptContext.Consumer, null, function (value) {
1225
- var handlerType = _this.props.handler.type;
1226
- var interceptor = value[handlerType];
864
+ render() {
865
+ return /*#__PURE__*/createElement(InterceptContext.Consumer, null, value => {
866
+ const handlerType = this.props.handler.type;
867
+ const interceptor = value[handlerType];
1227
868
 
1228
- var handler = _this._getHandlerFromInterceptor(interceptor);
869
+ const handler = this._getHandlerFromInterceptor(interceptor);
1229
870
 
1230
- var getEntry = _this._getCacheLookupFnFromInterceptor(interceptor);
1231
- /**
1232
- * Need to share our types with InternalData so Flow
1233
- * doesn't need to infer them and find mismatches.
1234
- * However, just deriving a new component creates issues
1235
- * where InternalData starts rerendering too often.
1236
- * Couldn't track down why, so suppressing the error
1237
- * instead.
1238
- */
871
+ const getEntry = this._getCacheLookupFnFromInterceptor(interceptor);
872
+ /**
873
+ * Need to share our types with InternalData so Flow
874
+ * doesn't need to infer them and find mismatches.
875
+ * However, just deriving a new component creates issues
876
+ * where InternalData starts rerendering too often.
877
+ * Couldn't track down why, so suppressing the error
878
+ * instead.
879
+ */
1239
880
 
1240
881
 
1241
- return /*#__PURE__*/createElement(InternalData // $FlowIgnore[incompatible-type-arg]
1242
- , {
1243
- handler: handler,
1244
- options: _this.props.options,
1245
- getEntry: getEntry
1246
- }, function (result) {
1247
- return _this.props.children(result);
1248
- });
1249
- });
1250
- }
1251
- }]);
882
+ return /*#__PURE__*/createElement(InternalData // $FlowIgnore[incompatible-type-arg]
883
+ , {
884
+ handler: handler,
885
+ options: this.props.options,
886
+ getEntry: getEntry
887
+ }, result => this.props.children(result));
888
+ });
889
+ }
1252
890
 
1253
- return Data;
1254
- }(Component);
891
+ }
1255
892
 
1256
893
  /**
1257
894
  * This component provides a mechanism to intercept the data requests for the
@@ -1272,41 +909,27 @@ var Data = /*#__PURE__*/function (_React$Component) {
1272
909
  * new instance will replace this interceptor for its children. All methods
1273
910
  * will be replaced.
1274
911
  */
1275
- var InterceptData = /*#__PURE__*/function (_React$Component) {
1276
- _inherits(InterceptData, _React$Component);
1277
-
1278
- var _super = _createSuper(InterceptData);
912
+ class InterceptData extends Component {
913
+ render() {
914
+ return /*#__PURE__*/createElement(InterceptContext.Consumer, null, value => {
915
+ const handlerType = this.props.handler.type;
916
+
917
+ const interceptor = _extends({}, value[handlerType], {
918
+ fulfillRequest: this.props.fulfillRequest || null,
919
+ shouldRefreshCache: this.props.shouldRefreshCache || null
920
+ });
1279
921
 
1280
- function InterceptData() {
1281
- _classCallCheck(this, InterceptData);
922
+ const newValue = _extends({}, value, {
923
+ [handlerType]: interceptor
924
+ });
1282
925
 
1283
- return _super.apply(this, arguments);
926
+ return /*#__PURE__*/createElement(InterceptContext.Provider, {
927
+ value: newValue
928
+ }, this.props.children);
929
+ });
1284
930
  }
1285
931
 
1286
- _createClass(InterceptData, [{
1287
- key: "render",
1288
- value: function render() {
1289
- var _this = this;
1290
-
1291
- return /*#__PURE__*/createElement(InterceptContext.Consumer, null, function (value) {
1292
- var handlerType = _this.props.handler.type;
1293
-
1294
- var interceptor = _objectSpread2(_objectSpread2({}, value[handlerType]), {}, {
1295
- fulfillRequest: _this.props.fulfillRequest || null,
1296
- shouldRefreshCache: _this.props.shouldRefreshCache || null
1297
- });
1298
-
1299
- var newValue = _objectSpread2(_objectSpread2({}, value), {}, _defineProperty({}, handlerType, interceptor));
1300
-
1301
- return /*#__PURE__*/createElement(InterceptContext.Provider, {
1302
- value: newValue
1303
- }, _this.props.children);
1304
- });
1305
- }
1306
- }]);
1307
-
1308
- return InterceptData;
1309
- }(Component);
932
+ }
1310
933
 
1311
934
  /**
1312
935
  * This component provides a mechanism to intercept cache lookups for the
@@ -1323,63 +946,43 @@ var InterceptData = /*#__PURE__*/function (_React$Component) {
1323
946
  * rendered within this one that intercepts the same handler type, then that
1324
947
  * new instance will replace this interceptor for its children.
1325
948
  */
1326
- var InterceptCache = /*#__PURE__*/function (_React$Component) {
1327
- _inherits(InterceptCache, _React$Component);
949
+ class InterceptCache extends Component {
950
+ render() {
951
+ return /*#__PURE__*/createElement(InterceptContext.Consumer, null, value => {
952
+ const handlerType = this.props.handler.type;
1328
953
 
1329
- var _super = _createSuper(InterceptCache);
954
+ const interceptor = _extends({}, value[handlerType], {
955
+ getEntry: this.props.getEntry
956
+ });
1330
957
 
1331
- function InterceptCache() {
1332
- _classCallCheck(this, InterceptCache);
958
+ const newValue = _extends({}, value, {
959
+ [handlerType]: interceptor
960
+ });
1333
961
 
1334
- return _super.apply(this, arguments);
962
+ return /*#__PURE__*/createElement(InterceptContext.Provider, {
963
+ value: newValue
964
+ }, this.props.children);
965
+ });
1335
966
  }
1336
967
 
1337
- _createClass(InterceptCache, [{
1338
- key: "render",
1339
- value: function render() {
1340
- var _this = this;
1341
-
1342
- return /*#__PURE__*/createElement(InterceptContext.Consumer, null, function (value) {
1343
- var handlerType = _this.props.handler.type;
1344
-
1345
- var interceptor = _objectSpread2(_objectSpread2({}, value[handlerType]), {}, {
1346
- getEntry: _this.props.getEntry
1347
- });
1348
-
1349
- var newValue = _objectSpread2(_objectSpread2({}, value), {}, _defineProperty({}, handlerType, interceptor));
1350
-
1351
- return /*#__PURE__*/createElement(InterceptContext.Provider, {
1352
- value: newValue
1353
- }, _this.props.children);
1354
- });
1355
- }
1356
- }]);
1357
-
1358
- return InterceptCache;
1359
- }(Component);
968
+ }
1360
969
 
1361
- var initializeCache = function initializeCache(source) {
1362
- return ResponseCache.Default.initialize(source);
1363
- };
1364
- var fulfillAllDataRequests = function fulfillAllDataRequests() {
970
+ const initializeCache = source => ResponseCache.Default.initialize(source);
971
+ const fulfillAllDataRequests = () => {
1365
972
  if (!Server.isServerSide()) {
1366
973
  return Promise.reject(new Error("Data requests are not tracked when client-side"));
1367
974
  }
1368
975
 
1369
976
  return RequestTracker.Default.fulfillTrackedRequests();
1370
977
  };
1371
- var hasUnfulfilledRequests = function hasUnfulfilledRequests() {
978
+ const hasUnfulfilledRequests = () => {
1372
979
  if (!Server.isServerSide()) {
1373
980
  throw new Error("Data requests are not tracked when client-side");
1374
981
  }
1375
982
 
1376
983
  return RequestTracker.Default.hasUnfulfilledRequests;
1377
984
  };
1378
- var removeFromCache = function removeFromCache(handler, options) {
1379
- return ResponseCache.Default.remove(handler, options);
1380
- };
1381
- var removeAllFromCache = function removeAllFromCache(handler, predicate) {
1382
- return ResponseCache.Default.removeAll(handler, predicate);
1383
- };
985
+ const removeFromCache = (handler, options) => ResponseCache.Default.remove(handler, options);
986
+ const removeAllFromCache = (handler, predicate) => ResponseCache.Default.removeAll(handler, predicate);
1384
987
 
1385
988
  export { Data, InterceptCache, InterceptData, NoCache, RequestHandler, TrackData, fulfillAllDataRequests, hasUnfulfilledRequests, initializeCache, removeAllFromCache, removeFromCache };