@khanacademy/wonder-blocks-data 5.0.1 → 7.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 +31 -0
- package/dist/es/index.js +767 -371
- package/dist/index.js +1194 -564
- package/legacy-docs.md +3 -0
- package/package.json +2 -2
- package/src/__docs__/_overview_.stories.mdx +18 -0
- package/src/__docs__/_overview_graphql.stories.mdx +35 -0
- package/src/__docs__/_overview_ssr_.stories.mdx +185 -0
- package/src/__docs__/_overview_testing_.stories.mdx +123 -0
- package/src/__docs__/exports.clear-shared-cache.stories.mdx +20 -0
- package/src/__docs__/exports.data-error.stories.mdx +23 -0
- package/src/__docs__/exports.data-errors.stories.mdx +23 -0
- package/src/{components/data.md → __docs__/exports.data.stories.mdx} +15 -18
- package/src/__docs__/exports.fulfill-all-data-requests.stories.mdx +24 -0
- package/src/__docs__/exports.gql-error.stories.mdx +23 -0
- package/src/__docs__/exports.gql-errors.stories.mdx +20 -0
- package/src/__docs__/exports.gql-router.stories.mdx +29 -0
- package/src/__docs__/exports.has-unfulfilled-requests.stories.mdx +20 -0
- package/src/{components/intercept-requests.md → __docs__/exports.intercept-requests.stories.mdx} +16 -1
- package/src/__docs__/exports.intialize-cache.stories.mdx +29 -0
- package/src/__docs__/exports.remove-all-from-cache.stories.mdx +24 -0
- package/src/__docs__/exports.remove-from-cache.stories.mdx +25 -0
- package/src/__docs__/exports.request-fulfillment.stories.mdx +36 -0
- package/src/__docs__/exports.scoped-in-memory-cache.stories.mdx +92 -0
- package/src/__docs__/exports.serializable-in-memory-cache.stories.mdx +112 -0
- package/src/__docs__/exports.status.stories.mdx +31 -0
- package/src/{components/track-data.md → __docs__/exports.track-data.stories.mdx} +15 -0
- package/src/__docs__/exports.use-cached-effect.stories.mdx +41 -0
- package/src/__docs__/exports.use-gql.stories.mdx +73 -0
- package/src/__docs__/exports.use-hydratable-effect.stories.mdx +43 -0
- package/src/__docs__/exports.use-server-effect.stories.mdx +50 -0
- package/src/__docs__/exports.use-shared-cache.stories.mdx +30 -0
- package/src/__docs__/exports.when-client-side.stories.mdx +33 -0
- package/src/__docs__/types.cached-response.stories.mdx +29 -0
- package/src/__docs__/types.error-options.stories.mdx +21 -0
- package/src/__docs__/types.gql-context.stories.mdx +20 -0
- package/src/__docs__/types.gql-fetch-fn.stories.mdx +24 -0
- package/src/__docs__/types.gql-fetch-options.stories.mdx +24 -0
- package/src/__docs__/types.gql-operation-type.stories.mdx +24 -0
- package/src/__docs__/types.gql-operation.stories.mdx +67 -0
- package/src/__docs__/types.response-cache.stories.mdx +33 -0
- package/src/__docs__/types.result.stories.mdx +39 -0
- package/src/__docs__/types.scoped-cache.stories.mdx +27 -0
- package/src/__docs__/types.valid-cache-data.stories.mdx +23 -0
- package/src/__tests__/__snapshots__/generated-snapshot.test.js.snap +0 -80
- package/src/__tests__/generated-snapshot.test.js +0 -24
- package/src/components/__tests__/data.test.js +149 -128
- package/src/components/data.js +22 -112
- package/src/components/intercept-requests.js +1 -1
- package/src/hooks/__tests__/__snapshots__/use-shared-cache.test.js.snap +8 -8
- package/src/hooks/__tests__/use-cached-effect.test.js +507 -0
- package/src/hooks/__tests__/use-gql-router-context.test.js +133 -0
- package/src/hooks/__tests__/use-gql.test.js +1 -30
- package/src/hooks/__tests__/use-hydratable-effect.test.js +705 -0
- package/src/hooks/__tests__/use-server-effect.test.js +90 -11
- package/src/hooks/use-cached-effect.js +225 -0
- package/src/hooks/use-gql-router-context.js +50 -0
- package/src/hooks/use-gql.js +22 -52
- package/src/hooks/use-hydratable-effect.js +206 -0
- package/src/hooks/use-request-interception.js +20 -23
- package/src/hooks/use-server-effect.js +42 -10
- package/src/hooks/use-shared-cache.js +13 -11
- package/src/index.js +53 -3
- package/src/util/__tests__/__snapshots__/serializable-in-memory-cache.test.js.snap +19 -0
- package/src/util/__tests__/merge-gql-context.test.js +74 -0
- package/src/util/__tests__/request-fulfillment.test.js +23 -42
- package/src/util/__tests__/request-tracking.test.js +26 -7
- package/src/util/__tests__/result-from-cache-response.test.js +19 -5
- package/src/util/__tests__/scoped-in-memory-cache.test.js +6 -85
- package/src/util/__tests__/serializable-in-memory-cache.test.js +398 -0
- package/src/util/__tests__/ssr-cache.test.js +52 -52
- package/src/util/data-error.js +58 -0
- package/src/util/get-gql-data-from-response.js +3 -2
- package/src/util/gql-error.js +19 -11
- package/src/util/merge-gql-context.js +34 -0
- package/src/util/request-fulfillment.js +49 -46
- package/src/util/request-tracking.js +69 -15
- package/src/util/result-from-cache-response.js +12 -16
- package/src/util/scoped-in-memory-cache.js +24 -47
- package/src/util/serializable-in-memory-cache.js +49 -0
- package/src/util/ssr-cache.js +9 -8
- package/src/util/status.js +30 -0
- package/src/util/types.js +18 -1
- package/docs.md +0 -122
package/dist/index.js
CHANGED
|
@@ -82,7 +82,7 @@ module.exports =
|
|
|
82
82
|
/******/
|
|
83
83
|
/******/
|
|
84
84
|
/******/ // Load entry module and return exports
|
|
85
|
-
/******/ return __webpack_require__(__webpack_require__.s =
|
|
85
|
+
/******/ return __webpack_require__(__webpack_require__.s = 27);
|
|
86
86
|
/******/ })
|
|
87
87
|
/************************************************************************/
|
|
88
88
|
/******/ ([
|
|
@@ -93,32 +93,111 @@ module.exports = require("react");
|
|
|
93
93
|
|
|
94
94
|
/***/ }),
|
|
95
95
|
/* 1 */
|
|
96
|
-
/***/ (function(module,
|
|
96
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
97
97
|
|
|
98
|
-
|
|
98
|
+
"use strict";
|
|
99
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return DataErrors; });
|
|
100
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return DataError; });
|
|
101
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6);
|
|
102
|
+
/* 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
|
+
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Error kinds for DataError.
|
|
107
|
+
*/
|
|
108
|
+
const DataErrors = Object.freeze({
|
|
109
|
+
/**
|
|
110
|
+
* The kind of error is not known.
|
|
111
|
+
*/
|
|
112
|
+
Unknown: "Unknown",
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The error is internal to the executing code.
|
|
116
|
+
*/
|
|
117
|
+
Internal: "Internal",
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* There was a problem with the provided input.
|
|
121
|
+
*/
|
|
122
|
+
InvalidInput: "InvalidInput",
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A network error occurred.
|
|
126
|
+
*/
|
|
127
|
+
Network: "Network",
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Response could not be parsed.
|
|
131
|
+
*/
|
|
132
|
+
Parse: "Parse",
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* An error that occurred during SSR and was hydrated from cache
|
|
136
|
+
*/
|
|
137
|
+
Hydrated: "Hydrated"
|
|
138
|
+
});
|
|
139
|
+
/**
|
|
140
|
+
* An error from the Wonder Blocks Data API.
|
|
141
|
+
*
|
|
142
|
+
* Errors of this type will have names of the format:
|
|
143
|
+
* `${kind}DataError`
|
|
144
|
+
*/
|
|
145
|
+
|
|
146
|
+
class DataError extends _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["KindError"] {
|
|
147
|
+
constructor(message, kind, {
|
|
148
|
+
metadata,
|
|
149
|
+
cause
|
|
150
|
+
} = {}) {
|
|
151
|
+
super(message, kind, {
|
|
152
|
+
metadata,
|
|
153
|
+
cause,
|
|
154
|
+
name: "Data"
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
}
|
|
99
159
|
|
|
100
160
|
/***/ }),
|
|
101
161
|
/* 2 */
|
|
162
|
+
/***/ (function(module, exports) {
|
|
163
|
+
|
|
164
|
+
module.exports = require("@khanacademy/wonder-blocks-core");
|
|
165
|
+
|
|
166
|
+
/***/ }),
|
|
167
|
+
/* 3 */
|
|
102
168
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
103
169
|
|
|
104
170
|
"use strict";
|
|
105
171
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return GqlErrors; });
|
|
106
172
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return GqlError; });
|
|
107
|
-
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
|
|
173
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6);
|
|
108
174
|
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
109
175
|
|
|
110
176
|
|
|
111
177
|
/**
|
|
112
178
|
* Error kinds for GqlError.
|
|
113
179
|
*/
|
|
114
|
-
const GqlErrors = Object.freeze({
|
|
115
|
-
|
|
116
|
-
|
|
180
|
+
const GqlErrors = Object.freeze({
|
|
181
|
+
/**
|
|
182
|
+
* An internal framework error.
|
|
183
|
+
*/
|
|
184
|
+
Internal: "Internal",
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Response does not have the correct structure for a GraphQL response.
|
|
188
|
+
*/
|
|
117
189
|
BadResponse: "BadResponse",
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* A valid GraphQL result with errors field in the payload.
|
|
193
|
+
*/
|
|
118
194
|
ErrorResult: "ErrorResult"
|
|
119
195
|
});
|
|
120
196
|
/**
|
|
121
197
|
* An error from the GQL API.
|
|
198
|
+
*
|
|
199
|
+
* Errors of this type will have names of the format:
|
|
200
|
+
* `${kind}GqlError`
|
|
122
201
|
*/
|
|
123
202
|
|
|
124
203
|
class GqlError extends _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["KindError"] {
|
|
@@ -129,27 +208,50 @@ class GqlError extends _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0
|
|
|
129
208
|
super(message, kind, {
|
|
130
209
|
metadata,
|
|
131
210
|
cause,
|
|
132
|
-
|
|
211
|
+
name: "Gql"
|
|
133
212
|
});
|
|
134
213
|
}
|
|
135
214
|
|
|
136
215
|
}
|
|
137
216
|
|
|
138
217
|
/***/ }),
|
|
139
|
-
/*
|
|
140
|
-
/***/ (function(module,
|
|
218
|
+
/* 4 */
|
|
219
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
141
220
|
|
|
142
|
-
|
|
221
|
+
"use strict";
|
|
222
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return Status; });
|
|
223
|
+
const loadingStatus = Object.freeze({
|
|
224
|
+
status: "loading"
|
|
225
|
+
});
|
|
226
|
+
const abortedStatus = Object.freeze({
|
|
227
|
+
status: "aborted"
|
|
228
|
+
});
|
|
229
|
+
/**
|
|
230
|
+
* Create Result<TData> instances with specific statuses.
|
|
231
|
+
*/
|
|
232
|
+
|
|
233
|
+
const Status = Object.freeze({
|
|
234
|
+
loading: () => loadingStatus,
|
|
235
|
+
aborted: () => abortedStatus,
|
|
236
|
+
success: data => ({
|
|
237
|
+
status: "success",
|
|
238
|
+
data
|
|
239
|
+
}),
|
|
240
|
+
error: error => ({
|
|
241
|
+
status: "error",
|
|
242
|
+
error
|
|
243
|
+
})
|
|
244
|
+
});
|
|
143
245
|
|
|
144
246
|
/***/ }),
|
|
145
|
-
/*
|
|
247
|
+
/* 5 */
|
|
146
248
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
147
249
|
|
|
148
250
|
"use strict";
|
|
149
251
|
/* 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__(
|
|
252
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
|
|
151
253
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
152
|
-
/* harmony import */ var
|
|
254
|
+
/* harmony import */ var _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(12);
|
|
153
255
|
|
|
154
256
|
|
|
155
257
|
const DefaultScope = "default";
|
|
@@ -181,7 +283,7 @@ class SsrCache {
|
|
|
181
283
|
throw new Error("Cannot initialize data response cache more than once");
|
|
182
284
|
}
|
|
183
285
|
|
|
184
|
-
this._hydrationCache = new
|
|
286
|
+
this._hydrationCache = new _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SerializableInMemoryCache */ "a"]({
|
|
185
287
|
// $FlowIgnore[incompatible-call]
|
|
186
288
|
[DefaultScope]: source
|
|
187
289
|
});
|
|
@@ -259,8 +361,8 @@ class SsrCache {
|
|
|
259
361
|
return (_cache$DefaultScope = cache[DefaultScope]) != null ? _cache$DefaultScope : {};
|
|
260
362
|
};
|
|
261
363
|
|
|
262
|
-
this._ssrOnlyCache = _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() ? ssrOnlyCache || new
|
|
263
|
-
this._hydrationCache = hydrationCache || new
|
|
364
|
+
this._ssrOnlyCache = _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide() ? ssrOnlyCache || new _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SerializableInMemoryCache */ "a"]() : undefined;
|
|
365
|
+
this._hydrationCache = hydrationCache || new _serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SerializableInMemoryCache */ "a"]();
|
|
264
366
|
}
|
|
265
367
|
|
|
266
368
|
_setCachedResponse(id, entry, hydrate) {
|
|
@@ -293,124 +395,13 @@ class SsrCache {
|
|
|
293
395
|
}
|
|
294
396
|
|
|
295
397
|
/***/ }),
|
|
296
|
-
/*
|
|
297
|
-
/***/ (function(module,
|
|
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
|
-
|
|
398
|
+
/* 6 */
|
|
399
|
+
/***/ (function(module, exports) {
|
|
409
400
|
|
|
410
|
-
|
|
401
|
+
module.exports = require("@khanacademy/wonder-stuff-core");
|
|
411
402
|
|
|
412
403
|
/***/ }),
|
|
413
|
-
/*
|
|
404
|
+
/* 7 */
|
|
414
405
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
415
406
|
|
|
416
407
|
"use strict";
|
|
@@ -418,8 +409,8 @@ class ScopedInMemoryCache {
|
|
|
418
409
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return RequestTracker; });
|
|
419
410
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
420
411
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
421
|
-
/* harmony import */ var _ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
422
|
-
/* harmony import */ var _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
|
|
412
|
+
/* harmony import */ var _ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5);
|
|
413
|
+
/* harmony import */ var _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(10);
|
|
423
414
|
|
|
424
415
|
|
|
425
416
|
|
|
@@ -477,11 +468,55 @@ class RequestTracker {
|
|
|
477
468
|
|
|
478
469
|
this.fulfillTrackedRequests = () => {
|
|
479
470
|
const promises = [];
|
|
471
|
+
const {
|
|
472
|
+
cacheData,
|
|
473
|
+
cacheError
|
|
474
|
+
} = this._responseCache;
|
|
480
475
|
|
|
481
476
|
for (const requestKey of Object.keys(this._trackedRequests)) {
|
|
482
|
-
const
|
|
483
|
-
|
|
484
|
-
|
|
477
|
+
const options = this._trackedRequests[requestKey];
|
|
478
|
+
|
|
479
|
+
try {
|
|
480
|
+
promises.push(this._requestFulfillment.fulfill(requestKey, { ...options
|
|
481
|
+
}).then(result => {
|
|
482
|
+
switch (result.status) {
|
|
483
|
+
case "success":
|
|
484
|
+
/**
|
|
485
|
+
* Let's cache the data!
|
|
486
|
+
*
|
|
487
|
+
* NOTE: This only caches when we're
|
|
488
|
+
* server side.
|
|
489
|
+
*/
|
|
490
|
+
cacheData(requestKey, result.data, options.hydrate);
|
|
491
|
+
break;
|
|
492
|
+
|
|
493
|
+
case "error":
|
|
494
|
+
/**
|
|
495
|
+
* Let's cache the error!
|
|
496
|
+
*
|
|
497
|
+
* NOTE: This only caches when we're
|
|
498
|
+
* server side.
|
|
499
|
+
*/
|
|
500
|
+
cacheError(requestKey, result.error, options.hydrate);
|
|
501
|
+
break;
|
|
502
|
+
} // For status === "loading":
|
|
503
|
+
// Could never get here unless we wrote
|
|
504
|
+
// the code wrong. Rather than bloat
|
|
505
|
+
// code with useless error, just ignore.
|
|
506
|
+
// For status === "aborted":
|
|
507
|
+
// We won't cache this.
|
|
508
|
+
// We don't hydrate aborted requests,
|
|
509
|
+
// so the client would just see them
|
|
510
|
+
// as unfulfilled data.
|
|
511
|
+
|
|
512
|
+
|
|
513
|
+
return;
|
|
514
|
+
}));
|
|
515
|
+
} catch (e) {
|
|
516
|
+
// This captures if there are problems in the code that
|
|
517
|
+
// begins the requests.
|
|
518
|
+
promises.push(Promise.resolve(cacheError(requestKey, e, options.hydrate)));
|
|
519
|
+
}
|
|
485
520
|
}
|
|
486
521
|
/**
|
|
487
522
|
* Clear out our tracked info.
|
|
@@ -489,16 +524,15 @@ class RequestTracker {
|
|
|
489
524
|
* We call this now for a simpler API.
|
|
490
525
|
*
|
|
491
526
|
* If we reset the tracked calls after all promises resolve, any
|
|
492
|
-
*
|
|
527
|
+
* request tracking done while promises are in flight would be lost.
|
|
493
528
|
*
|
|
494
529
|
* If we don't reset at all, then we have to expose the `reset` call
|
|
495
530
|
* for consumers to use, or they'll only ever be able to accumulate
|
|
496
531
|
* more and more tracked requests, having to fulfill them all every
|
|
497
532
|
* time.
|
|
498
533
|
*
|
|
499
|
-
* Calling it here means we can have multiple "track -> request"
|
|
500
|
-
* in a row and in an easy to reason about manner.
|
|
501
|
-
*
|
|
534
|
+
* Calling it here means we can have multiple "track -> request"
|
|
535
|
+
* cycles in a row and in an easy to reason about manner.
|
|
502
536
|
*/
|
|
503
537
|
|
|
504
538
|
|
|
@@ -511,7 +545,7 @@ class RequestTracker {
|
|
|
511
545
|
};
|
|
512
546
|
|
|
513
547
|
this._responseCache = responseCache || _ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default;
|
|
514
|
-
this._requestFulfillment = new _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestFulfillment */ "a"](
|
|
548
|
+
this._requestFulfillment = new _request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestFulfillment */ "a"]();
|
|
515
549
|
}
|
|
516
550
|
/**
|
|
517
551
|
* Track a request.
|
|
@@ -545,71 +579,96 @@ class RequestTracker {
|
|
|
545
579
|
}
|
|
546
580
|
|
|
547
581
|
/***/ }),
|
|
548
|
-
/*
|
|
582
|
+
/* 8 */
|
|
549
583
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
550
584
|
|
|
551
585
|
"use strict";
|
|
586
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return clearSharedCache; });
|
|
587
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return useSharedCache; });
|
|
552
588
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
553
589
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
590
|
+
/* harmony import */ var _util_data_error_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
|
|
591
|
+
/* harmony import */ var _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(9);
|
|
592
|
+
|
|
593
|
+
|
|
554
594
|
|
|
555
595
|
|
|
556
596
|
/**
|
|
557
|
-
*
|
|
597
|
+
* This is the cache.
|
|
598
|
+
* It's incredibly complex.
|
|
599
|
+
* Very in-memory. So cache. Such complex. Wow.
|
|
600
|
+
*/
|
|
601
|
+
const cache = new _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__[/* ScopedInMemoryCache */ "a"]();
|
|
602
|
+
/**
|
|
603
|
+
* Clear the in-memory cache or a single scope within it.
|
|
604
|
+
*/
|
|
605
|
+
|
|
606
|
+
const clearSharedCache = (scope = "") => {
|
|
607
|
+
// If we have a valid scope (empty string is falsy), then clear that scope.
|
|
608
|
+
if (scope && typeof scope === "string") {
|
|
609
|
+
cache.purgeScope(scope);
|
|
610
|
+
} else {
|
|
611
|
+
// Just reset the object. This should be sufficient.
|
|
612
|
+
cache.purgeAll();
|
|
613
|
+
}
|
|
614
|
+
};
|
|
615
|
+
/**
|
|
616
|
+
* Hook to retrieve data from and store data in an in-memory cache.
|
|
558
617
|
*
|
|
559
|
-
*
|
|
618
|
+
* @returns {[?ReadOnlyCacheValue, CacheValueFn]}
|
|
619
|
+
* Returns an array containing the current cache entry (or undefined), a
|
|
620
|
+
* function to set the cache entry (passing null or undefined to this function
|
|
621
|
+
* will delete the entry).
|
|
622
|
+
*
|
|
623
|
+
* To clear a single scope within the cache or the entire cache,
|
|
624
|
+
* the `clearScopedCache` export is available.
|
|
625
|
+
*
|
|
626
|
+
* NOTE: Unlike useState or useReducer, we don't automatically update folks
|
|
627
|
+
* if the value they reference changes. We might add it later (if we need to),
|
|
628
|
+
* but the likelihood here is that things won't be changing in this cache in a
|
|
629
|
+
* way where we would need that. If we do (and likely only in specific
|
|
630
|
+
* circumstances), we should consider adding a simple boolean useState that can
|
|
631
|
+
* be toggled to cause a rerender whenever the referenced cached data changes
|
|
632
|
+
* so that callers can re-render on cache changes. However, we should make
|
|
633
|
+
* sure this toggling is optional - or we could use a callback argument, to
|
|
634
|
+
* achieve this on an as-needed basis.
|
|
560
635
|
*/
|
|
561
|
-
const InterceptContext = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["createContext"]([]);
|
|
562
|
-
/* harmony default export */ __webpack_exports__["a"] = (InterceptContext);
|
|
563
636
|
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
637
|
+
const useSharedCache = (id, scope, initialValue) => {
|
|
638
|
+
// Verify arguments.
|
|
639
|
+
if (!id || typeof id !== "string") {
|
|
640
|
+
throw new _util_data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataError */ "a"]("id must be a non-empty string", _util_data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataErrors */ "b"].InvalidInput);
|
|
641
|
+
}
|
|
567
642
|
|
|
568
|
-
"
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(0);
|
|
573
|
-
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
|
|
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);
|
|
643
|
+
if (!scope || typeof scope !== "string") {
|
|
644
|
+
throw new _util_data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataError */ "a"]("scope must be a non-empty string", _util_data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataErrors */ "b"].InvalidInput);
|
|
645
|
+
} // Memoize our APIs.
|
|
646
|
+
// This one allows callers to set or replace the cached value.
|
|
576
647
|
|
|
577
648
|
|
|
649
|
+
const cacheValue = react__WEBPACK_IMPORTED_MODULE_0__["useCallback"](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
|
|
650
|
+
// since our last run through. Also, our cache does not know what type it
|
|
651
|
+
// stores, so we have to cast it to the type we're exporting. This is a
|
|
652
|
+
// dev time courtesy, rather than a runtime thing.
|
|
653
|
+
// $FlowIgnore[incompatible-type]
|
|
578
654
|
|
|
655
|
+
let currentValue = cache.get(scope, id); // If we have an initial value, we need to add it to the cache
|
|
656
|
+
// and use it as our current value.
|
|
579
657
|
|
|
658
|
+
if (currentValue == null && initialValue !== undefined) {
|
|
659
|
+
// Get the initial value.
|
|
660
|
+
const value = typeof initialValue === "function" ? initialValue() : initialValue;
|
|
580
661
|
|
|
581
|
-
|
|
582
|
-
|
|
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 = (requestId, handler, hydrate = true) => {
|
|
597
|
-
// If we're server-side or hydrating, we'll have a cached entry to use.
|
|
598
|
-
// So we get that and use it to initialize our state.
|
|
599
|
-
// This works in both hydration and SSR because the very first call to
|
|
600
|
-
// this will have cached data in those cases as it will be present on the
|
|
601
|
-
// initial render - and subsequent renders on the client it will be null.
|
|
602
|
-
const cachedResult = _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_3__[/* SsrCache */ "a"].Default.getEntry(requestId); // We only track data requests when we are server-side and we don't
|
|
603
|
-
// already have a result, as given by the cachedData (which is also the
|
|
604
|
-
// initial value for the result state).
|
|
662
|
+
if (value != null) {
|
|
663
|
+
// Update the cache.
|
|
664
|
+
cacheValue(value); // Make sure we return this value as our current value.
|
|
605
665
|
|
|
606
|
-
|
|
666
|
+
currentValue = value;
|
|
667
|
+
}
|
|
668
|
+
} // Now we have everything, let's return it.
|
|
607
669
|
|
|
608
|
-
if (cachedResult == null && _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
609
|
-
maybeTrack == null ? void 0 : maybeTrack(requestId, handler, hydrate);
|
|
610
|
-
}
|
|
611
670
|
|
|
612
|
-
return
|
|
671
|
+
return [currentValue, cacheValue];
|
|
613
672
|
};
|
|
614
673
|
|
|
615
674
|
/***/ }),
|
|
@@ -617,114 +676,142 @@ const useServerEffect = (requestId, handler, hydrate = true) => {
|
|
|
617
676
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
618
677
|
|
|
619
678
|
"use strict";
|
|
620
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return
|
|
621
|
-
/* harmony import */ var
|
|
622
|
-
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
623
|
-
/* harmony import */ var _components_intercept_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(7);
|
|
624
|
-
|
|
679
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return ScopedInMemoryCache; });
|
|
680
|
+
/* harmony import */ var _data_error_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
|
|
625
681
|
|
|
626
682
|
|
|
627
683
|
/**
|
|
628
|
-
*
|
|
629
|
-
*
|
|
630
|
-
* Hook to take a uniquely identified request handler and return a
|
|
631
|
-
* method that will support request interception from the InterceptRequest
|
|
632
|
-
* component.
|
|
633
|
-
*
|
|
634
|
-
* If you want request interception to be supported with `useServerEffect` or
|
|
635
|
-
* any client-side effect that uses the handler, call this first to generate
|
|
636
|
-
* an intercepted handler, and then invoke `useServerEffect` (or other things)
|
|
637
|
-
* with that intercepted handler.
|
|
684
|
+
* Describe an in-memory cache.
|
|
638
685
|
*/
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
686
|
+
class ScopedInMemoryCache {
|
|
687
|
+
constructor(initialCache = {}) {
|
|
688
|
+
this._cache = initialCache;
|
|
689
|
+
}
|
|
690
|
+
/**
|
|
691
|
+
* Indicate if this cache is being used or not.
|
|
692
|
+
*
|
|
693
|
+
* When the cache has entries, returns `true`; otherwise, returns `false`.
|
|
694
|
+
*/
|
|
646
695
|
|
|
647
|
-
const interceptedHandler = react__WEBPACK_IMPORTED_MODULE_0__["useMemo"](() => () => {
|
|
648
|
-
// Call the interceptors from closest to furthest.
|
|
649
|
-
// If one returns a non-null result, then we keep that.
|
|
650
|
-
const interceptResponse = interceptors.reduceRight((prev, interceptor) => {
|
|
651
|
-
if (prev != null) {
|
|
652
|
-
return prev;
|
|
653
|
-
}
|
|
654
696
|
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
697
|
+
get inUse() {
|
|
698
|
+
return Object.keys(this._cache).length > 0;
|
|
699
|
+
}
|
|
700
|
+
/**
|
|
701
|
+
* Set a value in the cache.
|
|
702
|
+
*/
|
|
660
703
|
|
|
661
|
-
return interceptResponse != null ? interceptResponse : handler();
|
|
662
|
-
}, [handler, interceptors, requestId]);
|
|
663
|
-
return interceptedHandler;
|
|
664
|
-
};
|
|
665
704
|
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
705
|
+
set(scope, id, value) {
|
|
706
|
+
var _this$_cache$scope;
|
|
669
707
|
|
|
670
|
-
"
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
}
|
|
708
|
+
if (!id || typeof id !== "string") {
|
|
709
|
+
throw new _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataError */ "a"]("id must be non-empty string", _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataErrors */ "b"].InvalidInput);
|
|
710
|
+
}
|
|
711
|
+
|
|
712
|
+
if (!scope || typeof scope !== "string") {
|
|
713
|
+
throw new _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataError */ "a"]("scope must be non-empty string", _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataErrors */ "b"].InvalidInput);
|
|
714
|
+
}
|
|
715
|
+
|
|
716
|
+
if (typeof value === "function") {
|
|
717
|
+
throw new _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataError */ "a"]("value must be a non-function value", _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataErrors */ "b"].InvalidInput);
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
this._cache[scope] = (_this$_cache$scope = this._cache[scope]) != null ? _this$_cache$scope : {};
|
|
721
|
+
this._cache[scope][id] = value;
|
|
681
722
|
}
|
|
723
|
+
/**
|
|
724
|
+
* Retrieve a value from the cache.
|
|
725
|
+
*/
|
|
682
726
|
|
|
683
|
-
const {
|
|
684
|
-
data,
|
|
685
|
-
error
|
|
686
|
-
} = cacheEntry;
|
|
687
727
|
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
};
|
|
728
|
+
get(scope, id) {
|
|
729
|
+
var _this$_cache$scope$id, _this$_cache$scope2;
|
|
730
|
+
|
|
731
|
+
return (_this$_cache$scope$id = (_this$_cache$scope2 = this._cache[scope]) == null ? void 0 : _this$_cache$scope2[id]) != null ? _this$_cache$scope$id : null;
|
|
693
732
|
}
|
|
733
|
+
/**
|
|
734
|
+
* Purge an item from the cache.
|
|
735
|
+
*/
|
|
694
736
|
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
737
|
+
|
|
738
|
+
purge(scope, id) {
|
|
739
|
+
var _this$_cache$scope3;
|
|
740
|
+
|
|
741
|
+
if (!((_this$_cache$scope3 = this._cache[scope]) != null && _this$_cache$scope3[id])) {
|
|
742
|
+
return;
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
delete this._cache[scope][id];
|
|
746
|
+
|
|
747
|
+
if (Object.keys(this._cache[scope]).length === 0) {
|
|
748
|
+
delete this._cache[scope];
|
|
749
|
+
}
|
|
700
750
|
}
|
|
751
|
+
/**
|
|
752
|
+
* Purge a scope of items that match the given predicate.
|
|
753
|
+
*
|
|
754
|
+
* If the predicate is omitted, then all items in the scope are purged.
|
|
755
|
+
*/
|
|
701
756
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
757
|
+
|
|
758
|
+
purgeScope(scope, predicate) {
|
|
759
|
+
if (!this._cache[scope]) {
|
|
760
|
+
return;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
if (predicate == null) {
|
|
764
|
+
delete this._cache[scope];
|
|
765
|
+
return;
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
for (const key of Object.keys(this._cache[scope])) {
|
|
769
|
+
if (predicate(key, this._cache[scope][key])) {
|
|
770
|
+
delete this._cache[scope][key];
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
if (Object.keys(this._cache[scope]).length === 0) {
|
|
775
|
+
delete this._cache[scope];
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
/**
|
|
779
|
+
* Purge all items from the cache that match the given predicate.
|
|
780
|
+
*
|
|
781
|
+
* If the predicate is omitted, then all items in the cache are purged.
|
|
782
|
+
*/
|
|
783
|
+
|
|
784
|
+
|
|
785
|
+
purgeAll(predicate) {
|
|
786
|
+
if (predicate == null) {
|
|
787
|
+
this._cache = {};
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
for (const scope of Object.keys(this._cache)) {
|
|
792
|
+
this.purgeScope(scope, (id, value) => predicate(scope, id, value));
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
}
|
|
706
797
|
|
|
707
798
|
/***/ }),
|
|
708
|
-
/*
|
|
799
|
+
/* 10 */
|
|
709
800
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
710
801
|
|
|
711
802
|
"use strict";
|
|
712
803
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return RequestFulfillment; });
|
|
713
|
-
/* harmony import */ var
|
|
804
|
+
/* harmony import */ var _data_error_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
|
|
714
805
|
|
|
715
806
|
|
|
716
807
|
let _default;
|
|
808
|
+
/**
|
|
809
|
+
* This fulfills a request, making sure that in-flight requests are shared.
|
|
810
|
+
*/
|
|
717
811
|
|
|
718
|
-
class RequestFulfillment {
|
|
719
|
-
static get Default() {
|
|
720
|
-
if (!_default) {
|
|
721
|
-
_default = new RequestFulfillment();
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
return _default;
|
|
725
|
-
}
|
|
726
812
|
|
|
727
|
-
|
|
813
|
+
class RequestFulfillment {
|
|
814
|
+
constructor() {
|
|
728
815
|
this._requests = {};
|
|
729
816
|
|
|
730
817
|
this.fulfill = (id, {
|
|
@@ -744,62 +831,495 @@ class RequestFulfillment {
|
|
|
744
831
|
*/
|
|
745
832
|
|
|
746
833
|
|
|
747
|
-
const {
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
}
|
|
834
|
+
const request = handler().then(data => ({
|
|
835
|
+
status: "success",
|
|
836
|
+
data
|
|
837
|
+
})).catch(error => {
|
|
838
|
+
const actualError = typeof error === "string" ? new _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataError */ "a"]("Request failed", _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataErrors */ "b"].Unknown, {
|
|
839
|
+
metadata: {
|
|
840
|
+
unexpectedError: error
|
|
841
|
+
}
|
|
842
|
+
}) : error; // Return aborted result if the request was aborted.
|
|
843
|
+
// The only way to detect this reliably, it seems, is to
|
|
844
|
+
// check the error name and see if it's "AbortError" (this
|
|
845
|
+
// is also what Apollo does).
|
|
846
|
+
// Even then, it's reliant on the handler supporting aborts.
|
|
847
|
+
// TODO(somewhatabstract, FEI-4276): Add first class abort
|
|
848
|
+
// support to the handler API.
|
|
849
|
+
|
|
850
|
+
if (actualError.name === "AbortError") {
|
|
851
|
+
return {
|
|
852
|
+
status: "aborted"
|
|
853
|
+
};
|
|
854
|
+
}
|
|
751
855
|
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
856
|
+
return {
|
|
857
|
+
status: "error",
|
|
858
|
+
error: actualError
|
|
859
|
+
};
|
|
860
|
+
}).finally(() => {
|
|
861
|
+
delete this._requests[id];
|
|
862
|
+
}); // Store the request in our cache.
|
|
755
863
|
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
return null;
|
|
759
|
-
}
|
|
760
|
-
/**
|
|
761
|
-
* Let's cache the data!
|
|
762
|
-
*
|
|
763
|
-
* NOTE: This only caches when we're server side.
|
|
764
|
-
*/
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
return cacheData(id, data, hydrate);
|
|
768
|
-
}).catch(error => {
|
|
769
|
-
delete this._requests[id];
|
|
770
|
-
/**
|
|
771
|
-
* Let's cache the error!
|
|
772
|
-
*
|
|
773
|
-
* NOTE: This only caches when we're server side.
|
|
774
|
-
*/
|
|
775
|
-
|
|
776
|
-
return cacheError(id, error, hydrate);
|
|
777
|
-
});
|
|
778
|
-
this._requests[id] = request;
|
|
779
|
-
return request;
|
|
780
|
-
} catch (e) {
|
|
781
|
-
/**
|
|
782
|
-
* In this case, we don't cache an inflight request, because there
|
|
783
|
-
* really isn't one.
|
|
784
|
-
*/
|
|
785
|
-
return Promise.resolve(cacheError(id, e, hydrate));
|
|
786
|
-
}
|
|
864
|
+
this._requests[id] = request;
|
|
865
|
+
return request;
|
|
787
866
|
};
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
static get Default() {
|
|
870
|
+
if (!_default) {
|
|
871
|
+
_default = new RequestFulfillment();
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
return _default;
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
/***/ }),
|
|
880
|
+
/* 11 */
|
|
881
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
882
|
+
|
|
883
|
+
"use strict";
|
|
884
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return WhenClientSide; });
|
|
885
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return useHydratableEffect; });
|
|
886
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
887
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
888
|
+
/* harmony import */ var _use_server_effect_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(14);
|
|
889
|
+
/* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(8);
|
|
890
|
+
/* harmony import */ var _use_cached_effect_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(15);
|
|
891
|
+
|
|
892
|
+
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
/**
|
|
897
|
+
* Policies to define how a hydratable effect should behave client-side.
|
|
898
|
+
*/
|
|
899
|
+
const WhenClientSide = __webpack_require__(28).Mirrored(["DoNotHydrate", "ExecuteWhenNoResult", "ExecuteWhenNoSuccessResult", "AlwaysExecute"]);
|
|
900
|
+
const DefaultScope = "useHydratableEffect";
|
|
901
|
+
/**
|
|
902
|
+
* Hook to execute an async operation on server and client.
|
|
903
|
+
*
|
|
904
|
+
* This hook executes the given handler on the server and on the client,
|
|
905
|
+
* and, depending on the given options, can hydrate the server-side result.
|
|
906
|
+
*
|
|
907
|
+
* Results are cached on the client so they can be shared between equivalent
|
|
908
|
+
* invocations. Cache changes from one hook instance do not trigger renders
|
|
909
|
+
* in components that use the same requestID.
|
|
910
|
+
*/
|
|
911
|
+
|
|
912
|
+
const useHydratableEffect = (requestId, handler, options = {}) => {
|
|
913
|
+
const {
|
|
914
|
+
clientBehavior = WhenClientSide.ExecuteWhenNoSuccessResult,
|
|
915
|
+
skip = false,
|
|
916
|
+
retainResultOnChange = false,
|
|
917
|
+
onResultChanged,
|
|
918
|
+
scope = DefaultScope
|
|
919
|
+
} = options; // Now we instruct the server to perform the operation.
|
|
920
|
+
// When client-side, this will look up any response for hydration; it does
|
|
921
|
+
// not invoke the handler.
|
|
922
|
+
|
|
923
|
+
const serverResult = Object(_use_server_effect_js__WEBPACK_IMPORTED_MODULE_1__[/* useServerEffect */ "a"])(requestId, handler, {
|
|
924
|
+
// Only hydrate if our behavior isn't telling us not to.
|
|
925
|
+
hydrate: clientBehavior !== WhenClientSide.DoNotHydrate,
|
|
926
|
+
skip
|
|
927
|
+
});
|
|
928
|
+
const getDefaultCacheValue = react__WEBPACK_IMPORTED_MODULE_0__["useCallback"](() => {
|
|
929
|
+
// If we don't have a requestId, it's our first render, the one
|
|
930
|
+
// where we hydrated. So defer to our clientBehavior value.
|
|
931
|
+
switch (clientBehavior) {
|
|
932
|
+
case WhenClientSide.DoNotHydrate:
|
|
933
|
+
case WhenClientSide.AlwaysExecute:
|
|
934
|
+
// Either we weren't hydrating at all, or we don't care
|
|
935
|
+
// if we hydrated something or not, either way, we're
|
|
936
|
+
// doing a request.
|
|
937
|
+
return null;
|
|
938
|
+
|
|
939
|
+
case WhenClientSide.ExecuteWhenNoResult:
|
|
940
|
+
// We only execute if we didn't hydrate something.
|
|
941
|
+
// So, returning the hydration result as default for our
|
|
942
|
+
// cache, will then prevent the cached effect running.
|
|
943
|
+
return serverResult;
|
|
944
|
+
|
|
945
|
+
case WhenClientSide.ExecuteWhenNoSuccessResult:
|
|
946
|
+
// We only execute if we didn't hydrate a success result.
|
|
947
|
+
if ((serverResult == null ? void 0 : serverResult.status) === "success") {
|
|
948
|
+
// So, returning the hydration result as default for our
|
|
949
|
+
// cache, will then prevent the cached effect running.
|
|
950
|
+
return serverResult;
|
|
951
|
+
}
|
|
952
|
+
|
|
953
|
+
return null;
|
|
954
|
+
} // There is no reason for this to change after the first render,
|
|
955
|
+
// you might think, but the function closes around serverResult and if
|
|
956
|
+
// the requestId changes, it still returns the hydrate result of the
|
|
957
|
+
// first render of the previous requestId. This then means that the
|
|
958
|
+
// hydrate result is still the same, and the effect is not re-executed
|
|
959
|
+
// because the cache gets incorrectly defaulted.
|
|
960
|
+
// However, we don't want to bother doing anything with this on
|
|
961
|
+
// client behavior changing since that truly is irrelevant.
|
|
962
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
963
|
+
|
|
964
|
+
}, [serverResult]); // Instead of using state, which would be local to just this hook instance,
|
|
965
|
+
// we use a shared in-memory cache.
|
|
966
|
+
|
|
967
|
+
Object(_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_2__[/* useSharedCache */ "b"])(requestId, // The key of the cached item
|
|
968
|
+
scope, // The scope of the cached items
|
|
969
|
+
getDefaultCacheValue); // When we're client-side, we ultimately want the result from this call.
|
|
970
|
+
|
|
971
|
+
const clientResult = Object(_use_cached_effect_js__WEBPACK_IMPORTED_MODULE_3__[/* useCachedEffect */ "a"])(requestId, handler, {
|
|
972
|
+
skip,
|
|
973
|
+
onResultChanged,
|
|
974
|
+
retainResultOnChange,
|
|
975
|
+
scope
|
|
976
|
+
}); // OK, now which result do we return.
|
|
977
|
+
// Well, we return the serverResult on our very first call and then
|
|
978
|
+
// the clientResult thereafter. The great thing is that after the very
|
|
979
|
+
// first call, the serverResult is going to be `null` anyway.
|
|
980
|
+
|
|
981
|
+
return serverResult != null ? serverResult : clientResult;
|
|
982
|
+
};
|
|
983
|
+
|
|
984
|
+
/***/ }),
|
|
985
|
+
/* 12 */
|
|
986
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
987
|
+
|
|
988
|
+
"use strict";
|
|
989
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return SerializableInMemoryCache; });
|
|
990
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(6);
|
|
991
|
+
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
992
|
+
/* harmony import */ var _data_error_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
|
|
993
|
+
/* harmony import */ var _scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(9);
|
|
994
|
+
|
|
788
995
|
|
|
789
|
-
|
|
996
|
+
|
|
997
|
+
|
|
998
|
+
/**
|
|
999
|
+
* Describe a serializable in-memory cache.
|
|
1000
|
+
*/
|
|
1001
|
+
class SerializableInMemoryCache extends _scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__[/* ScopedInMemoryCache */ "a"] {
|
|
1002
|
+
constructor(initialCache = {}) {
|
|
1003
|
+
try {
|
|
1004
|
+
super(Object(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["clone"])(initialCache));
|
|
1005
|
+
} catch (e) {
|
|
1006
|
+
throw new _data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataError */ "a"](`An error occurred trying to initialize from a response cache snapshot: ${e}`, _data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataErrors */ "b"].InvalidInput);
|
|
1007
|
+
}
|
|
790
1008
|
}
|
|
791
1009
|
/**
|
|
792
|
-
*
|
|
793
|
-
*
|
|
794
|
-
* This will return an inflight request if one exists, otherwise it will
|
|
795
|
-
* make a new request. Inflight requests are deleted once they resolve.
|
|
1010
|
+
* Set a value in the cache.
|
|
796
1011
|
*/
|
|
797
1012
|
|
|
798
1013
|
|
|
1014
|
+
set(scope, id, value) {
|
|
1015
|
+
super.set(scope, id, Object.freeze(Object(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["clone"])(value)));
|
|
1016
|
+
}
|
|
1017
|
+
/**
|
|
1018
|
+
* Clone the cache.
|
|
1019
|
+
*/
|
|
1020
|
+
|
|
1021
|
+
|
|
1022
|
+
clone() {
|
|
1023
|
+
try {
|
|
1024
|
+
return Object(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_0__["clone"])(this._cache);
|
|
1025
|
+
} catch (e) {
|
|
1026
|
+
throw new _data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataError */ "a"]("An error occurred while trying to clone the cache", _data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataErrors */ "b"].Internal, {
|
|
1027
|
+
cause: e
|
|
1028
|
+
});
|
|
1029
|
+
}
|
|
1030
|
+
}
|
|
1031
|
+
|
|
799
1032
|
}
|
|
800
1033
|
|
|
801
1034
|
/***/ }),
|
|
802
|
-
/*
|
|
1035
|
+
/* 13 */
|
|
1036
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1037
|
+
|
|
1038
|
+
"use strict";
|
|
1039
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1040
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1041
|
+
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
* InterceptContext defines a map from request ID to interception methods.
|
|
1045
|
+
*
|
|
1046
|
+
* INTERNAL USE ONLY
|
|
1047
|
+
*/
|
|
1048
|
+
const InterceptContext = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["createContext"]([]);
|
|
1049
|
+
/* harmony default export */ __webpack_exports__["a"] = (InterceptContext);
|
|
1050
|
+
|
|
1051
|
+
/***/ }),
|
|
1052
|
+
/* 14 */
|
|
1053
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1054
|
+
|
|
1055
|
+
"use strict";
|
|
1056
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useServerEffect; });
|
|
1057
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
|
|
1058
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
1059
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(0);
|
|
1060
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
|
|
1061
|
+
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(7);
|
|
1062
|
+
/* harmony import */ var _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(5);
|
|
1063
|
+
/* harmony import */ var _util_result_from_cache_response_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(24);
|
|
1064
|
+
/* harmony import */ var _use_request_interception_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(16);
|
|
1065
|
+
|
|
1066
|
+
|
|
1067
|
+
|
|
1068
|
+
|
|
1069
|
+
|
|
1070
|
+
|
|
1071
|
+
|
|
1072
|
+
/**
|
|
1073
|
+
* Hook to perform an asynchronous action during server-side rendering.
|
|
1074
|
+
*
|
|
1075
|
+
* This hook registers an asynchronous action to be performed during
|
|
1076
|
+
* server-side rendering. The action is performed only once, and the result
|
|
1077
|
+
* is cached against the given identifier so that subsequent calls return that
|
|
1078
|
+
* cached result allowing components to render more of the component.
|
|
1079
|
+
*
|
|
1080
|
+
* This hook requires the Wonder Blocks Data functionality for resolving
|
|
1081
|
+
* pending requests, as well as support for the hydration cache to be
|
|
1082
|
+
* embedded into a page so that the result can by hydrated (if that is a
|
|
1083
|
+
* requirement).
|
|
1084
|
+
*
|
|
1085
|
+
* The asynchronous action is never invoked on the client-side.
|
|
1086
|
+
*/
|
|
1087
|
+
const useServerEffect = (requestId, handler, options = {}) => {
|
|
1088
|
+
const {
|
|
1089
|
+
hydrate = true,
|
|
1090
|
+
skip = false
|
|
1091
|
+
} = options; // Plug in to the request interception framework for code that wants
|
|
1092
|
+
// to use that.
|
|
1093
|
+
|
|
1094
|
+
const interceptedHandler = Object(_use_request_interception_js__WEBPACK_IMPORTED_MODULE_5__[/* useRequestInterception */ "a"])(requestId, handler); // If we're server-side or hydrating, we'll have a cached entry to use.
|
|
1095
|
+
// So we get that and use it to initialize our state.
|
|
1096
|
+
// This works in both hydration and SSR because the very first call to
|
|
1097
|
+
// this will have cached data in those cases as it will be present on the
|
|
1098
|
+
// initial render - and subsequent renders on the client it will be null.
|
|
1099
|
+
|
|
1100
|
+
const cachedResult = _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_3__[/* SsrCache */ "a"].Default.getEntry(requestId); // We only track data requests when we are server-side, we are not skipping
|
|
1101
|
+
// the request, and we don't already have a result, as given by the
|
|
1102
|
+
// cachedData (which is also the initial value for the result state).
|
|
1103
|
+
|
|
1104
|
+
const maybeTrack = Object(react__WEBPACK_IMPORTED_MODULE_1__["useContext"])(_util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__[/* TrackerContext */ "b"]);
|
|
1105
|
+
|
|
1106
|
+
if (!skip && cachedResult == null && _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
1107
|
+
maybeTrack == null ? void 0 : maybeTrack(requestId, interceptedHandler, hydrate);
|
|
1108
|
+
} // A null result means there was no result to hydrate.
|
|
1109
|
+
|
|
1110
|
+
|
|
1111
|
+
return cachedResult == null ? null : Object(_util_result_from_cache_response_js__WEBPACK_IMPORTED_MODULE_4__[/* resultFromCachedResponse */ "a"])(cachedResult);
|
|
1112
|
+
};
|
|
1113
|
+
|
|
1114
|
+
/***/ }),
|
|
1115
|
+
/* 15 */
|
|
1116
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1117
|
+
|
|
1118
|
+
"use strict";
|
|
1119
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useCachedEffect; });
|
|
1120
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1121
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1122
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
|
|
1123
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
1124
|
+
/* harmony import */ var _util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(10);
|
|
1125
|
+
/* harmony import */ var _util_status_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(4);
|
|
1126
|
+
/* harmony import */ var _use_shared_cache_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(8);
|
|
1127
|
+
/* harmony import */ var _use_request_interception_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(16);
|
|
1128
|
+
|
|
1129
|
+
|
|
1130
|
+
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
|
|
1134
|
+
const DefaultScope = "useCachedEffect";
|
|
1135
|
+
/**
|
|
1136
|
+
* Hook to execute and cache an async operation on the client.
|
|
1137
|
+
*
|
|
1138
|
+
* This hook executes the given handler on the client if there is no
|
|
1139
|
+
* cached result to use.
|
|
1140
|
+
*
|
|
1141
|
+
* Results are cached so they can be shared between equivalent invocations.
|
|
1142
|
+
* In-flight requests are also shared, so that concurrent calls will
|
|
1143
|
+
* behave as one might exect. Cache updates invoked by one hook instance
|
|
1144
|
+
* do not trigger renders in components that use the same requestID; however,
|
|
1145
|
+
* that should not matter since concurrent requests will share the same
|
|
1146
|
+
* in-flight request, and subsequent renders will grab from the cache.
|
|
1147
|
+
*
|
|
1148
|
+
* Once the request has been tried once and a non-loading response has been
|
|
1149
|
+
* cached, the request will not executed made again.
|
|
1150
|
+
*/
|
|
1151
|
+
|
|
1152
|
+
const useCachedEffect = (requestId, handler, options = {}) => {
|
|
1153
|
+
const {
|
|
1154
|
+
skip: hardSkip = false,
|
|
1155
|
+
retainResultOnChange = false,
|
|
1156
|
+
onResultChanged,
|
|
1157
|
+
scope = DefaultScope
|
|
1158
|
+
} = options; // Plug in to the request interception framework for code that wants
|
|
1159
|
+
// to use that.
|
|
1160
|
+
|
|
1161
|
+
const interceptedHandler = Object(_use_request_interception_js__WEBPACK_IMPORTED_MODULE_5__[/* useRequestInterception */ "a"])(requestId, handler); // Instead of using state, which would be local to just this hook instance,
|
|
1162
|
+
// we use a shared in-memory cache.
|
|
1163
|
+
|
|
1164
|
+
const [mostRecentResult, setMostRecentResult] = Object(_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_4__[/* useSharedCache */ "b"])(requestId, // The key of the cached item
|
|
1165
|
+
scope // The scope of the cached items
|
|
1166
|
+
// No default value. We don't want the loading status there; to ensure
|
|
1167
|
+
// that all calls when the request is in-flight will update once that
|
|
1168
|
+
// request is done, we want the cache to be empty until that point.
|
|
1169
|
+
); // Build a function that will update the cache and either invoke the
|
|
1170
|
+
// callback provided in options, or force an update.
|
|
1171
|
+
|
|
1172
|
+
const forceUpdate = Object(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__["useForceUpdate"])();
|
|
1173
|
+
const setCacheAndNotify = react__WEBPACK_IMPORTED_MODULE_0__["useCallback"](value => {
|
|
1174
|
+
setMostRecentResult(value); // If our caller provided a cacheUpdated callback, we use that.
|
|
1175
|
+
// Otherwise, we toggle our little state update.
|
|
1176
|
+
|
|
1177
|
+
if (onResultChanged != null) {
|
|
1178
|
+
onResultChanged(value);
|
|
1179
|
+
} else {
|
|
1180
|
+
forceUpdate();
|
|
1181
|
+
}
|
|
1182
|
+
}, [setMostRecentResult, onResultChanged, forceUpdate]); // We need to trigger a re-render when the request ID changes as that
|
|
1183
|
+
// indicates its a different request. We don't default the current id as
|
|
1184
|
+
// this is a proxy for the first render, where we will make the request
|
|
1185
|
+
// if we don't already have a cached value.
|
|
1186
|
+
|
|
1187
|
+
const requestIdRef = react__WEBPACK_IMPORTED_MODULE_0__["useRef"]();
|
|
1188
|
+
const previousRequestId = requestIdRef.current; // Calculate our soft skip state.
|
|
1189
|
+
// Soft skip changes are things that should skip the effect if something
|
|
1190
|
+
// else triggers the effect to run, but should not itself trigger the effect
|
|
1191
|
+
// (which would cancel a previous invocation).
|
|
1192
|
+
|
|
1193
|
+
const softSkip = react__WEBPACK_IMPORTED_MODULE_0__["useMemo"](() => {
|
|
1194
|
+
if (requestId === previousRequestId) {
|
|
1195
|
+
// If the requestId is unchanged, it means we already rendered at
|
|
1196
|
+
// least once and so we already made the request at least once. So
|
|
1197
|
+
// we can bail out right here.
|
|
1198
|
+
return true;
|
|
1199
|
+
} // If we already have a cached value, we're going to skip.
|
|
1200
|
+
|
|
1201
|
+
|
|
1202
|
+
if (mostRecentResult != null) {
|
|
1203
|
+
return true;
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
return false;
|
|
1207
|
+
}, [requestId, previousRequestId, mostRecentResult]); // So now we make sure the client-side request happens per our various
|
|
1208
|
+
// options.
|
|
1209
|
+
|
|
1210
|
+
react__WEBPACK_IMPORTED_MODULE_0__["useEffect"](() => {
|
|
1211
|
+
let cancel = false; // We don't do anything if we've been told to hard skip (a hard skip
|
|
1212
|
+
// means we should cancel the previous request and is therefore a
|
|
1213
|
+
// dependency on that), or we have determined we have already done
|
|
1214
|
+
// enough and can soft skip (a soft skip doesn't trigger the request
|
|
1215
|
+
// to re-run; we don't want to cancel the in progress effect if we're
|
|
1216
|
+
// soft skipping.
|
|
1217
|
+
|
|
1218
|
+
if (hardSkip || softSkip) {
|
|
1219
|
+
return;
|
|
1220
|
+
} // If we got here, we're going to perform the request.
|
|
1221
|
+
// Let's make sure our ref is set to the most recent requestId.
|
|
1222
|
+
|
|
1223
|
+
|
|
1224
|
+
requestIdRef.current = requestId; // OK, we've done all our checks and things. It's time to make the
|
|
1225
|
+
// request. We use our request fulfillment here so that in-flight
|
|
1226
|
+
// requests are shared.
|
|
1227
|
+
// NOTE: Our request fulfillment handles the error cases here.
|
|
1228
|
+
// Catching shouldn't serve a purpose.
|
|
1229
|
+
// eslint-disable-next-line promise/catch-or-return
|
|
1230
|
+
|
|
1231
|
+
_util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestFulfillment */ "a"].Default.fulfill(requestId, {
|
|
1232
|
+
handler: interceptedHandler
|
|
1233
|
+
}).then(result => {
|
|
1234
|
+
if (cancel) {
|
|
1235
|
+
// We don't modify our result if an earlier effect was
|
|
1236
|
+
// cancelled as it means that this hook no longer cares about
|
|
1237
|
+
// that old request.
|
|
1238
|
+
return;
|
|
1239
|
+
}
|
|
1240
|
+
|
|
1241
|
+
setCacheAndNotify(result);
|
|
1242
|
+
return; // Shut up eslint always-return rule.
|
|
1243
|
+
});
|
|
1244
|
+
return () => {
|
|
1245
|
+
// TODO(somewhatabstract, FEI-4276): Eventually, we will want to be
|
|
1246
|
+
// able abort in-flight requests, but for now, we don't have that.
|
|
1247
|
+
// (Of course, we will only want to abort them if no one is waiting
|
|
1248
|
+
// on them)
|
|
1249
|
+
// For now, we just block cancelled requests from changing our
|
|
1250
|
+
// cache.
|
|
1251
|
+
cancel = true;
|
|
1252
|
+
}; // We only want to run this effect if the requestId, or skip values
|
|
1253
|
+
// change. These are the only two things that should affect the
|
|
1254
|
+
// cancellation of a pending request. We do not update if the handler
|
|
1255
|
+
// changes, in order to simplify the API - otherwise, callers would
|
|
1256
|
+
// not be able to use inline functions with this hook.
|
|
1257
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1258
|
+
}, [hardSkip, requestId]); // We track the last result we returned in order to support the
|
|
1259
|
+
// "retainResultOnChange" option.
|
|
1260
|
+
|
|
1261
|
+
const lastResultAgnosticOfIdRef = react__WEBPACK_IMPORTED_MODULE_0__["useRef"](_util_status_js__WEBPACK_IMPORTED_MODULE_3__[/* Status */ "a"].loading());
|
|
1262
|
+
const loadingResult = retainResultOnChange ? lastResultAgnosticOfIdRef.current : _util_status_js__WEBPACK_IMPORTED_MODULE_3__[/* Status */ "a"].loading(); // Loading is a transient state, so we only use it here; it's not something
|
|
1263
|
+
// we cache.
|
|
1264
|
+
|
|
1265
|
+
const result = react__WEBPACK_IMPORTED_MODULE_0__["useMemo"](() => mostRecentResult != null ? mostRecentResult : loadingResult, [mostRecentResult, loadingResult]);
|
|
1266
|
+
lastResultAgnosticOfIdRef.current = result;
|
|
1267
|
+
return result;
|
|
1268
|
+
};
|
|
1269
|
+
|
|
1270
|
+
/***/ }),
|
|
1271
|
+
/* 16 */
|
|
1272
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1273
|
+
|
|
1274
|
+
"use strict";
|
|
1275
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useRequestInterception; });
|
|
1276
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1277
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1278
|
+
/* harmony import */ var _components_intercept_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(13);
|
|
1279
|
+
|
|
1280
|
+
|
|
1281
|
+
|
|
1282
|
+
/**
|
|
1283
|
+
* Allow request handling to be intercepted.
|
|
1284
|
+
*
|
|
1285
|
+
* Hook to take a uniquely identified request handler and return a
|
|
1286
|
+
* method that will support request interception from the InterceptRequest
|
|
1287
|
+
* component.
|
|
1288
|
+
*
|
|
1289
|
+
* If you want request interception to be supported with `useServerEffect` or
|
|
1290
|
+
* any client-side effect that uses the handler, call this first to generate
|
|
1291
|
+
* an intercepted handler, and then invoke `useServerEffect` (or other things)
|
|
1292
|
+
* with that intercepted handler.
|
|
1293
|
+
*/
|
|
1294
|
+
const useRequestInterception = (requestId, handler) => {
|
|
1295
|
+
// Get the interceptors that have been registered.
|
|
1296
|
+
const interceptors = react__WEBPACK_IMPORTED_MODULE_0__["useContext"](_components_intercept_context_js__WEBPACK_IMPORTED_MODULE_1__[/* default */ "a"]); // Now, we need to create a new handler that will check if the
|
|
1297
|
+
// request is intercepted before ultimately calling the original handler
|
|
1298
|
+
// if nothing intercepted it.
|
|
1299
|
+
// We memoize this so that it only changes if something related to it
|
|
1300
|
+
// changes.
|
|
1301
|
+
|
|
1302
|
+
const interceptedHandler = react__WEBPACK_IMPORTED_MODULE_0__["useCallback"](() => {
|
|
1303
|
+
// Call the interceptors from closest to furthest.
|
|
1304
|
+
// If one returns a non-null result, then we keep that.
|
|
1305
|
+
const interceptResponse = interceptors.reduceRight((prev, interceptor) => {
|
|
1306
|
+
if (prev != null) {
|
|
1307
|
+
return prev;
|
|
1308
|
+
}
|
|
1309
|
+
|
|
1310
|
+
return interceptor(requestId);
|
|
1311
|
+
}, null); // If nothing intercepted this request, invoke the original handler.
|
|
1312
|
+
// NOTE: We can't guarantee all interceptors return the same type
|
|
1313
|
+
// as our handler, so how can flow know? Let's just suppress that.
|
|
1314
|
+
// $FlowFixMe[incompatible-return]
|
|
1315
|
+
|
|
1316
|
+
return interceptResponse != null ? interceptResponse : handler();
|
|
1317
|
+
}, [handler, interceptors, requestId]);
|
|
1318
|
+
return interceptedHandler;
|
|
1319
|
+
};
|
|
1320
|
+
|
|
1321
|
+
/***/ }),
|
|
1322
|
+
/* 17 */
|
|
803
1323
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
804
1324
|
|
|
805
1325
|
"use strict";
|
|
@@ -810,107 +1330,50 @@ class RequestFulfillment {
|
|
|
810
1330
|
const GqlRouterContext = /*#__PURE__*/react__WEBPACK_IMPORTED_MODULE_0__["createContext"](null);
|
|
811
1331
|
|
|
812
1332
|
/***/ }),
|
|
813
|
-
/*
|
|
1333
|
+
/* 18 */
|
|
814
1334
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
815
1335
|
|
|
816
1336
|
"use strict";
|
|
817
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return
|
|
818
|
-
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "b", function() { return useSharedCache; });
|
|
819
|
-
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
820
|
-
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
821
|
-
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
|
|
822
|
-
/* harmony import */ var _khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_stuff_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
823
|
-
/* harmony import */ var _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5);
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
/**
|
|
829
|
-
* This is the cache.
|
|
830
|
-
* It's incredibly complex.
|
|
831
|
-
* Very in-memory. So cache. Such complex. Wow.
|
|
832
|
-
*/
|
|
833
|
-
const cache = new _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_2__[/* ScopedInMemoryCache */ "a"]();
|
|
834
|
-
/**
|
|
835
|
-
* Clear the in-memory cache or a single scope within it.
|
|
836
|
-
*/
|
|
837
|
-
|
|
838
|
-
const clearSharedCache = (scope = "") => {
|
|
839
|
-
// If we have a valid scope (empty string is falsy), then clear that scope.
|
|
840
|
-
if (scope && typeof scope === "string") {
|
|
841
|
-
cache.purgeScope(scope);
|
|
842
|
-
} else {
|
|
843
|
-
// Just reset the object. This should be sufficient.
|
|
844
|
-
cache.purgeAll();
|
|
845
|
-
}
|
|
846
|
-
};
|
|
1337
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return mergeGqlContext; });
|
|
847
1338
|
/**
|
|
848
|
-
*
|
|
849
|
-
*
|
|
850
|
-
* @returns {[?ReadOnlyCacheValue, CacheValueFn]}
|
|
851
|
-
* Returns an array containing the current cache entry (or undefined), a
|
|
852
|
-
* function to set the cache entry (passing null or undefined to this function
|
|
853
|
-
* will delete the entry).
|
|
1339
|
+
* Construct a complete GqlContext from current defaults and a partial context.
|
|
854
1340
|
*
|
|
855
|
-
*
|
|
856
|
-
* the `
|
|
857
|
-
*
|
|
858
|
-
* NOTE: Unlike useState or useReducer, we don't automatically update folks
|
|
859
|
-
* if the value they reference changes. We might add it later (if we need to),
|
|
860
|
-
* but the likelihood here is that things won't be changing in this cache in a
|
|
861
|
-
* way where we would need that. If we do (and likely only in specific
|
|
862
|
-
* circumstances), we should consider adding a simple boolean useState that can
|
|
863
|
-
* be toggled to cause a rerender whenever the referenced cached data changes
|
|
864
|
-
* so that callers can re-render on cache changes. However, we should make
|
|
865
|
-
* sure this toggling is optional - or we could use a callback argument, to
|
|
866
|
-
* achieve this on an as-needed basis.
|
|
1341
|
+
* Values in the partial context that are `undefined` will be ignored.
|
|
1342
|
+
* Values in the partial context that are `null` will be deleted.
|
|
867
1343
|
*/
|
|
1344
|
+
const mergeGqlContext = (defaultContext, overrides) => {
|
|
1345
|
+
// Let's merge the partial context default context. We deliberately
|
|
1346
|
+
// don't spread because spreading would overwrite default context
|
|
1347
|
+
// values with undefined or null if the partial context includes a value
|
|
1348
|
+
// explicitly set to undefined or null.
|
|
1349
|
+
return Object.keys(overrides).reduce((acc, key) => {
|
|
1350
|
+
// Undefined values are ignored.
|
|
1351
|
+
if (overrides[key] !== undefined) {
|
|
1352
|
+
if (overrides[key] === null) {
|
|
1353
|
+
// Null indicates we delete this context value.
|
|
1354
|
+
delete acc[key];
|
|
1355
|
+
} else {
|
|
1356
|
+
// Otherwise, we set it.
|
|
1357
|
+
acc[key] = overrides[key];
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
868
1360
|
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
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);
|
|
873
|
-
}
|
|
874
|
-
|
|
875
|
-
if (!scope || typeof scope !== "string") {
|
|
876
|
-
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);
|
|
877
|
-
} // Memoize our APIs.
|
|
878
|
-
// This one allows callers to set or replace the cached value.
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
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
|
|
882
|
-
// since our last run through. Also, our cache does not know what type it
|
|
883
|
-
// stores, so we have to cast it to the type we're exporting. This is a
|
|
884
|
-
// dev time courtesy, rather than a runtime thing.
|
|
885
|
-
// $FlowIgnore[incompatible-type]
|
|
886
|
-
|
|
887
|
-
let currentValue = cache.get(scope, id); // If we have an initial value, we need to add it to the cache
|
|
888
|
-
// and use it as our current value.
|
|
889
|
-
|
|
890
|
-
if (currentValue == null && initialValue !== undefined) {
|
|
891
|
-
// Get the initial value.
|
|
892
|
-
const value = typeof initialValue === "function" ? initialValue() : initialValue; // Update the cache.
|
|
893
|
-
|
|
894
|
-
cacheValue(value); // Make sure we return this value as our current value.
|
|
895
|
-
|
|
896
|
-
currentValue = value;
|
|
897
|
-
} // Now we have everything, let's return it.
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
return [currentValue, cacheValue];
|
|
1361
|
+
return acc;
|
|
1362
|
+
}, { ...defaultContext
|
|
1363
|
+
});
|
|
901
1364
|
};
|
|
902
1365
|
|
|
903
1366
|
/***/ }),
|
|
904
|
-
/*
|
|
1367
|
+
/* 19 */
|
|
905
1368
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
906
1369
|
|
|
907
1370
|
"use strict";
|
|
908
1371
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return TrackData; });
|
|
909
1372
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
910
1373
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
911
|
-
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
1374
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(2);
|
|
912
1375
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
913
|
-
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
|
|
1376
|
+
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(7);
|
|
914
1377
|
|
|
915
1378
|
|
|
916
1379
|
|
|
@@ -932,22 +1395,13 @@ class TrackData extends react__WEBPACK_IMPORTED_MODULE_0__["Component"] {
|
|
|
932
1395
|
}
|
|
933
1396
|
|
|
934
1397
|
/***/ }),
|
|
935
|
-
/*
|
|
1398
|
+
/* 20 */
|
|
936
1399
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
937
1400
|
|
|
938
1401
|
"use strict";
|
|
939
1402
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
940
1403
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
941
|
-
/* harmony import */ var
|
|
942
|
-
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__);
|
|
943
|
-
/* harmony import */ var _util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(11);
|
|
944
|
-
/* harmony import */ var _hooks_use_server_effect_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(8);
|
|
945
|
-
/* harmony import */ var _hooks_use_request_interception_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(9);
|
|
946
|
-
/* harmony import */ var _util_result_from_cache_response_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(10);
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
1404
|
+
/* harmony import */ var _hooks_use_hydratable_effect_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(11);
|
|
951
1405
|
|
|
952
1406
|
|
|
953
1407
|
|
|
@@ -960,99 +1414,26 @@ const Data = ({
|
|
|
960
1414
|
requestId,
|
|
961
1415
|
handler,
|
|
962
1416
|
children,
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
alwaysRequestOnHydration
|
|
1417
|
+
retainResultOnChange = false,
|
|
1418
|
+
clientBehavior = _hooks_use_hydratable_effect_js__WEBPACK_IMPORTED_MODULE_1__[/* WhenClientSide */ "a"].ExecuteWhenNoSuccessResult
|
|
966
1419
|
}) => {
|
|
967
|
-
const
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
react__WEBPACK_IMPORTED_MODULE_0__["useEffect"](() => {
|
|
974
|
-
// This is here until I can do a better documentation example for
|
|
975
|
-
// the TrackData docs.
|
|
976
|
-
// istanbul ignore next
|
|
977
|
-
if (_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_1__["Server"].isServerSide()) {
|
|
978
|
-
return;
|
|
979
|
-
} // We don't bother with this if we have hydration data and we're not
|
|
980
|
-
// forcing a request on hydration.
|
|
981
|
-
// We don't care if these things change after the first render,
|
|
982
|
-
// so we don't want them in the inputs array.
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
if (!alwaysRequestOnHydration && (hydrateResult == null ? void 0 : hydrateResult.data) != null) {
|
|
986
|
-
return;
|
|
987
|
-
} // If we're not hydrating a result and we're not going to render
|
|
988
|
-
// with old data until we're loaded, we want to make sure we set our
|
|
989
|
-
// result to null so that we're in the loading state.
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
if (!showOldDataWhileLoading) {
|
|
993
|
-
// Mark ourselves as loading.
|
|
994
|
-
setResult(null);
|
|
995
|
-
} // We aren't server-side, so let's make the request.
|
|
996
|
-
// We don't need to use our built-in request fulfillment here if we
|
|
997
|
-
// don't want, but it does mean we'll share inflight requests for the
|
|
998
|
-
// same ID and the result will be in the same format as the
|
|
999
|
-
// hydrated value.
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
let cancel = false;
|
|
1003
|
-
_util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestFulfillment */ "a"].Default.fulfill(requestId, {
|
|
1004
|
-
handler: interceptedHandler
|
|
1005
|
-
}).then(result => {
|
|
1006
|
-
if (cancel) {
|
|
1007
|
-
return;
|
|
1008
|
-
}
|
|
1009
|
-
|
|
1010
|
-
setResult(result);
|
|
1011
|
-
return;
|
|
1012
|
-
}).catch(e => {
|
|
1013
|
-
if (cancel) {
|
|
1014
|
-
return;
|
|
1015
|
-
}
|
|
1016
|
-
/**
|
|
1017
|
-
* We should never get here as errors in fulfillment are part
|
|
1018
|
-
* of the `then`, but if we do.
|
|
1019
|
-
*/
|
|
1020
|
-
// eslint-disable-next-line no-console
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
console.error(`Unexpected error occurred during data fulfillment: ${e}`);
|
|
1024
|
-
setResult({
|
|
1025
|
-
error: typeof e === "string" ? e : e.message
|
|
1026
|
-
});
|
|
1027
|
-
return;
|
|
1028
|
-
});
|
|
1029
|
-
return () => {
|
|
1030
|
-
cancel = true;
|
|
1031
|
-
}; // If the handler changes, we don't care. The ID is what indicates
|
|
1032
|
-
// the request that should be made and folks shouldn't be changing the
|
|
1033
|
-
// handler without changing the ID as well.
|
|
1034
|
-
// In addition, we don't want to include hydrateResult nor
|
|
1035
|
-
// alwaysRequestOnHydration as them changinng after the first pass
|
|
1036
|
-
// is irrelevant.
|
|
1037
|
-
// Finally, we don't want to include showOldDataWhileLoading as that
|
|
1038
|
-
// changing on its own is also not relevant. It only matters if the
|
|
1039
|
-
// request itself changes. All of which is to say that we only
|
|
1040
|
-
// run this effect for the ID changing.
|
|
1041
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
1042
|
-
}, [requestId]);
|
|
1043
|
-
return children(Object(_util_result_from_cache_response_js__WEBPACK_IMPORTED_MODULE_5__[/* resultFromCachedResponse */ "a"])(currentResult));
|
|
1420
|
+
const result = Object(_hooks_use_hydratable_effect_js__WEBPACK_IMPORTED_MODULE_1__[/* useHydratableEffect */ "b"])(requestId, handler, {
|
|
1421
|
+
retainResultOnChange,
|
|
1422
|
+
clientBehavior
|
|
1423
|
+
});
|
|
1424
|
+
return children(result);
|
|
1044
1425
|
};
|
|
1045
1426
|
|
|
1046
1427
|
/* harmony default export */ __webpack_exports__["a"] = (Data);
|
|
1047
1428
|
|
|
1048
1429
|
/***/ }),
|
|
1049
|
-
/*
|
|
1430
|
+
/* 21 */
|
|
1050
1431
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1051
1432
|
|
|
1052
1433
|
"use strict";
|
|
1053
1434
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1054
1435
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1055
|
-
/* harmony import */ var _intercept_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
1436
|
+
/* harmony import */ var _intercept_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(13);
|
|
1056
1437
|
|
|
1057
1438
|
|
|
1058
1439
|
|
|
@@ -1087,14 +1468,14 @@ const InterceptRequests = ({
|
|
|
1087
1468
|
/* harmony default export */ __webpack_exports__["a"] = (InterceptRequests);
|
|
1088
1469
|
|
|
1089
1470
|
/***/ }),
|
|
1090
|
-
/*
|
|
1471
|
+
/* 22 */
|
|
1091
1472
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1092
1473
|
|
|
1093
1474
|
"use strict";
|
|
1094
1475
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return GqlRouter; });
|
|
1095
1476
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1096
1477
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1097
|
-
/* harmony import */ var _util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
1478
|
+
/* harmony import */ var _util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(17);
|
|
1098
1479
|
|
|
1099
1480
|
|
|
1100
1481
|
|
|
@@ -1131,16 +1512,16 @@ const GqlRouter = ({
|
|
|
1131
1512
|
};
|
|
1132
1513
|
|
|
1133
1514
|
/***/ }),
|
|
1134
|
-
/*
|
|
1515
|
+
/* 23 */
|
|
1135
1516
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1136
1517
|
|
|
1137
1518
|
"use strict";
|
|
1138
1519
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useGql; });
|
|
1139
1520
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1140
1521
|
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1141
|
-
/* harmony import */ var
|
|
1142
|
-
/* harmony import */ var
|
|
1143
|
-
/* harmony import */ var
|
|
1522
|
+
/* harmony import */ var _util_merge_gql_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(18);
|
|
1523
|
+
/* harmony import */ var _use_gql_router_context_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(25);
|
|
1524
|
+
/* harmony import */ var _util_get_gql_data_from_response_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(26);
|
|
1144
1525
|
|
|
1145
1526
|
|
|
1146
1527
|
|
|
@@ -1156,9 +1537,92 @@ const GqlRouter = ({
|
|
|
1156
1537
|
* Values in the partial context given to the returned fetch function will
|
|
1157
1538
|
* only be included if they have a value other than undefined.
|
|
1158
1539
|
*/
|
|
1159
|
-
const useGql = () => {
|
|
1540
|
+
const useGql = (context = {}) => {
|
|
1541
|
+
// This hook only works if the `GqlRouter` has been used to setup context.
|
|
1542
|
+
const gqlRouterContext = Object(_use_gql_router_context_js__WEBPACK_IMPORTED_MODULE_2__[/* useGqlRouterContext */ "a"])(context); // Let's memoize the gqlFetch function we create based off our context.
|
|
1543
|
+
// That way, even if the context happens to change, if its values don't
|
|
1544
|
+
// we give the same function instance back to our callers instead of
|
|
1545
|
+
// making a new one. That then means they can safely use the return value
|
|
1546
|
+
// in hooks deps without fear of it triggering extra renders.
|
|
1547
|
+
|
|
1548
|
+
const gqlFetch = Object(react__WEBPACK_IMPORTED_MODULE_0__["useCallback"])((operation, options = Object.freeze({})) => {
|
|
1549
|
+
const {
|
|
1550
|
+
fetch,
|
|
1551
|
+
defaultContext
|
|
1552
|
+
} = gqlRouterContext;
|
|
1553
|
+
const {
|
|
1554
|
+
variables,
|
|
1555
|
+
context = {}
|
|
1556
|
+
} = options;
|
|
1557
|
+
const finalContext = Object(_util_merge_gql_context_js__WEBPACK_IMPORTED_MODULE_1__[/* mergeGqlContext */ "a"])(defaultContext, context); // Invoke the fetch and extract the data.
|
|
1558
|
+
|
|
1559
|
+
return fetch(operation, variables, finalContext).then(_util_get_gql_data_from_response_js__WEBPACK_IMPORTED_MODULE_3__[/* getGqlDataFromResponse */ "a"]);
|
|
1560
|
+
}, [gqlRouterContext]);
|
|
1561
|
+
return gqlFetch;
|
|
1562
|
+
};
|
|
1563
|
+
|
|
1564
|
+
/***/ }),
|
|
1565
|
+
/* 24 */
|
|
1566
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1567
|
+
|
|
1568
|
+
"use strict";
|
|
1569
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return resultFromCachedResponse; });
|
|
1570
|
+
/* harmony import */ var _status_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(4);
|
|
1571
|
+
/* harmony import */ var _data_error_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
|
|
1572
|
+
|
|
1573
|
+
|
|
1574
|
+
|
|
1575
|
+
/**
|
|
1576
|
+
* Turns a cache entry into a stateful result.
|
|
1577
|
+
*/
|
|
1578
|
+
const resultFromCachedResponse = cacheEntry => {
|
|
1579
|
+
// No cache entry means no result to be hydrated.
|
|
1580
|
+
if (cacheEntry == null) {
|
|
1581
|
+
return null;
|
|
1582
|
+
}
|
|
1583
|
+
|
|
1584
|
+
const {
|
|
1585
|
+
data,
|
|
1586
|
+
error
|
|
1587
|
+
} = cacheEntry;
|
|
1588
|
+
|
|
1589
|
+
if (error != null) {
|
|
1590
|
+
// Let's hydrate the error. We don't persist everything about the
|
|
1591
|
+
// original error on the server, hence why we only superficially
|
|
1592
|
+
// hydrate it to a GqlHydratedError.
|
|
1593
|
+
return _status_js__WEBPACK_IMPORTED_MODULE_0__[/* Status */ "a"].error(new _data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataError */ "a"](error, _data_error_js__WEBPACK_IMPORTED_MODULE_1__[/* DataErrors */ "b"].Hydrated));
|
|
1594
|
+
}
|
|
1595
|
+
|
|
1596
|
+
if (data != null) {
|
|
1597
|
+
return _status_js__WEBPACK_IMPORTED_MODULE_0__[/* Status */ "a"].success(data);
|
|
1598
|
+
} // We shouldn't get here since we don't actually cache null data.
|
|
1599
|
+
|
|
1600
|
+
|
|
1601
|
+
return _status_js__WEBPACK_IMPORTED_MODULE_0__[/* Status */ "a"].aborted();
|
|
1602
|
+
};
|
|
1603
|
+
|
|
1604
|
+
/***/ }),
|
|
1605
|
+
/* 25 */
|
|
1606
|
+
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1607
|
+
|
|
1608
|
+
"use strict";
|
|
1609
|
+
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return useGqlRouterContext; });
|
|
1610
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0);
|
|
1611
|
+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
|
|
1612
|
+
/* harmony import */ var _util_merge_gql_context_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(18);
|
|
1613
|
+
/* harmony import */ var _util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(17);
|
|
1614
|
+
/* harmony import */ var _util_gql_error_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(3);
|
|
1615
|
+
|
|
1616
|
+
|
|
1617
|
+
|
|
1618
|
+
|
|
1619
|
+
|
|
1620
|
+
/**
|
|
1621
|
+
* Construct a GqlRouterContext from the current one and partial context.
|
|
1622
|
+
*/
|
|
1623
|
+
const useGqlRouterContext = (contextOverrides = {}) => {
|
|
1160
1624
|
// This hook only works if the `GqlRouter` has been used to setup context.
|
|
1161
|
-
const gqlRouterContext = Object(react__WEBPACK_IMPORTED_MODULE_0__["useContext"])(
|
|
1625
|
+
const gqlRouterContext = Object(react__WEBPACK_IMPORTED_MODULE_0__["useContext"])(_util_gql_router_context_js__WEBPACK_IMPORTED_MODULE_2__[/* GqlRouterContext */ "a"]);
|
|
1162
1626
|
|
|
1163
1627
|
if (gqlRouterContext == null) {
|
|
1164
1628
|
throw new _util_gql_error_js__WEBPACK_IMPORTED_MODULE_3__[/* GqlError */ "a"]("No GqlRouter", _util_gql_error_js__WEBPACK_IMPORTED_MODULE_3__[/* GqlErrors */ "b"].Internal);
|
|
@@ -1167,55 +1631,37 @@ const useGql = () => {
|
|
|
1167
1631
|
const {
|
|
1168
1632
|
fetch,
|
|
1169
1633
|
defaultContext
|
|
1170
|
-
} = gqlRouterContext;
|
|
1171
|
-
|
|
1172
|
-
// we
|
|
1173
|
-
//
|
|
1174
|
-
// in hooks deps without fear of it triggering extra renders.
|
|
1634
|
+
} = gqlRouterContext;
|
|
1635
|
+
const contextRef = Object(react__WEBPACK_IMPORTED_MODULE_0__["useRef"])(defaultContext);
|
|
1636
|
+
const mergedContext = Object(_util_merge_gql_context_js__WEBPACK_IMPORTED_MODULE_1__[/* mergeGqlContext */ "a"])(defaultContext, contextOverrides); // Now, we can see if this represents a new context and if so,
|
|
1637
|
+
// update our ref and return the merged value.
|
|
1175
1638
|
|
|
1176
|
-
const
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
context = {}
|
|
1180
|
-
} = options; // Let's merge the partial context of the fetch with the
|
|
1181
|
-
// default context. We deliberately don't spread because
|
|
1182
|
-
// spreading would overwrite default context values with
|
|
1183
|
-
// undefined if the partial context includes a value explicitly
|
|
1184
|
-
// set to undefined. Instead, we use a map/reduce of keys.
|
|
1185
|
-
|
|
1186
|
-
const mergedContext = Object.keys(context).reduce((acc, key) => {
|
|
1187
|
-
if (context[key] !== undefined) {
|
|
1188
|
-
acc[key] = context[key];
|
|
1189
|
-
}
|
|
1639
|
+
const refKeys = Object.keys(contextRef.current);
|
|
1640
|
+
const mergedKeys = Object.keys(mergedContext);
|
|
1641
|
+
const shouldWeUpdateRef = refKeys.length !== mergedKeys.length || mergedKeys.every(key => contextRef.current[key] !== mergedContext[key]);
|
|
1190
1642
|
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
return fetch(operation, variables, mergedContext).then(_util_get_gql_data_from_response_js__WEBPACK_IMPORTED_MODULE_2__[/* getGqlDataFromResponse */ "a"], error => {
|
|
1196
|
-
// Return null if the request was aborted.
|
|
1197
|
-
// The only way to detect this reliably, it seems, is to
|
|
1198
|
-
// check the error name and see if it's "AbortError" (this
|
|
1199
|
-
// is also what Apollo does).
|
|
1200
|
-
// Even then, it's reliant on the fetch supporting aborts.
|
|
1201
|
-
if (error.name === "AbortError") {
|
|
1202
|
-
return null;
|
|
1203
|
-
} // Need to make sure we pass other errors along.
|
|
1643
|
+
if (shouldWeUpdateRef) {
|
|
1644
|
+
contextRef.current = mergedContext;
|
|
1645
|
+
} // OK, now we're up-to-date, let's memoize our final result.
|
|
1204
1646
|
|
|
1205
1647
|
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1648
|
+
const finalContext = contextRef.current;
|
|
1649
|
+
const finalRouterContext = Object(react__WEBPACK_IMPORTED_MODULE_0__["useMemo"])(() => ({
|
|
1650
|
+
fetch,
|
|
1651
|
+
defaultContext: finalContext
|
|
1652
|
+
}), [fetch, finalContext]);
|
|
1653
|
+
return finalRouterContext;
|
|
1210
1654
|
};
|
|
1211
1655
|
|
|
1212
1656
|
/***/ }),
|
|
1213
|
-
/*
|
|
1657
|
+
/* 26 */
|
|
1214
1658
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1215
1659
|
|
|
1216
1660
|
"use strict";
|
|
1217
1661
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return getGqlDataFromResponse; });
|
|
1218
|
-
/* harmony import */ var
|
|
1662
|
+
/* harmony import */ var _data_error_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(1);
|
|
1663
|
+
/* harmony import */ var _gql_error_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(3);
|
|
1664
|
+
|
|
1219
1665
|
|
|
1220
1666
|
/**
|
|
1221
1667
|
* Validate a GQL operation response and extract the data.
|
|
@@ -1230,7 +1676,7 @@ const getGqlDataFromResponse = async response => {
|
|
|
1230
1676
|
try {
|
|
1231
1677
|
result = JSON.parse(bodyText);
|
|
1232
1678
|
} catch (e) {
|
|
1233
|
-
throw new
|
|
1679
|
+
throw new _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataError */ "a"]("Failed to parse response", _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataErrors */ "b"].Parse, {
|
|
1234
1680
|
metadata: {
|
|
1235
1681
|
statusCode: response.status,
|
|
1236
1682
|
bodyText
|
|
@@ -1241,7 +1687,7 @@ const getGqlDataFromResponse = async response => {
|
|
|
1241
1687
|
|
|
1242
1688
|
|
|
1243
1689
|
if (response.status >= 300) {
|
|
1244
|
-
throw new
|
|
1690
|
+
throw new _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataError */ "a"]("Response unsuccessful", _data_error_js__WEBPACK_IMPORTED_MODULE_0__[/* DataErrors */ "b"].Network, {
|
|
1245
1691
|
metadata: {
|
|
1246
1692
|
statusCode: response.status,
|
|
1247
1693
|
result
|
|
@@ -1255,7 +1701,7 @@ const getGqlDataFromResponse = async response => {
|
|
|
1255
1701
|
!Object.prototype.hasOwnProperty.call(result, "data") && // Flow shouldn't be warning about this.
|
|
1256
1702
|
// $FlowIgnore[method-unbinding]
|
|
1257
1703
|
!Object.prototype.hasOwnProperty.call(result, "errors")) {
|
|
1258
|
-
throw new
|
|
1704
|
+
throw new _gql_error_js__WEBPACK_IMPORTED_MODULE_1__[/* GqlError */ "a"]("Server response missing", _gql_error_js__WEBPACK_IMPORTED_MODULE_1__[/* GqlErrors */ "b"].BadResponse, {
|
|
1259
1705
|
metadata: {
|
|
1260
1706
|
statusCode: response.status,
|
|
1261
1707
|
result
|
|
@@ -1265,7 +1711,7 @@ const getGqlDataFromResponse = async response => {
|
|
|
1265
1711
|
|
|
1266
1712
|
|
|
1267
1713
|
if (result.errors != null && Array.isArray(result.errors) && result.errors.length > 0) {
|
|
1268
|
-
throw new
|
|
1714
|
+
throw new _gql_error_js__WEBPACK_IMPORTED_MODULE_1__[/* GqlError */ "a"]("GraphQL errors", _gql_error_js__WEBPACK_IMPORTED_MODULE_1__[/* GqlErrors */ "b"].ErrorResult, {
|
|
1269
1715
|
metadata: {
|
|
1270
1716
|
statusCode: response.status,
|
|
1271
1717
|
result
|
|
@@ -1278,7 +1724,7 @@ const getGqlDataFromResponse = async response => {
|
|
|
1278
1724
|
};
|
|
1279
1725
|
|
|
1280
1726
|
/***/ }),
|
|
1281
|
-
/*
|
|
1727
|
+
/* 27 */
|
|
1282
1728
|
/***/ (function(module, __webpack_exports__, __webpack_require__) {
|
|
1283
1729
|
|
|
1284
1730
|
"use strict";
|
|
@@ -1288,51 +1734,85 @@ __webpack_require__.r(__webpack_exports__);
|
|
|
1288
1734
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "hasUnfulfilledRequests", function() { return hasUnfulfilledRequests; });
|
|
1289
1735
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removeFromCache", function() { return removeFromCache; });
|
|
1290
1736
|
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "removeAllFromCache", function() { return removeAllFromCache; });
|
|
1291
|
-
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(
|
|
1737
|
+
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2);
|
|
1292
1738
|
/* harmony import */ var _khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__);
|
|
1293
|
-
/* harmony import */ var _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(
|
|
1294
|
-
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(
|
|
1295
|
-
/* harmony import */ var _components_track_data_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(
|
|
1739
|
+
/* harmony import */ var _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5);
|
|
1740
|
+
/* harmony import */ var _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(7);
|
|
1741
|
+
/* harmony import */ var _components_track_data_js__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(19);
|
|
1296
1742
|
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "TrackData", function() { return _components_track_data_js__WEBPACK_IMPORTED_MODULE_3__["a"]; });
|
|
1297
1743
|
|
|
1298
|
-
/* harmony import */ var _components_data_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(
|
|
1744
|
+
/* harmony import */ var _components_data_js__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(20);
|
|
1299
1745
|
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "Data", function() { return _components_data_js__WEBPACK_IMPORTED_MODULE_4__["a"]; });
|
|
1300
1746
|
|
|
1301
|
-
/* harmony import */ var _components_intercept_requests_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(
|
|
1747
|
+
/* harmony import */ var _components_intercept_requests_js__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(21);
|
|
1302
1748
|
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "InterceptRequests", function() { return _components_intercept_requests_js__WEBPACK_IMPORTED_MODULE_5__["a"]; });
|
|
1303
1749
|
|
|
1304
|
-
/* harmony import */ var
|
|
1305
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1750
|
+
/* harmony import */ var _util_data_error_js__WEBPACK_IMPORTED_MODULE_6__ = __webpack_require__(1);
|
|
1751
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "DataError", function() { return _util_data_error_js__WEBPACK_IMPORTED_MODULE_6__["a"]; });
|
|
1752
|
+
|
|
1753
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "DataErrors", function() { return _util_data_error_js__WEBPACK_IMPORTED_MODULE_6__["b"]; });
|
|
1754
|
+
|
|
1755
|
+
/* harmony import */ var _hooks_use_server_effect_js__WEBPACK_IMPORTED_MODULE_7__ = __webpack_require__(14);
|
|
1756
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useServerEffect", function() { return _hooks_use_server_effect_js__WEBPACK_IMPORTED_MODULE_7__["a"]; });
|
|
1306
1757
|
|
|
1307
|
-
/* harmony import */ var
|
|
1308
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1758
|
+
/* harmony import */ var _hooks_use_cached_effect_js__WEBPACK_IMPORTED_MODULE_8__ = __webpack_require__(15);
|
|
1759
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useCachedEffect", function() { return _hooks_use_cached_effect_js__WEBPACK_IMPORTED_MODULE_8__["a"]; });
|
|
1309
1760
|
|
|
1310
|
-
/* harmony import */ var
|
|
1311
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useSharedCache", function() { return
|
|
1761
|
+
/* harmony import */ var _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_9__ = __webpack_require__(8);
|
|
1762
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useSharedCache", function() { return _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_9__["b"]; });
|
|
1312
1763
|
|
|
1313
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "clearSharedCache", function() { return
|
|
1764
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "clearSharedCache", function() { return _hooks_use_shared_cache_js__WEBPACK_IMPORTED_MODULE_9__["a"]; });
|
|
1314
1765
|
|
|
1315
|
-
/* harmony import */ var
|
|
1316
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1766
|
+
/* harmony import */ var _hooks_use_hydratable_effect_js__WEBPACK_IMPORTED_MODULE_10__ = __webpack_require__(11);
|
|
1767
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useHydratableEffect", function() { return _hooks_use_hydratable_effect_js__WEBPACK_IMPORTED_MODULE_10__["b"]; });
|
|
1317
1768
|
|
|
1318
|
-
/* harmony
|
|
1319
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "resultFromCachedResponse", function() { return _util_result_from_cache_response_js__WEBPACK_IMPORTED_MODULE_10__["a"]; });
|
|
1769
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "WhenClientSide", function() { return _hooks_use_hydratable_effect_js__WEBPACK_IMPORTED_MODULE_10__["a"]; });
|
|
1320
1770
|
|
|
1321
|
-
/* harmony import */ var
|
|
1322
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1771
|
+
/* harmony import */ var _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_11__ = __webpack_require__(9);
|
|
1772
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "ScopedInMemoryCache", function() { return _util_scoped_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_11__["a"]; });
|
|
1323
1773
|
|
|
1324
|
-
/* harmony import */ var
|
|
1325
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1774
|
+
/* harmony import */ var _util_serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_12__ = __webpack_require__(12);
|
|
1775
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "SerializableInMemoryCache", function() { return _util_serializable_in_memory_cache_js__WEBPACK_IMPORTED_MODULE_12__["a"]; });
|
|
1326
1776
|
|
|
1327
|
-
/* harmony import */ var
|
|
1328
|
-
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "
|
|
1777
|
+
/* harmony import */ var _util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_13__ = __webpack_require__(10);
|
|
1778
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "RequestFulfillment", function() { return _util_request_fulfillment_js__WEBPACK_IMPORTED_MODULE_13__["a"]; });
|
|
1329
1779
|
|
|
1330
|
-
/* harmony
|
|
1780
|
+
/* harmony import */ var _util_status_js__WEBPACK_IMPORTED_MODULE_14__ = __webpack_require__(4);
|
|
1781
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "Status", function() { return _util_status_js__WEBPACK_IMPORTED_MODULE_14__["a"]; });
|
|
1331
1782
|
|
|
1783
|
+
/* harmony import */ var _components_gql_router_js__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__(22);
|
|
1784
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GqlRouter", function() { return _components_gql_router_js__WEBPACK_IMPORTED_MODULE_15__["a"]; });
|
|
1332
1785
|
|
|
1786
|
+
/* harmony import */ var _hooks_use_gql_js__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__(23);
|
|
1787
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "useGql", function() { return _hooks_use_gql_js__WEBPACK_IMPORTED_MODULE_16__["a"]; });
|
|
1333
1788
|
|
|
1789
|
+
/* harmony import */ var _util_gql_error_js__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__(3);
|
|
1790
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GqlError", function() { return _util_gql_error_js__WEBPACK_IMPORTED_MODULE_17__["a"]; });
|
|
1334
1791
|
|
|
1792
|
+
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "GqlErrors", function() { return _util_gql_error_js__WEBPACK_IMPORTED_MODULE_17__["b"]; });
|
|
1793
|
+
|
|
1794
|
+
|
|
1795
|
+
|
|
1796
|
+
|
|
1797
|
+
|
|
1798
|
+
/**
|
|
1799
|
+
* Initialize the hydration cache.
|
|
1800
|
+
*
|
|
1801
|
+
* @param {ResponseCache} source The cache content to use for initializing the
|
|
1802
|
+
* cache.
|
|
1803
|
+
* @throws {Error} If the cache is already initialized.
|
|
1804
|
+
*/
|
|
1335
1805
|
const initializeCache = source => _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default.initialize(source);
|
|
1806
|
+
/**
|
|
1807
|
+
* Fulfill all tracked data requests.
|
|
1808
|
+
*
|
|
1809
|
+
* This is for use with the `TrackData` component during server-side rendering.
|
|
1810
|
+
*
|
|
1811
|
+
* @throws {Error} If executed outside of server-side rendering.
|
|
1812
|
+
* @returns {Promise<void>} A promise that resolves when all tracked requests
|
|
1813
|
+
* have been fulfilled.
|
|
1814
|
+
*/
|
|
1815
|
+
|
|
1336
1816
|
const fulfillAllDataRequests = () => {
|
|
1337
1817
|
if (!_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
1338
1818
|
return Promise.reject(new Error("Data requests are not tracked when client-side"));
|
|
@@ -1340,6 +1820,16 @@ const fulfillAllDataRequests = () => {
|
|
|
1340
1820
|
|
|
1341
1821
|
return _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestTracker */ "a"].Default.fulfillTrackedRequests();
|
|
1342
1822
|
};
|
|
1823
|
+
/**
|
|
1824
|
+
* Indicate if there are unfulfilled tracked requests.
|
|
1825
|
+
*
|
|
1826
|
+
* This is used in conjunction with `TrackData`.
|
|
1827
|
+
*
|
|
1828
|
+
* @throws {Error} If executed outside of server-side rendering.
|
|
1829
|
+
* @returns {boolean} `true` if there are unfulfilled tracked requests;
|
|
1830
|
+
* otherwise, `false`.
|
|
1831
|
+
*/
|
|
1832
|
+
|
|
1343
1833
|
const hasUnfulfilledRequests = () => {
|
|
1344
1834
|
if (!_khanacademy_wonder_blocks_core__WEBPACK_IMPORTED_MODULE_0__["Server"].isServerSide()) {
|
|
1345
1835
|
throw new Error("Data requests are not tracked when client-side");
|
|
@@ -1347,7 +1837,21 @@ const hasUnfulfilledRequests = () => {
|
|
|
1347
1837
|
|
|
1348
1838
|
return _util_request_tracking_js__WEBPACK_IMPORTED_MODULE_2__[/* RequestTracker */ "a"].Default.hasUnfulfilledRequests;
|
|
1349
1839
|
};
|
|
1840
|
+
/**
|
|
1841
|
+
* Remove the request identified from the cached hydration responses.
|
|
1842
|
+
*
|
|
1843
|
+
* @param {string} id The request ID of the response to remove from the cache.
|
|
1844
|
+
*/
|
|
1845
|
+
|
|
1350
1846
|
const removeFromCache = id => _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default.remove(id);
|
|
1847
|
+
/**
|
|
1848
|
+
* Remove all cached hydration responses that match the given predicate.
|
|
1849
|
+
*
|
|
1850
|
+
* @param {(id: string) => boolean} [predicate] The predicate to match against
|
|
1851
|
+
* the cached hydration responses. If no predicate is provided, all cached
|
|
1852
|
+
* hydration responses will be removed.
|
|
1853
|
+
*/
|
|
1854
|
+
|
|
1351
1855
|
const removeAllFromCache = predicate => _util_ssr_cache_js__WEBPACK_IMPORTED_MODULE_1__[/* SsrCache */ "a"].Default.removeAll(predicate);
|
|
1352
1856
|
|
|
1353
1857
|
|
|
@@ -1356,11 +1860,137 @@ const removeAllFromCache = predicate => _util_ssr_cache_js__WEBPACK_IMPORTED_MOD
|
|
|
1356
1860
|
|
|
1357
1861
|
|
|
1358
1862
|
|
|
1863
|
+
|
|
1864
|
+
|
|
1865
|
+
|
|
1866
|
+
|
|
1359
1867
|
// GraphQL
|
|
1360
1868
|
|
|
1361
1869
|
|
|
1362
1870
|
|
|
1363
1871
|
|
|
1364
1872
|
|
|
1873
|
+
/***/ }),
|
|
1874
|
+
/* 28 */
|
|
1875
|
+
/***/ (function(module, exports, __webpack_require__) {
|
|
1876
|
+
|
|
1877
|
+
"use strict";
|
|
1878
|
+
/**
|
|
1879
|
+
* Copyright (c) Facebook, Inc. and its affiliates.
|
|
1880
|
+
*
|
|
1881
|
+
* This source code is licensed under the MIT license found in the
|
|
1882
|
+
* LICENSE file in the root directory of this source tree.
|
|
1883
|
+
*/
|
|
1884
|
+
|
|
1885
|
+
|
|
1886
|
+
|
|
1887
|
+
// Below we want to use `hasOwnProperty` on an object that doesn't have
|
|
1888
|
+
// `Object.prototype` in its proto chain, so we must extract it here.
|
|
1889
|
+
var hasOwnProperty = Object.prototype.hasOwnProperty;
|
|
1890
|
+
|
|
1891
|
+
// Map from an enum object to a reverse map of its values to names
|
|
1892
|
+
var reverseMapCache = typeof WeakMap === 'function' ? new WeakMap() : new Map();
|
|
1893
|
+
|
|
1894
|
+
// Computes the reverse mapping of the enum object: from value to name.
|
|
1895
|
+
// Flow Enum values are unique (enforced by the parser), so this is a
|
|
1896
|
+
// one to one mapping.
|
|
1897
|
+
function getReverseMap(enumObject) {
|
|
1898
|
+
var reverseMap = reverseMapCache.get(enumObject);
|
|
1899
|
+
if (reverseMap !== undefined) {
|
|
1900
|
+
return reverseMap;
|
|
1901
|
+
}
|
|
1902
|
+
// We aren't using `Object.values` because that gets enumerable
|
|
1903
|
+
// properties, and our properties aren't enumerable.
|
|
1904
|
+
var newReverseMap = new Map();
|
|
1905
|
+
Object.getOwnPropertyNames(enumObject).forEach(function (name) {
|
|
1906
|
+
newReverseMap.set(enumObject[name], name);
|
|
1907
|
+
});
|
|
1908
|
+
reverseMapCache.set(enumObject, newReverseMap);
|
|
1909
|
+
return newReverseMap;
|
|
1910
|
+
}
|
|
1911
|
+
|
|
1912
|
+
var EnumPrototype = Object.freeze(
|
|
1913
|
+
Object.defineProperties(Object.create(null), {
|
|
1914
|
+
isValid: {
|
|
1915
|
+
value: function (x) {
|
|
1916
|
+
return getReverseMap(this).has(x);
|
|
1917
|
+
},
|
|
1918
|
+
},
|
|
1919
|
+
cast: {
|
|
1920
|
+
value: function (x) {
|
|
1921
|
+
return this.isValid(x) ? x : undefined;
|
|
1922
|
+
},
|
|
1923
|
+
},
|
|
1924
|
+
members: {
|
|
1925
|
+
value: function () {
|
|
1926
|
+
return getReverseMap(this).keys();
|
|
1927
|
+
},
|
|
1928
|
+
},
|
|
1929
|
+
getName: {
|
|
1930
|
+
value: function (value) {
|
|
1931
|
+
return getReverseMap(this).get(value);
|
|
1932
|
+
}
|
|
1933
|
+
}
|
|
1934
|
+
})
|
|
1935
|
+
);
|
|
1936
|
+
|
|
1937
|
+
// `members` is an object mapping name to value.
|
|
1938
|
+
function Enum(members) {
|
|
1939
|
+
var o = Object.create(EnumPrototype);
|
|
1940
|
+
for (var k in members) {
|
|
1941
|
+
if (hasOwnProperty.call(members, k)) {
|
|
1942
|
+
// Create non-enumerable properties.
|
|
1943
|
+
Object.defineProperty(o, k, {value: members[k]});
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
return Object.freeze(o);
|
|
1947
|
+
}
|
|
1948
|
+
|
|
1949
|
+
// Mirrored enum (string enum with no member initializers).
|
|
1950
|
+
// Optimized implementation, taking advantage of the fact that
|
|
1951
|
+
// keys and values are identical.
|
|
1952
|
+
var EnumMirroredPrototype = Object.freeze(
|
|
1953
|
+
Object.defineProperties(Object.create(null), {
|
|
1954
|
+
isValid: {
|
|
1955
|
+
value: function (x) {
|
|
1956
|
+
if (typeof x === 'string') {
|
|
1957
|
+
return hasOwnProperty.call(this, x);
|
|
1958
|
+
}
|
|
1959
|
+
return false;
|
|
1960
|
+
},
|
|
1961
|
+
},
|
|
1962
|
+
cast: {
|
|
1963
|
+
value: EnumPrototype.cast,
|
|
1964
|
+
},
|
|
1965
|
+
members: {
|
|
1966
|
+
value: function () {
|
|
1967
|
+
// We aren't using `Object.values` because that gets enumerable
|
|
1968
|
+
// properties, and our properties aren't enumerable.
|
|
1969
|
+
return Object.getOwnPropertyNames(this).values();
|
|
1970
|
+
},
|
|
1971
|
+
},
|
|
1972
|
+
getName: {
|
|
1973
|
+
value: function (value) {
|
|
1974
|
+
return value;
|
|
1975
|
+
}
|
|
1976
|
+
}
|
|
1977
|
+
})
|
|
1978
|
+
);
|
|
1979
|
+
|
|
1980
|
+
// `members` is an array of names (which, are also the values).
|
|
1981
|
+
Enum.Mirrored = function EnumMirrored(members) {
|
|
1982
|
+
var o = Object.create(EnumMirroredPrototype);
|
|
1983
|
+
for (var i = 0, len = members.length; i < len; ++i) {
|
|
1984
|
+
// Value is same as key. Also, non-enumerable.
|
|
1985
|
+
Object.defineProperty(o, members[i], {value: members[i]});
|
|
1986
|
+
}
|
|
1987
|
+
return Object.freeze(o);
|
|
1988
|
+
};
|
|
1989
|
+
|
|
1990
|
+
Object.freeze(Enum.Mirrored);
|
|
1991
|
+
|
|
1992
|
+
module.exports = Object.freeze(Enum);
|
|
1993
|
+
|
|
1994
|
+
|
|
1365
1995
|
/***/ })
|
|
1366
1996
|
/******/ ]);
|