@khanacademy/wonder-blocks-data 3.2.0 → 4.0.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.
- package/CHANGELOG.md +23 -0
- package/dist/es/index.js +356 -332
- package/dist/index.js +507 -456
- package/docs.md +17 -35
- package/package.json +1 -1
- package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +7 -46
- package/src/__tests__/generated-snapshot.test.js +56 -122
- package/src/components/__tests__/data.test.js +372 -297
- package/src/components/__tests__/intercept-data.test.js +6 -30
- package/src/components/data.js +153 -21
- package/src/components/data.md +38 -69
- package/src/components/intercept-context.js +6 -2
- package/src/components/intercept-data.js +40 -51
- package/src/components/intercept-data.md +13 -27
- package/src/components/track-data.md +9 -23
- package/src/hooks/__tests__/__snapshots__/use-shared-cache.test.js.snap +17 -0
- package/src/hooks/__tests__/use-server-effect.test.js +217 -0
- package/src/hooks/__tests__/use-shared-cache.test.js +307 -0
- package/src/hooks/use-server-effect.js +45 -0
- package/src/hooks/use-shared-cache.js +106 -0
- package/src/index.js +15 -19
- package/src/util/__tests__/__snapshots__/scoped-in-memory-cache.test.js.snap +19 -0
- package/src/util/__tests__/request-fulfillment.test.js +42 -85
- package/src/util/__tests__/request-tracking.test.js +72 -191
- package/src/util/__tests__/{result-from-cache-entry.test.js → result-from-cache-response.test.js} +9 -10
- package/src/util/__tests__/scoped-in-memory-cache.test.js +396 -0
- package/src/util/__tests__/ssr-cache.test.js +639 -0
- package/src/util/request-fulfillment.js +36 -44
- package/src/util/request-tracking.js +62 -75
- package/src/util/{result-from-cache-entry.js → result-from-cache-response.js} +10 -13
- package/src/util/scoped-in-memory-cache.js +149 -0
- package/src/util/ssr-cache.js +206 -0
- package/src/util/types.js +43 -108
- package/src/hooks/__tests__/use-data.test.js +0 -826
- package/src/hooks/use-data.js +0 -143
- package/src/util/__tests__/memory-cache.test.js +0 -446
- package/src/util/__tests__/request-handler.test.js +0 -121
- package/src/util/__tests__/response-cache.test.js +0 -879
- package/src/util/memory-cache.js +0 -187
- package/src/util/request-handler.js +0 -42
- package/src/util/request-handler.md +0 -51
- package/src/util/response-cache.js +0 -213
package/dist/index.js
CHANGED
|
@@ -93,12 +93,18 @@ module.exports = require("react");
|
|
|
93
93
|
|
|
94
94
|
/***/ }),
|
|
95
95
|
/* 1 */
|
|
96
|
+
/***/ (function(module, exports) {
|
|
97
|
+
|
|
98
|
+
module.exports = require("@khanacademy/wonder-stuff-core");
|
|
99
|
+
|
|
100
|
+
/***/ }),
|
|
101
|
+
/* 2 */
|
|
96
102
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
97
103
|
|
|
98
104
|
"use strict";
|
|
99
105
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return GqlErrors; });
|
|
100
106
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return GqlError; });
|
|
101
|
-
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
|
|
107
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
|
|
102
108
|
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
103
109
|
|
|
104
110
|
|
|
@@ -130,27 +136,28 @@ class GqlError extends _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0
|
|
|
130
136
|
}
|
|
131
137
|
|
|
132
138
|
/***/ }),
|
|
133
|
-
/*
|
|
139
|
+
/* 3 */
|
|
134
140
|
/***/ (function(module, exports) {
|
|
135
141
|
|
|
136
142
|
module.exports = require("@khanacademy/wonder-blocks-core");
|
|
137
143
|
|
|
138
144
|
/***/ }),
|
|
139
|
-
/*
|
|
145
|
+
/* 4 */
|
|
140
146
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
141
147
|
|
|
142
148
|
"use strict";
|
|
143
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return
|
|
144
|
-
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
|
|
149
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return SsrCache; });
|
|
150
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
|
|
145
151
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
146
|
-
/* harmony import */ var
|
|
147
|
-
|
|
152
|
+
/* harmony import */ var _scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5);
|
|
148
153
|
|
|
149
154
|
|
|
155
|
+
const DefaultScope = "default";
|
|
150
156
|
/**
|
|
151
157
|
* The default instance is stored here.
|
|
152
158
|
* It's created below in the Default() static property.
|
|
153
159
|
*/
|
|
160
|
+
|
|
154
161
|
let _default;
|
|
155
162
|
/**
|
|
156
163
|
* Implements the response cache.
|
|
@@ -159,10 +166,10 @@ let _default;
|
|
|
159
166
|
*/
|
|
160
167
|
|
|
161
168
|
|
|
162
|
-
class
|
|
169
|
+
class SsrCache {
|
|
163
170
|
static get Default() {
|
|
164
171
|
if (!_default) {
|
|
165
|
-
_default = new
|
|
172
|
+
_default = new SsrCache();
|
|
166
173
|
}
|
|
167
174
|
|
|
168
175
|
return _default;
|
|
@@ -174,31 +181,29 @@ class ResponseCache {
|
|
|
174
181
|
throw new Error("Cannot initialize data response cache more than once");
|
|
175
182
|
}
|
|
176
183
|
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
184
|
+
this._hydrationCache = new _scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* ScopedInMemoryCache */ "a"]({
|
|
185
|
+
// $FlowIgnore[incompatible-call]
|
|
186
|
+
[DefaultScope]: source
|
|
187
|
+
});
|
|
182
188
|
};
|
|
183
189
|
|
|
184
|
-
this.cacheData = (
|
|
190
|
+
this.cacheData = (id, data, hydrate) => this._setCachedResponse(id, {
|
|
185
191
|
data
|
|
186
|
-
});
|
|
192
|
+
}, hydrate);
|
|
187
193
|
|
|
188
|
-
this.cacheError = (
|
|
194
|
+
this.cacheError = (id, error, hydrate) => {
|
|
189
195
|
const errorMessage = typeof error === "string" ? error : error.message;
|
|
190
|
-
return this.
|
|
196
|
+
return this._setCachedResponse(id, {
|
|
191
197
|
error: errorMessage
|
|
192
|
-
});
|
|
198
|
+
}, hydrate);
|
|
193
199
|
};
|
|
194
200
|
|
|
195
|
-
this.getEntry =
|
|
201
|
+
this.getEntry = id => {
|
|
202
|
+
var _this$_ssrOnlyCache$g, _this$_ssrOnlyCache;
|
|
203
|
+
|
|
196
204
|
// Get the cached entry for this value.
|
|
197
|
-
//
|
|
198
|
-
|
|
199
|
-
// we use our SSR-only cache. Otherwise, there's no entry to return.
|
|
200
|
-
const cache = handler.hydrate ? this._hydrationCache : _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() ? this._ssrOnlyCache : undefined;
|
|
201
|
-
const internalEntry = cache == null ? void 0 : cache.retrieve(handler, options); // If we are not server-side and we hydrated something, let's clear
|
|
205
|
+
// We first look in the ssr cache and then the hydration cache.
|
|
206
|
+
const internalEntry = (_this$_ssrOnlyCache$g = (_this$_ssrOnlyCache = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache.get(DefaultScope, id)) != null ? _this$_ssrOnlyCache$g : this._hydrationCache.get(DefaultScope, id); // If we are not server-side and we hydrated something, let's clear
|
|
202
207
|
// that from the hydration cache to save memory.
|
|
203
208
|
|
|
204
209
|
if (this._ssrOnlyCache == null && internalEntry != null) {
|
|
@@ -208,48 +213,71 @@ class ResponseCache {
|
|
|
208
213
|
// that's not an expected use-case. If two different places use the
|
|
209
214
|
// same handler and options (i.e. the same request), then the
|
|
210
215
|
// handler should cater to that to ensure they share the result.
|
|
211
|
-
this._hydrationCache.
|
|
212
|
-
}
|
|
216
|
+
this._hydrationCache.purge(DefaultScope, id);
|
|
217
|
+
} // Getting the typing right between the in-memory cache and this
|
|
218
|
+
// is hard. Just telling flow it's OK.
|
|
219
|
+
// $FlowIgnore[incompatible-return]
|
|
220
|
+
|
|
213
221
|
|
|
214
222
|
return internalEntry;
|
|
215
223
|
};
|
|
216
224
|
|
|
217
|
-
this.remove =
|
|
218
|
-
var _this$_ssrOnlyCache$
|
|
225
|
+
this.remove = id => {
|
|
226
|
+
var _this$_ssrOnlyCache$p, _this$_ssrOnlyCache2;
|
|
219
227
|
|
|
220
228
|
// NOTE(somewhatabstract): We could invoke removeAll with a predicate
|
|
221
229
|
// to match the key of the entry we're removing, but that's an
|
|
222
230
|
// inefficient way to remove a single item, so let's not do that.
|
|
223
231
|
// Delete the entry from the appropriate cache.
|
|
224
|
-
return
|
|
232
|
+
return this._hydrationCache.purge(DefaultScope, id) || ((_this$_ssrOnlyCache$p = (_this$_ssrOnlyCache2 = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache2.purge(DefaultScope, id)) != null ? _this$_ssrOnlyCache$p : false);
|
|
225
233
|
};
|
|
226
234
|
|
|
227
|
-
this.removeAll =
|
|
228
|
-
var _this$
|
|
235
|
+
this.removeAll = predicate => {
|
|
236
|
+
var _this$_ssrOnlyCache3;
|
|
229
237
|
|
|
230
|
-
|
|
231
|
-
|
|
238
|
+
const realPredicate = predicate ? // We know what we're putting into the cache so let's assume it
|
|
239
|
+
// conforms.
|
|
240
|
+
// $FlowIgnore[incompatible-call]
|
|
241
|
+
(_, key, cachedEntry) => predicate(key, cachedEntry) : undefined; // Apply the predicate to what we have in our caches.
|
|
242
|
+
|
|
243
|
+
this._hydrationCache.purgeAll(realPredicate);
|
|
244
|
+
|
|
245
|
+
(_this$_ssrOnlyCache3 = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache3.purgeAll(realPredicate);
|
|
232
246
|
};
|
|
233
247
|
|
|
234
248
|
this.cloneHydratableData = () => {
|
|
249
|
+
var _cache$DefaultScope;
|
|
250
|
+
|
|
235
251
|
// We return our hydration cache only.
|
|
236
|
-
|
|
252
|
+
const cache = this._hydrationCache.clone(); // If we're empty, we still want to return an object, so we default
|
|
253
|
+
// to an empty object.
|
|
254
|
+
// We only need the default scope out of our scoped in-memory cache.
|
|
255
|
+
// We know that it conforms to our expectations.
|
|
256
|
+
// $FlowIgnore[incompatible-return]
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
return (_cache$DefaultScope = cache[DefaultScope]) != null ? _cache$DefaultScope : {};
|
|
237
260
|
};
|
|
238
261
|
|
|
239
|
-
this._ssrOnlyCache = _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() ? ssrOnlyCache || new
|
|
240
|
-
this._hydrationCache = hydrationCache || new
|
|
262
|
+
this._ssrOnlyCache = _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() ? ssrOnlyCache || new _scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* ScopedInMemoryCache */ "a"]() : undefined;
|
|
263
|
+
this._hydrationCache = hydrationCache || new _scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* ScopedInMemoryCache */ "a"]();
|
|
241
264
|
}
|
|
242
265
|
|
|
243
|
-
|
|
266
|
+
_setCachedResponse(id, entry, hydrate) {
|
|
244
267
|
const frozenEntry = Object.freeze(entry);
|
|
245
268
|
|
|
246
|
-
if (
|
|
269
|
+
if (_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
247
270
|
// We are server-side.
|
|
248
271
|
// We need to store this value.
|
|
249
|
-
if (
|
|
250
|
-
this._hydrationCache.
|
|
272
|
+
if (hydrate) {
|
|
273
|
+
this._hydrationCache.set(DefaultScope, id, frozenEntry);
|
|
251
274
|
} else {
|
|
252
|
-
|
|
275
|
+
var _this$_ssrOnlyCache4;
|
|
276
|
+
|
|
277
|
+
// Usually, when server-side, this cache will always be present.
|
|
278
|
+
// We do fake server-side in our doc example though, when it
|
|
279
|
+
// won't be.
|
|
280
|
+
(_this$_ssrOnlyCache4 = this._ssrOnlyCache) == null ? void 0 : _this$_ssrOnlyCache4.set(DefaultScope, id, frozenEntry);
|
|
253
281
|
}
|
|
254
282
|
}
|
|
255
283
|
|
|
@@ -265,7 +293,124 @@ class ResponseCache {
|
|
|
265
293
|
}
|
|
266
294
|
|
|
267
295
|
/***/ }),
|
|
268
|
-
/*
|
|
296
|
+
/* 5 */
|
|
297
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
298
|
+
|
|
299
|
+
"use strict";
|
|
300
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return ScopedInMemoryCache; });
|
|
301
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
|
|
302
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Describe an in-memory cache.
|
|
307
|
+
*/
|
|
308
|
+
class ScopedInMemoryCache {
|
|
309
|
+
constructor(initialCache = Object.freeze({})) {
|
|
310
|
+
this.set = (scope, id, value) => {
|
|
311
|
+
var _this$_cache$scope;
|
|
312
|
+
|
|
313
|
+
if (!id || typeof id !== "string") {
|
|
314
|
+
throw new _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["KindError"]("id must be non-empty string", _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["Errors"].InvalidInput);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
if (!scope || typeof scope !== "string") {
|
|
318
|
+
throw new _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["KindError"]("scope must be non-empty string", _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["Errors"].InvalidInput);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
if (typeof value === "function") {
|
|
322
|
+
throw new _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["KindError"]("value must be a non-function value", _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["Errors"].InvalidInput);
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
this._cache[scope] = (_this$_cache$scope = this._cache[scope]) != null ? _this$_cache$scope : {};
|
|
326
|
+
this._cache[scope][id] = Object.freeze(Object(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["clone"])(value));
|
|
327
|
+
};
|
|
328
|
+
|
|
329
|
+
this.get = (scope, id) => {
|
|
330
|
+
var _this$_cache$scope$id, _this$_cache$scope2;
|
|
331
|
+
|
|
332
|
+
return (_this$_cache$scope$id = (_this$_cache$scope2 = this._cache[scope]) == null ? void 0 : _this$_cache$scope2[id]) != null ? _this$_cache$scope$id : null;
|
|
333
|
+
};
|
|
334
|
+
|
|
335
|
+
this.purge = (scope, id) => {
|
|
336
|
+
var _this$_cache$scope3;
|
|
337
|
+
|
|
338
|
+
if (!((_this$_cache$scope3 = this._cache[scope]) != null && _this$_cache$scope3[id])) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
delete this._cache[scope][id];
|
|
343
|
+
|
|
344
|
+
if (Object.keys(this._cache[scope]).length === 0) {
|
|
345
|
+
delete this._cache[scope];
|
|
346
|
+
}
|
|
347
|
+
};
|
|
348
|
+
|
|
349
|
+
this.purgeScope = (scope, predicate) => {
|
|
350
|
+
if (!this._cache[scope]) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
if (predicate == null) {
|
|
355
|
+
delete this._cache[scope];
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
for (const key of Object.keys(this._cache[scope])) {
|
|
360
|
+
if (predicate(key, this._cache[scope][key])) {
|
|
361
|
+
delete this._cache[scope][key];
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
if (Object.keys(this._cache[scope]).length === 0) {
|
|
366
|
+
delete this._cache[scope];
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
this.purgeAll = predicate => {
|
|
371
|
+
if (predicate == null) {
|
|
372
|
+
this._cache = {};
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
for (const scope of Object.keys(this._cache)) {
|
|
377
|
+
this.purgeScope(scope, (id, value) => predicate(scope, id, value));
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
|
|
381
|
+
this.clone = () => {
|
|
382
|
+
try {
|
|
383
|
+
return Object(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["clone"])(this._cache);
|
|
384
|
+
} catch (e) {
|
|
385
|
+
throw new Error(`An error occurred while trying to clone the cache: ${e}`);
|
|
386
|
+
}
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
try {
|
|
390
|
+
this._cache = Object(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["clone"])(initialCache);
|
|
391
|
+
} catch (e) {
|
|
392
|
+
throw new _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["KindError"](`An error occurred trying to initialize from a response cache snapshot: ${e}`, _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["Errors"].InvalidInput);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Indicate if this cache is being used or not.
|
|
397
|
+
*
|
|
398
|
+
* When the cache has entries, returns `true`; otherwise, returns `false`.
|
|
399
|
+
*/
|
|
400
|
+
|
|
401
|
+
|
|
402
|
+
get inUse() {
|
|
403
|
+
return Object.keys(this._cache).length > 0;
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Set a value in the cache.
|
|
407
|
+
*/
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/***/ }),
|
|
413
|
+
/* 6 */
|
|
269
414
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
270
415
|
|
|
271
416
|
"use strict";
|
|
@@ -273,8 +418,8 @@ class ResponseCache {
|
|
|
273
418
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return RequestTracker; });
|
|
274
419
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
275
420
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
276
|
-
/* harmony import */ var
|
|
277
|
-
/* harmony import */ var _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
|
|
421
|
+
/* harmony import */ var _ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(4);
|
|
422
|
+
/* harmony import */ var _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(9);
|
|
278
423
|
|
|
279
424
|
|
|
280
425
|
|
|
@@ -312,48 +457,31 @@ class RequestTracker {
|
|
|
312
457
|
|
|
313
458
|
|
|
314
459
|
constructor(responseCache = undefined) {
|
|
315
|
-
this._trackedHandlers = {};
|
|
316
460
|
this._trackedRequests = {};
|
|
317
461
|
|
|
318
|
-
this.trackDataRequest = (handler,
|
|
319
|
-
const key = handler.getKey(options);
|
|
320
|
-
const type = handler.type;
|
|
321
|
-
/**
|
|
322
|
-
* Make sure we have stored the handler for use when fulfilling requests.
|
|
323
|
-
*/
|
|
324
|
-
|
|
325
|
-
if (this._trackedHandlers[type] == null) {
|
|
326
|
-
this._trackedHandlers[type] = handler;
|
|
327
|
-
this._trackedRequests[type] = {};
|
|
328
|
-
}
|
|
462
|
+
this.trackDataRequest = (id, handler, hydrate) => {
|
|
329
463
|
/**
|
|
330
464
|
* If we don't already have this tracked, then let's track it.
|
|
331
465
|
*/
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
466
|
+
if (this._trackedRequests[id] == null) {
|
|
467
|
+
this._trackedRequests[id] = {
|
|
468
|
+
handler,
|
|
469
|
+
hydrate
|
|
470
|
+
};
|
|
336
471
|
}
|
|
337
472
|
};
|
|
338
473
|
|
|
339
474
|
this.reset = () => {
|
|
340
|
-
this._trackedHandlers = {};
|
|
341
475
|
this._trackedRequests = {};
|
|
342
476
|
};
|
|
343
477
|
|
|
344
478
|
this.fulfillTrackedRequests = () => {
|
|
345
479
|
const promises = [];
|
|
346
480
|
|
|
347
|
-
for (const
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
const requests = this._trackedRequests[handlerType];
|
|
351
|
-
|
|
352
|
-
for (const requestKey of Object.keys(requests)) {
|
|
353
|
-
const promise = this._requestFulfillment.fulfill(handler, requests[requestKey]);
|
|
481
|
+
for (const requestKey of Object.keys(this._trackedRequests)) {
|
|
482
|
+
const promise = this._requestFulfillment.fulfill(requestKey, this._trackedRequests[requestKey]);
|
|
354
483
|
|
|
355
|
-
|
|
356
|
-
}
|
|
484
|
+
promises.push(promise);
|
|
357
485
|
}
|
|
358
486
|
/**
|
|
359
487
|
* Clear out our tracked info.
|
|
@@ -382,7 +510,7 @@ class RequestTracker {
|
|
|
382
510
|
return Promise.all(promises).then(() => this._responseCache.cloneHydratableData());
|
|
383
511
|
};
|
|
384
512
|
|
|
385
|
-
this._responseCache = responseCache ||
|
|
513
|
+
this._responseCache = responseCache || _ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default;
|
|
386
514
|
this._requestFulfillment = new _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestFulfillment */ "a"](responseCache);
|
|
387
515
|
}
|
|
388
516
|
/**
|
|
@@ -409,15 +537,15 @@ class RequestTracker {
|
|
|
409
537
|
* Calling this method marks tracked requests as fulfilled; requests are
|
|
410
538
|
* removed from the list of tracked requests by calling this method.
|
|
411
539
|
*
|
|
412
|
-
* @returns {Promise<
|
|
413
|
-
* as a result of fulfilling the tracked requests.
|
|
540
|
+
* @returns {Promise<ResponseCache>} The promise of the data that was
|
|
541
|
+
* cached as a result of fulfilling the tracked requests.
|
|
414
542
|
*/
|
|
415
543
|
|
|
416
544
|
|
|
417
545
|
}
|
|
418
546
|
|
|
419
547
|
/***/ }),
|
|
420
|
-
/*
|
|
548
|
+
/* 7 */
|
|
421
549
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
422
550
|
|
|
423
551
|
"use strict";
|
|
@@ -426,7 +554,7 @@ class RequestTracker {
|
|
|
426
554
|
|
|
427
555
|
|
|
428
556
|
/**
|
|
429
|
-
* InterceptContext defines a map from
|
|
557
|
+
* InterceptContext defines a map from request ID to interception methods.
|
|
430
558
|
*
|
|
431
559
|
* INTERNAL USE ONLY
|
|
432
560
|
*/
|
|
@@ -434,150 +562,63 @@ const InterceptContext = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["create
|
|
|
434
562
|
/* harmony default export */ __webpack_exports__["a"] = (InterceptContext);
|
|
435
563
|
|
|
436
564
|
/***/ }),
|
|
437
|
-
/*
|
|
565
|
+
/* 8 */
|
|
438
566
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
439
567
|
|
|
440
568
|
"use strict";
|
|
441
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return
|
|
442
|
-
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
|
|
569
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useServerEffect; });
|
|
570
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
|
|
443
571
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
444
572
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(0);
|
|
445
573
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
|
|
446
|
-
/* harmony import */ var
|
|
447
|
-
/* harmony import */ var
|
|
448
|
-
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(4);
|
|
449
|
-
/* harmony import */ var _util_result_from_cache_entry_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(17);
|
|
450
|
-
/* harmony import */ var _util_response_cache_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(3);
|
|
451
|
-
|
|
574
|
+
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6);
|
|
575
|
+
/* harmony import */ var _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4);
|
|
452
576
|
|
|
453
577
|
|
|
454
578
|
|
|
455
579
|
|
|
456
580
|
|
|
457
|
-
|
|
458
|
-
|
|
581
|
+
/**
|
|
582
|
+
* Hook to perform an asynchronous action during server-side rendering.
|
|
583
|
+
*
|
|
584
|
+
* This hook registers an asynchronous action to be performed during
|
|
585
|
+
* server-side rendering. The action is performed only once, and the result
|
|
586
|
+
* is cached against the given identifier so that subsequent calls return that
|
|
587
|
+
* cached result allowing components to render more of the component.
|
|
588
|
+
*
|
|
589
|
+
* This hook requires the Wonder Blocks Data functionality for resolving
|
|
590
|
+
* pending requests, as well as support for the hydration cache to be
|
|
591
|
+
* embedded into a page so that the result can by hydrated (if that is a
|
|
592
|
+
* requirement).
|
|
593
|
+
*
|
|
594
|
+
* The asynchronous action is never invoked on the client-side.
|
|
595
|
+
*/
|
|
596
|
+
const useServerEffect = (id, handler, hydrate = true) => {
|
|
459
597
|
// If we're server-side or hydrating, we'll have a cached entry to use.
|
|
460
598
|
// So we get that and use it to initialize our state.
|
|
461
599
|
// This works in both hydration and SSR because the very first call to
|
|
462
600
|
// this will have cached data in those cases as it will be present on the
|
|
463
601
|
// initial render - and subsequent renders on the client it will be null.
|
|
464
|
-
const cachedResult =
|
|
465
|
-
const [result, setResult] = Object(react__WEBPACK_IMPORTED_MODULE_1__["useState"])(cachedResult); // Lookup to see if there's an interceptor for the handler.
|
|
466
|
-
// If we have one, we need to replace the handler with one that
|
|
467
|
-
// uses the interceptor.
|
|
468
|
-
|
|
469
|
-
const interceptorMap = Object(react__WEBPACK_IMPORTED_MODULE_1__["useContext"])(_components_intercept_context_js__WEBPACK_IMPORTED_MODULE_3__[/* default */ "a"]);
|
|
470
|
-
const interceptor = interceptorMap[handler.type]; // If we have an interceptor, we need to replace the handler with one that
|
|
471
|
-
// uses the interceptor. This helper function generates a new handler.
|
|
472
|
-
// We need this before we track the request as we want the interceptor
|
|
473
|
-
// to also work for tracked requests to simplify testing the server-side
|
|
474
|
-
// request fulfillment.
|
|
475
|
-
|
|
476
|
-
const getMaybeInterceptedHandler = () => {
|
|
477
|
-
if (interceptor == null) {
|
|
478
|
-
return handler;
|
|
479
|
-
}
|
|
480
|
-
|
|
481
|
-
const fulfillRequestFn = options => {
|
|
482
|
-
var _interceptor$fulfillR;
|
|
483
|
-
|
|
484
|
-
return (_interceptor$fulfillR = interceptor.fulfillRequest(options)) != null ? _interceptor$fulfillR : handler.fulfillRequest(options);
|
|
485
|
-
};
|
|
486
|
-
|
|
487
|
-
return {
|
|
488
|
-
fulfillRequest: fulfillRequestFn,
|
|
489
|
-
getKey: options => handler.getKey(options),
|
|
490
|
-
type: handler.type,
|
|
491
|
-
hydrate: handler.hydrate
|
|
492
|
-
};
|
|
493
|
-
}; // We only track data requests when we are server-side and we don't
|
|
602
|
+
const cachedResult = _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_3__[/* SsrCache */ "a"].Default.getEntry(id); // We only track data requests when we are server-side and we don't
|
|
494
603
|
// already have a result, as given by the cachedData (which is also the
|
|
495
604
|
// initial value for the result state).
|
|
496
605
|
|
|
606
|
+
const maybeTrack = Object(react__WEBPACK_IMPORTED_MODULE_1__["useContext"])(_util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__[/* TrackerContext */ "b"]);
|
|
497
607
|
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
maybeTrack == null ? void 0 : maybeTrack(getMaybeInterceptedHandler(), options);
|
|
502
|
-
} // We need to update our request when the handler changes or the key
|
|
503
|
-
// to the options change, so we keep track of those.
|
|
504
|
-
// However, even if we are hydrating from cache, we still need to make the
|
|
505
|
-
// request at least once, so we do not initialize these references.
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
const handlerRef = Object(react__WEBPACK_IMPORTED_MODULE_1__["useRef"])();
|
|
509
|
-
const keyRef = Object(react__WEBPACK_IMPORTED_MODULE_1__["useRef"])();
|
|
510
|
-
const interceptorRef = Object(react__WEBPACK_IMPORTED_MODULE_1__["useRef"])(); // This effect will ensure that we fulfill the request as desired.
|
|
511
|
-
|
|
512
|
-
Object(react__WEBPACK_IMPORTED_MODULE_1__["useEffect"])(() => {
|
|
513
|
-
// If we are server-side, then just skip the effect. We track requests
|
|
514
|
-
// during SSR and fulfill them outside of the React render cycle.
|
|
515
|
-
// NOTE: This shouldn't happen since effects would not run on the server
|
|
516
|
-
// but let's be defensive - I think it makes the code clearer.
|
|
517
|
-
|
|
518
|
-
/* istanbul ignore next */
|
|
519
|
-
if (_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
520
|
-
return;
|
|
521
|
-
} // Update our refs to the current handler and key.
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
handlerRef.current = handler;
|
|
525
|
-
keyRef.current = handler.getKey(options);
|
|
526
|
-
interceptorRef.current = interceptor; // If we're not hydrating a result, we want to make sure we set our
|
|
527
|
-
// result to null so that we're in the loading state.
|
|
528
|
-
|
|
529
|
-
if (cachedResult == null) {
|
|
530
|
-
// Mark ourselves as loading.
|
|
531
|
-
setResult(null);
|
|
532
|
-
} // We aren't server-side, so let's make the request.
|
|
533
|
-
// The request handler is in control of whether that request actually
|
|
534
|
-
// happens or not.
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
let cancel = false;
|
|
538
|
-
_util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestFulfillment */ "a"].Default.fulfill(getMaybeInterceptedHandler(), options).then(updateEntry => {
|
|
539
|
-
if (cancel) {
|
|
540
|
-
return;
|
|
541
|
-
}
|
|
542
|
-
|
|
543
|
-
setResult(updateEntry);
|
|
544
|
-
return;
|
|
545
|
-
}).catch(e => {
|
|
546
|
-
if (cancel) {
|
|
547
|
-
return;
|
|
548
|
-
}
|
|
549
|
-
/**
|
|
550
|
-
* We should never get here as errors in fulfillment are part
|
|
551
|
-
* of the `then`, but if we do.
|
|
552
|
-
*/
|
|
553
|
-
// eslint-disable-next-line no-console
|
|
554
|
-
|
|
608
|
+
if (cachedResult == null && _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
609
|
+
maybeTrack == null ? void 0 : maybeTrack(id, handler, hydrate);
|
|
610
|
+
}
|
|
555
611
|
|
|
556
|
-
|
|
557
|
-
setResult({
|
|
558
|
-
data: null,
|
|
559
|
-
error: typeof e === "string" ? e : e.message
|
|
560
|
-
});
|
|
561
|
-
return;
|
|
562
|
-
});
|
|
563
|
-
return () => {
|
|
564
|
-
cancel = true;
|
|
565
|
-
}; // - handler.getKey is a proxy for options
|
|
566
|
-
// - We don't want to trigger on cachedResult changing, we're
|
|
567
|
-
// just using that as a flag for render state if the other things
|
|
568
|
-
// trigger this effect.
|
|
569
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
570
|
-
}, [handler, handler.getKey(options), interceptor]);
|
|
571
|
-
return Object(_util_result_from_cache_entry_js__WEBPACK_IMPORTED_MODULE_5__[/* resultFromCacheEntry */ "a"])(result);
|
|
612
|
+
return cachedResult;
|
|
572
613
|
};
|
|
573
614
|
|
|
574
615
|
/***/ }),
|
|
575
|
-
/*
|
|
616
|
+
/* 9 */
|
|
576
617
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
577
618
|
|
|
578
619
|
"use strict";
|
|
579
620
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return RequestFulfillment; });
|
|
580
|
-
/* harmony import */ var
|
|
621
|
+
/* harmony import */ var _ssr_cache_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4);
|
|
581
622
|
|
|
582
623
|
|
|
583
624
|
let _default;
|
|
@@ -594,23 +635,14 @@ class RequestFulfillment {
|
|
|
594
635
|
constructor(responseCache = undefined) {
|
|
595
636
|
this._requests = {};
|
|
596
637
|
|
|
597
|
-
this.
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
return this._requests[handler.type];
|
|
603
|
-
};
|
|
604
|
-
|
|
605
|
-
this.fulfill = (handler, options) => {
|
|
606
|
-
const handlerRequests = this._getHandlerSubcache(handler);
|
|
607
|
-
|
|
608
|
-
const key = handler.getKey(options);
|
|
638
|
+
this.fulfill = (id, {
|
|
639
|
+
handler,
|
|
640
|
+
hydrate = true
|
|
641
|
+
}) => {
|
|
609
642
|
/**
|
|
610
643
|
* If we have an inflight request, we'll provide that.
|
|
611
644
|
*/
|
|
612
|
-
|
|
613
|
-
const inflight = handlerRequests[key];
|
|
645
|
+
const inflight = this._requests[id];
|
|
614
646
|
|
|
615
647
|
if (inflight) {
|
|
616
648
|
return inflight;
|
|
@@ -626,43 +658,56 @@ class RequestFulfillment {
|
|
|
626
658
|
} = this._responseCache;
|
|
627
659
|
|
|
628
660
|
try {
|
|
629
|
-
const request = handler
|
|
630
|
-
delete
|
|
661
|
+
const request = handler().then(data => {
|
|
662
|
+
delete this._requests[id];
|
|
663
|
+
|
|
664
|
+
if (data == null) {
|
|
665
|
+
// Request aborted. We won't cache this.
|
|
666
|
+
return null;
|
|
667
|
+
}
|
|
631
668
|
/**
|
|
632
669
|
* Let's cache the data!
|
|
633
670
|
*
|
|
634
671
|
* NOTE: This only caches when we're server side.
|
|
635
672
|
*/
|
|
636
673
|
|
|
637
|
-
|
|
674
|
+
|
|
675
|
+
return cacheData(id, data, hydrate);
|
|
638
676
|
}).catch(error => {
|
|
639
|
-
delete
|
|
677
|
+
delete this._requests[id];
|
|
640
678
|
/**
|
|
641
679
|
* Let's cache the error!
|
|
642
680
|
*
|
|
643
681
|
* NOTE: This only caches when we're server side.
|
|
644
682
|
*/
|
|
645
683
|
|
|
646
|
-
return cacheError(
|
|
684
|
+
return cacheError(id, error, hydrate);
|
|
647
685
|
});
|
|
648
|
-
|
|
686
|
+
this._requests[id] = request;
|
|
649
687
|
return request;
|
|
650
688
|
} catch (e) {
|
|
651
689
|
/**
|
|
652
690
|
* In this case, we don't cache an inflight request, because there
|
|
653
691
|
* really isn't one.
|
|
654
692
|
*/
|
|
655
|
-
return Promise.resolve(cacheError(
|
|
693
|
+
return Promise.resolve(cacheError(id, e, hydrate));
|
|
656
694
|
}
|
|
657
695
|
};
|
|
658
696
|
|
|
659
|
-
this._responseCache = responseCache ||
|
|
697
|
+
this._responseCache = responseCache || _ssr_cache_js__WEBPACK_IMPORTED_MODULE_0__[/* SsrCache */ "a"].Default;
|
|
660
698
|
}
|
|
699
|
+
/**
|
|
700
|
+
* Get a promise of a request for a given handler and options.
|
|
701
|
+
*
|
|
702
|
+
* This will return an inflight request if one exists, otherwise it will
|
|
703
|
+
* make a new request. Inflight requests are deleted once they resolve.
|
|
704
|
+
*/
|
|
705
|
+
|
|
661
706
|
|
|
662
707
|
}
|
|
663
708
|
|
|
664
709
|
/***/ }),
|
|
665
|
-
/*
|
|
710
|
+
/* 10 */
|
|
666
711
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
667
712
|
|
|
668
713
|
"use strict";
|
|
@@ -673,199 +718,95 @@ class RequestFulfillment {
|
|
|
673
718
|
const GqlRouterContext = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["createContext"](null);
|
|
674
719
|
|
|
675
720
|
/***/ }),
|
|
676
|
-
/*
|
|
721
|
+
/* 11 */
|
|
677
722
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
678
723
|
|
|
679
724
|
"use strict";
|
|
680
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
* NOTE: This doesn't work for get/set property accessors.
|
|
688
|
-
*/
|
|
689
|
-
const serializedInitCache = JSON.stringify(source);
|
|
690
|
-
const cloneInitCache = JSON.parse(serializedInitCache);
|
|
691
|
-
return Object.freeze(cloneInitCache);
|
|
692
|
-
}
|
|
693
|
-
/**
|
|
694
|
-
* INTERNAL USE ONLY
|
|
695
|
-
*
|
|
696
|
-
* Special case cache implementation for the memory cache.
|
|
697
|
-
*
|
|
698
|
-
* This is only used within our framework for SSR (see ./response-cache.js).
|
|
699
|
-
*/
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
class MemoryCache {
|
|
703
|
-
constructor(source = null) {
|
|
704
|
-
this.store = (handler, options, entry) => {
|
|
705
|
-
const requestType = handler.type;
|
|
706
|
-
const frozenEntry = Object.freeze(entry); // Ensure we have a cache location for this handler type.
|
|
707
|
-
|
|
708
|
-
this._cache[requestType] = this._cache[requestType] || {}; // Cache the data.
|
|
709
|
-
|
|
710
|
-
const key = handler.getKey(options);
|
|
711
|
-
this._cache[requestType][key] = frozenEntry;
|
|
712
|
-
};
|
|
713
|
-
|
|
714
|
-
this.retrieve = (handler, options) => {
|
|
715
|
-
const requestType = handler.type; // Get the internal subcache for the handler.
|
|
716
|
-
|
|
717
|
-
const handlerCache = this._cache[requestType];
|
|
718
|
-
|
|
719
|
-
if (!handlerCache) {
|
|
720
|
-
return null;
|
|
721
|
-
} // Get the response.
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
const key = handler.getKey(options);
|
|
725
|
-
const internalEntry = handlerCache[key];
|
|
726
|
-
|
|
727
|
-
if (internalEntry == null) {
|
|
728
|
-
return null;
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
return internalEntry;
|
|
732
|
-
};
|
|
733
|
-
|
|
734
|
-
this.remove = (handler, options) => {
|
|
735
|
-
const requestType = handler.type; // NOTE(somewhatabstract): We could invoke removeAll with a predicate
|
|
736
|
-
// to match the key of the entry we're removing, but that's an
|
|
737
|
-
// inefficient way to remove a single item, so let's not do that.
|
|
738
|
-
// Get the internal subcache for the handler.
|
|
739
|
-
|
|
740
|
-
const handlerCache = this._cache[requestType];
|
|
741
|
-
|
|
742
|
-
if (!handlerCache) {
|
|
743
|
-
return false;
|
|
744
|
-
} // Get the entry.
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
const key = handler.getKey(options);
|
|
748
|
-
const internalEntry = handlerCache[key];
|
|
749
|
-
|
|
750
|
-
if (internalEntry == null) {
|
|
751
|
-
return false;
|
|
752
|
-
} // Delete the entry.
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
delete handlerCache[key];
|
|
756
|
-
return true;
|
|
757
|
-
};
|
|
758
|
-
|
|
759
|
-
this.removeAll = (handler, predicate) => {
|
|
760
|
-
const requestType = handler.type; // Get the internal subcache for the handler.
|
|
761
|
-
|
|
762
|
-
const handlerCache = this._cache[requestType];
|
|
763
|
-
|
|
764
|
-
if (!handlerCache) {
|
|
765
|
-
return 0;
|
|
766
|
-
}
|
|
725
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return clearSharedCache; });
|
|
726
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return useSharedCache; });
|
|
727
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
728
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
729
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
|
|
730
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
731
|
+
/* harmony import */ var _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5);
|
|
767
732
|
|
|
768
|
-
let removedCount = 0;
|
|
769
733
|
|
|
770
|
-
if (typeof predicate === "function") {
|
|
771
|
-
// Apply the predicate to what we have cached.
|
|
772
|
-
for (const [key, entry] of Object.entries(handlerCache)) {
|
|
773
|
-
if (predicate(key, entry)) {
|
|
774
|
-
removedCount++;
|
|
775
|
-
delete handlerCache[key];
|
|
776
|
-
}
|
|
777
|
-
}
|
|
778
|
-
} else {
|
|
779
|
-
// We're removing everything so delete the entire subcache.
|
|
780
|
-
removedCount = Object.keys(handlerCache).length;
|
|
781
|
-
delete this._cache[requestType];
|
|
782
|
-
}
|
|
783
734
|
|
|
784
|
-
return removedCount;
|
|
785
|
-
};
|
|
786
735
|
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
736
|
+
/**
|
|
737
|
+
* This is the cache.
|
|
738
|
+
* It's incredibly complex.
|
|
739
|
+
* Very in-memory. So cache. Such complex. Wow.
|
|
740
|
+
*/
|
|
741
|
+
const cache = new _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__[/* ScopedInMemoryCache */ "a"]();
|
|
742
|
+
/**
|
|
743
|
+
* Clear the in-memory cache or a single scope within it.
|
|
744
|
+
*/
|
|
796
745
|
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
const cloneInitCache = deepClone(source);
|
|
805
|
-
Object.assign(this._cache, cloneInitCache);
|
|
806
|
-
} catch (e) {
|
|
807
|
-
throw new Error(`An error occurred trying to initialize from a response cache snapshot: ${e}`);
|
|
808
|
-
}
|
|
809
|
-
}
|
|
746
|
+
const clearSharedCache = (scope = "") => {
|
|
747
|
+
// If we have a valid scope (empty string is falsy), then clear that scope.
|
|
748
|
+
if (scope && typeof scope === "string") {
|
|
749
|
+
cache.purgeScope(scope);
|
|
750
|
+
} else {
|
|
751
|
+
// Just reset the object. This should be sufficient.
|
|
752
|
+
cache.purgeAll();
|
|
810
753
|
}
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
754
|
+
};
|
|
755
|
+
/**
|
|
756
|
+
* Hook to retrieve data from and store data in an in-memory cache.
|
|
757
|
+
*
|
|
758
|
+
* @returns {[?ReadOnlyCacheValue, CacheValueFn]}
|
|
759
|
+
* Returns an array containing the current cache entry (or undefined), a
|
|
760
|
+
* function to set the cache entry (passing null or undefined to this function
|
|
761
|
+
* will delete the entry).
|
|
762
|
+
*
|
|
763
|
+
* To clear a single scope within the cache or the entire cache,
|
|
764
|
+
* the `clearScopedCache` export is available.
|
|
765
|
+
*
|
|
766
|
+
* NOTE: Unlike useState or useReducer, we don't automatically update folks
|
|
767
|
+
* if the value they reference changes. We might add it later (if we need to),
|
|
768
|
+
* but the likelihood here is that things won't be changing in this cache in a
|
|
769
|
+
* way where we would need that. If we do (and likely only in specific
|
|
770
|
+
* circumstances), we should consider adding a simple boolean useState that can
|
|
771
|
+
* be toggled to cause a rerender whenever the referenced cached data changes
|
|
772
|
+
* so that callers can re-render on cache changes. However, we should make
|
|
773
|
+
* sure this toggling is optional - or we could use a callback argument, to
|
|
774
|
+
* achieve this on an as-needed basis.
|
|
775
|
+
*/
|
|
817
776
|
|
|
818
|
-
|
|
819
|
-
|
|
777
|
+
const useSharedCache = (id, scope, initialValue) => {
|
|
778
|
+
// Verify arguments.
|
|
779
|
+
if (!id || typeof id !== "string") {
|
|
780
|
+
throw new _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__["KindError"]("id must be a non-empty string", _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__["Errors"].InvalidInput);
|
|
820
781
|
}
|
|
821
782
|
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
/***/ (function(module, exports) {
|
|
783
|
+
if (!scope || typeof scope !== "string") {
|
|
784
|
+
throw new _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__["KindError"]("scope must be a non-empty string", _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__["Errors"].InvalidInput);
|
|
785
|
+
} // Memoize our APIs.
|
|
786
|
+
// This one allows callers to set or replace the cached value.
|
|
827
787
|
|
|
828
|
-
module.exports = require("@khanacademy/wonder-stuff-core");
|
|
829
788
|
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
789
|
+
const cacheValue = react__WEBPACK_IMPORTED_MODULE_0__["useMemo"](() => value => value == null ? cache.purge(scope, id) : cache.set(scope, id, value), [id, scope]); // We don't memo-ize the current value, just in case the cache was updated
|
|
790
|
+
// since our last run through. Also, our cache does not know what type it
|
|
791
|
+
// stores, so we have to cast it to the type we're exporting. This is a
|
|
792
|
+
// dev time courtesy, rather than a runtime thing.
|
|
793
|
+
// $FlowIgnore[incompatible-type]
|
|
833
794
|
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
/**
|
|
837
|
-
* Base implementation for creating a request handler.
|
|
838
|
-
*
|
|
839
|
-
* Provides a base implementation of the `IRequestHandler` base class for
|
|
840
|
-
* use with the Wonder Blocks Data framework.
|
|
841
|
-
*/
|
|
842
|
-
class RequestHandler {
|
|
843
|
-
constructor(type, hydrate = true) {
|
|
844
|
-
this._type = type;
|
|
845
|
-
this._hydrate = !!hydrate;
|
|
846
|
-
}
|
|
795
|
+
let currentValue = cache.get(scope, id); // If we have an initial value, we need to add it to the cache
|
|
796
|
+
// and use it as our current value.
|
|
847
797
|
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
798
|
+
if (currentValue == null && initialValue !== undefined) {
|
|
799
|
+
// Get the initial value.
|
|
800
|
+
const value = typeof initialValue === "function" ? initialValue() : initialValue; // Update the cache.
|
|
851
801
|
|
|
852
|
-
|
|
853
|
-
return this._hydrate;
|
|
854
|
-
}
|
|
802
|
+
cacheValue(value); // Make sure we return this value as our current value.
|
|
855
803
|
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
return options === undefined ? "undefined" : JSON.stringify(options);
|
|
859
|
-
} catch (e) {
|
|
860
|
-
throw new Error(`Failed to auto-generate key: ${e}`);
|
|
861
|
-
}
|
|
862
|
-
}
|
|
804
|
+
currentValue = value;
|
|
805
|
+
} // Now we have everything, let's return it.
|
|
863
806
|
|
|
864
|
-
fulfillRequest(options) {
|
|
865
|
-
throw new Error("Not implemented");
|
|
866
|
-
}
|
|
867
807
|
|
|
868
|
-
|
|
808
|
+
return [currentValue, cacheValue];
|
|
809
|
+
};
|
|
869
810
|
|
|
870
811
|
/***/ }),
|
|
871
812
|
/* 12 */
|
|
@@ -875,9 +816,9 @@ class RequestHandler {
|
|
|
875
816
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return TrackData; });
|
|
876
817
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
877
818
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
878
|
-
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
819
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3);
|
|
879
820
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
880
|
-
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
|
|
821
|
+
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6);
|
|
881
822
|
|
|
882
823
|
|
|
883
824
|
|
|
@@ -905,7 +846,16 @@ class TrackData extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
|
905
846
|
"use strict";
|
|
906
847
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
907
848
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
908
|
-
/* harmony import */ var
|
|
849
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3);
|
|
850
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
851
|
+
/* harmony import */ var _util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(9);
|
|
852
|
+
/* harmony import */ var _intercept_context_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(7);
|
|
853
|
+
/* harmony import */ var _hooks_use_server_effect_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(8);
|
|
854
|
+
/* harmony import */ var _util_result_from_cache_response_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(17);
|
|
855
|
+
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
|
|
909
859
|
|
|
910
860
|
|
|
911
861
|
|
|
@@ -914,9 +864,110 @@ class TrackData extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
|
914
864
|
* requirements can be placed in a React application in a manner that will
|
|
915
865
|
* support server-side rendering and efficient caching.
|
|
916
866
|
*/
|
|
917
|
-
const Data =
|
|
918
|
-
|
|
919
|
-
|
|
867
|
+
const Data = ({
|
|
868
|
+
requestId,
|
|
869
|
+
handler,
|
|
870
|
+
children,
|
|
871
|
+
hydrate,
|
|
872
|
+
showOldDataWhileLoading,
|
|
873
|
+
alwaysRequestOnHydration
|
|
874
|
+
}) => {
|
|
875
|
+
// Lookup to see if there's an interceptor for the handler.
|
|
876
|
+
// If we have one, we need to replace the handler with one that
|
|
877
|
+
// uses the interceptor.
|
|
878
|
+
const interceptorMap = react__WEBPACK_IMPORTED_MODULE_0__["useContext"](_intercept_context_js__WEBPACK_IMPORTED_MODULE_3__[/* default */ "a"]); // If we have an interceptor, we need to replace the handler with one
|
|
879
|
+
// that uses the interceptor. This helper function generates a new
|
|
880
|
+
// handler.
|
|
881
|
+
|
|
882
|
+
const maybeInterceptedHandler = react__WEBPACK_IMPORTED_MODULE_0__["useMemo"](() => {
|
|
883
|
+
const interceptor = interceptorMap[requestId];
|
|
884
|
+
|
|
885
|
+
if (interceptor == null) {
|
|
886
|
+
return handler;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
return () => {
|
|
890
|
+
var _interceptor;
|
|
891
|
+
|
|
892
|
+
return (_interceptor = interceptor()) != null ? _interceptor : handler();
|
|
893
|
+
};
|
|
894
|
+
}, [handler, interceptorMap, requestId]);
|
|
895
|
+
const hydrateResult = Object(_hooks_use_server_effect_js__WEBPACK_IMPORTED_MODULE_4__[/* useServerEffect */ "a"])(requestId, maybeInterceptedHandler, hydrate);
|
|
896
|
+
const [currentResult, setResult] = react__WEBPACK_IMPORTED_MODULE_0__["useState"](hydrateResult); // Here we make sure the request still occurs client-side as needed.
|
|
897
|
+
// This is for legacy usage that expects this. Eventually we will want
|
|
898
|
+
// to deprecate.
|
|
899
|
+
|
|
900
|
+
react__WEBPACK_IMPORTED_MODULE_0__["useEffect"](() => {
|
|
901
|
+
// This is here until I can do a better documentation example for
|
|
902
|
+
// the TrackData docs.
|
|
903
|
+
// istanbul ignore next
|
|
904
|
+
if (_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__["Server"].isServerSide()) {
|
|
905
|
+
return;
|
|
906
|
+
} // We don't bother with this if we have hydration data and we're not
|
|
907
|
+
// forcing a request on hydration.
|
|
908
|
+
// We don't care if these things change after the first render,
|
|
909
|
+
// so we don't want them in the inputs array.
|
|
910
|
+
|
|
911
|
+
|
|
912
|
+
if (!alwaysRequestOnHydration && (hydrateResult == null ? void 0 : hydrateResult.data) != null) {
|
|
913
|
+
return;
|
|
914
|
+
} // If we're not hydrating a result and we're not going to render
|
|
915
|
+
// with old data until we're loaded, we want to make sure we set our
|
|
916
|
+
// result to null so that we're in the loading state.
|
|
917
|
+
|
|
918
|
+
|
|
919
|
+
if (!showOldDataWhileLoading) {
|
|
920
|
+
// Mark ourselves as loading.
|
|
921
|
+
setResult(null);
|
|
922
|
+
} // We aren't server-side, so let's make the request.
|
|
923
|
+
// We don't need to use our built-in request fulfillment here if we
|
|
924
|
+
// don't want, but it does mean we'll share inflight requests for the
|
|
925
|
+
// same ID and the result will be in the same format as the
|
|
926
|
+
// hydrated value.
|
|
927
|
+
|
|
928
|
+
|
|
929
|
+
let cancel = false;
|
|
930
|
+
_util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestFulfillment */ "a"].Default.fulfill(requestId, {
|
|
931
|
+
handler: maybeInterceptedHandler
|
|
932
|
+
}).then(result => {
|
|
933
|
+
if (cancel) {
|
|
934
|
+
return;
|
|
935
|
+
}
|
|
936
|
+
|
|
937
|
+
setResult(result);
|
|
938
|
+
return;
|
|
939
|
+
}).catch(e => {
|
|
940
|
+
if (cancel) {
|
|
941
|
+
return;
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* We should never get here as errors in fulfillment are part
|
|
945
|
+
* of the `then`, but if we do.
|
|
946
|
+
*/
|
|
947
|
+
// eslint-disable-next-line no-console
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
console.error(`Unexpected error occurred during data fulfillment: ${e}`);
|
|
951
|
+
setResult({
|
|
952
|
+
error: typeof e === "string" ? e : e.message
|
|
953
|
+
});
|
|
954
|
+
return;
|
|
955
|
+
});
|
|
956
|
+
return () => {
|
|
957
|
+
cancel = true;
|
|
958
|
+
}; // If the handler changes, we don't care. The ID is what indicates
|
|
959
|
+
// the request that should be made and folks shouldn't be changing the
|
|
960
|
+
// handler without changing the ID as well.
|
|
961
|
+
// In addition, we don't want to include hydrateResult nor
|
|
962
|
+
// alwaysRequestOnHydration as them changinng after the first pass
|
|
963
|
+
// is irrelevant.
|
|
964
|
+
// Finally, we don't want to include showOldDataWhileLoading as that
|
|
965
|
+
// changing on its own is also not relevant. It only matters if the
|
|
966
|
+
// request itself changes. All of which is to say that we only
|
|
967
|
+
// run this effect for the ID changing.
|
|
968
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
969
|
+
}, [requestId]);
|
|
970
|
+
return children(Object(_util_result_from_cache_response_js__WEBPACK_IMPORTED_MODULE_5__[/* resultFromCachedResponse */ "a"])(currentResult));
|
|
920
971
|
};
|
|
921
972
|
|
|
922
973
|
/* harmony default export */ __webpack_exports__["a"] = (Data);
|
|
@@ -926,46 +977,42 @@ const Data = props => {
|
|
|
926
977
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
927
978
|
|
|
928
979
|
"use strict";
|
|
929
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return InterceptData; });
|
|
930
980
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
931
981
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
932
|
-
/* harmony import */ var _intercept_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
982
|
+
/* harmony import */ var _intercept_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(7);
|
|
933
983
|
|
|
934
984
|
|
|
935
985
|
|
|
936
986
|
/**
|
|
937
|
-
* This component provides a mechanism to intercept
|
|
938
|
-
*
|
|
939
|
-
* useful for testing.
|
|
987
|
+
* This component provides a mechanism to intercept data requests.
|
|
988
|
+
* This is for use in testing.
|
|
940
989
|
*
|
|
941
990
|
* This component is not recommended for use in production code as it
|
|
942
991
|
* can prevent predictable functioning of the Wonder Blocks Data framework.
|
|
943
992
|
* One possible side-effect is that inflight requests from the interceptor could
|
|
944
|
-
* be picked up by `Data` component requests
|
|
945
|
-
*
|
|
993
|
+
* be picked up by `Data` component requests from outside the children of this
|
|
994
|
+
* component.
|
|
946
995
|
*
|
|
947
996
|
* These components do not chain. If a different `InterceptData` instance is
|
|
948
|
-
* rendered within this one that intercepts the same
|
|
997
|
+
* rendered within this one that intercepts the same id, then that
|
|
949
998
|
* new instance will replace this interceptor for its children. All methods
|
|
950
999
|
* will be replaced.
|
|
951
1000
|
*/
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
});
|
|
966
|
-
}
|
|
1001
|
+
const InterceptData = ({
|
|
1002
|
+
requestId,
|
|
1003
|
+
handler,
|
|
1004
|
+
children
|
|
1005
|
+
}) => {
|
|
1006
|
+
const interceptMap = react__WEBPACK_IMPORTED_MODULE_0__["useContext"](_intercept_context_js__WEBPACK_IMPORTED_MODULE_1__[/* default */ "a"]);
|
|
1007
|
+
const updatedInterceptMap = react__WEBPACK_IMPORTED_MODULE_0__["useMemo"](() => ({ ...interceptMap,
|
|
1008
|
+
[requestId]: handler
|
|
1009
|
+
}), [interceptMap, requestId, handler]);
|
|
1010
|
+
return /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["createElement"](_intercept_context_js__WEBPACK_IMPORTED_MODULE_1__[/* default */ "a"].Provider, {
|
|
1011
|
+
value: updatedInterceptMap
|
|
1012
|
+
}, children);
|
|
1013
|
+
};
|
|
967
1014
|
|
|
968
|
-
|
|
1015
|
+
/* harmony default export */ __webpack_exports__["a"] = (InterceptData);
|
|
969
1016
|
|
|
970
1017
|
/***/ }),
|
|
971
1018
|
/* 15 */
|
|
@@ -975,7 +1022,7 @@ class InterceptData extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
|
975
1022
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return GqlRouter; });
|
|
976
1023
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
977
1024
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
978
|
-
/* harmony import */ var _util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
1025
|
+
/* harmony import */ var _util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(10);
|
|
979
1026
|
|
|
980
1027
|
|
|
981
1028
|
|
|
@@ -1019,9 +1066,9 @@ const GqlRouter = ({
|
|
|
1019
1066
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useGql; });
|
|
1020
1067
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1021
1068
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1022
|
-
/* harmony import */ var _util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
1069
|
+
/* harmony import */ var _util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(10);
|
|
1023
1070
|
/* harmony import */ var _util_get_gql_data_from_response_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(18);
|
|
1024
|
-
/* harmony import */ var _util_gql_error_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(
|
|
1071
|
+
/* harmony import */ var _util_gql_error_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(2);
|
|
1025
1072
|
|
|
1026
1073
|
|
|
1027
1074
|
|
|
@@ -1095,11 +1142,11 @@ const useGql = () => {
|
|
|
1095
1142
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1096
1143
|
|
|
1097
1144
|
"use strict";
|
|
1098
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return
|
|
1145
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return resultFromCachedResponse; });
|
|
1099
1146
|
/**
|
|
1100
1147
|
* Turns a cache entry into a stateful result.
|
|
1101
1148
|
*/
|
|
1102
|
-
const
|
|
1149
|
+
const resultFromCachedResponse = cacheEntry => {
|
|
1103
1150
|
// No cache entry means we didn't load one yet.
|
|
1104
1151
|
if (cacheEntry == null) {
|
|
1105
1152
|
return {
|
|
@@ -1112,24 +1159,22 @@ const resultFromCacheEntry = cacheEntry => {
|
|
|
1112
1159
|
error
|
|
1113
1160
|
} = cacheEntry;
|
|
1114
1161
|
|
|
1115
|
-
if (
|
|
1162
|
+
if (error != null) {
|
|
1116
1163
|
return {
|
|
1117
|
-
status: "
|
|
1118
|
-
|
|
1164
|
+
status: "error",
|
|
1165
|
+
error
|
|
1119
1166
|
};
|
|
1120
1167
|
}
|
|
1121
1168
|
|
|
1122
|
-
if (
|
|
1123
|
-
// We should never get here ever.
|
|
1169
|
+
if (data != null) {
|
|
1124
1170
|
return {
|
|
1125
|
-
status: "
|
|
1126
|
-
|
|
1171
|
+
status: "success",
|
|
1172
|
+
data
|
|
1127
1173
|
};
|
|
1128
1174
|
}
|
|
1129
1175
|
|
|
1130
1176
|
return {
|
|
1131
|
-
status: "
|
|
1132
|
-
error
|
|
1177
|
+
status: "aborted"
|
|
1133
1178
|
};
|
|
1134
1179
|
};
|
|
1135
1180
|
|
|
@@ -1139,7 +1184,7 @@ const resultFromCacheEntry = cacheEntry => {
|
|
|
1139
1184
|
|
|
1140
1185
|
"use strict";
|
|
1141
1186
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return getGqlDataFromResponse; });
|
|
1142
|
-
/* harmony import */ var _gql_error_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
|
|
1187
|
+
/* harmony import */ var _gql_error_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
|
|
1143
1188
|
|
|
1144
1189
|
/**
|
|
1145
1190
|
* Validate a GQL operation response and extract the data.
|
|
@@ -1212,40 +1257,45 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1212
1257
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "hasUnfulfilledRequests", function() { return hasUnfulfilledRequests; });
|
|
1213
1258
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removeFromCache", function() { return removeFromCache; });
|
|
1214
1259
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removeAllFromCache", function() { return removeAllFromCache; });
|
|
1215
|
-
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
|
|
1260
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(3);
|
|
1216
1261
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
1217
|
-
/* harmony import */ var
|
|
1218
|
-
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
|
|
1219
|
-
/* harmony import */ var
|
|
1220
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1262
|
+
/* harmony import */ var _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(4);
|
|
1263
|
+
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(6);
|
|
1264
|
+
/* harmony import */ var _components_track_data_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(12);
|
|
1265
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "TrackData", function() { return _components_track_data_js__WEBPACK_IMPORTED_MODULE_3__["a"]; });
|
|
1266
|
+
|
|
1267
|
+
/* harmony import */ var _components_data_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(13);
|
|
1268
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "Data", function() { return _components_data_js__WEBPACK_IMPORTED_MODULE_4__["a"]; });
|
|
1221
1269
|
|
|
1222
|
-
/* harmony import */ var
|
|
1223
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1270
|
+
/* harmony import */ var _components_intercept_data_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(14);
|
|
1271
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "InterceptData", function() { return _components_intercept_data_js__WEBPACK_IMPORTED_MODULE_5__["a"]; });
|
|
1224
1272
|
|
|
1225
|
-
/* harmony import */ var
|
|
1226
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1273
|
+
/* harmony import */ var _hooks_use_server_effect_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(8);
|
|
1274
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useServerEffect", function() { return _hooks_use_server_effect_js__WEBPACK_IMPORTED_MODULE_6__["a"]; });
|
|
1227
1275
|
|
|
1228
|
-
/* harmony import */ var
|
|
1229
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1276
|
+
/* harmony import */ var _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(11);
|
|
1277
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useSharedCache", function() { return _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_7__["b"]; });
|
|
1230
1278
|
|
|
1231
|
-
/* harmony
|
|
1232
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useData", function() { return _hooks_use_data_js__WEBPACK_IMPORTED_MODULE_7__["a"]; });
|
|
1279
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "clearSharedCache", function() { return _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_7__["a"]; });
|
|
1233
1280
|
|
|
1234
|
-
/* harmony import */ var
|
|
1235
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1281
|
+
/* harmony import */ var _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(5);
|
|
1282
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "ScopedInMemoryCache", function() { return _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_8__["a"]; });
|
|
1236
1283
|
|
|
1237
|
-
/* harmony import */ var
|
|
1238
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1284
|
+
/* harmony import */ var _components_gql_router_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(15);
|
|
1285
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GqlRouter", function() { return _components_gql_router_js__WEBPACK_IMPORTED_MODULE_9__["a"]; });
|
|
1239
1286
|
|
|
1240
|
-
/* harmony import */ var
|
|
1241
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1287
|
+
/* harmony import */ var _hooks_use_gql_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(16);
|
|
1288
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useGql", function() { return _hooks_use_gql_js__WEBPACK_IMPORTED_MODULE_10__["a"]; });
|
|
1242
1289
|
|
|
1243
|
-
/* harmony
|
|
1290
|
+
/* harmony import */ var _util_gql_error_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(2);
|
|
1291
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GqlErrors", function() { return _util_gql_error_js__WEBPACK_IMPORTED_MODULE_11__["b"]; });
|
|
1244
1292
|
|
|
1293
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GqlError", function() { return _util_gql_error_js__WEBPACK_IMPORTED_MODULE_11__["a"]; });
|
|
1245
1294
|
|
|
1246
1295
|
|
|
1247
1296
|
|
|
1248
|
-
|
|
1297
|
+
|
|
1298
|
+
const initializeCache = source => _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default.initialize(source);
|
|
1249
1299
|
const fulfillAllDataRequests = () => {
|
|
1250
1300
|
if (!_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
1251
1301
|
return Promise.reject(new Error("Data requests are not tracked when client-side"));
|
|
@@ -1260,8 +1310,9 @@ const hasUnfulfilledRequests = () => {
|
|
|
1260
1310
|
|
|
1261
1311
|
return _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestTracker */ "a"].Default.hasUnfulfilledRequests;
|
|
1262
1312
|
};
|
|
1263
|
-
const removeFromCache =
|
|
1264
|
-
const removeAllFromCache =
|
|
1313
|
+
const removeFromCache = id => _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default.remove(id);
|
|
1314
|
+
const removeAllFromCache = predicate => _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default.removeAll(predicate);
|
|
1315
|
+
|
|
1265
1316
|
|
|
1266
1317
|
|
|
1267
1318
|
|