@mjhls/mjh-framework 1.0.850-beta.0 → 1.0.850-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/AdSlotsProvider.js +2 -895
- package/dist/cjs/AlgoliaSearch.js +336 -237
- package/dist/cjs/ArticleProgramLandingPage.js +2 -3
- package/dist/cjs/ArticleSeriesLandingPage.js +1 -1
- package/dist/cjs/ArticleSeriesListing.js +1 -1
- package/dist/cjs/DeckContent.js +1 -1
- package/dist/cjs/DeckQueue.js +1 -1
- package/dist/cjs/ExternalResources.js +2 -3
- package/dist/cjs/GridContent.js +1 -1
- package/dist/cjs/HorizontalArticleListing.js +2 -3
- package/dist/cjs/IssueLanding.js +2 -3
- package/dist/cjs/MasterDeck.js +1 -1
- package/dist/cjs/MediaSeriesLanding.js +5 -6
- package/dist/cjs/PartnerDetailListing.js +6 -1105
- package/dist/cjs/QueueDeckExpanded.js +1097 -112
- package/dist/cjs/TaxonomyDescription.js +2 -3
- package/dist/cjs/VideoProgramLandingPage.js +2 -3
- package/dist/cjs/VideoSeriesCard.js +2 -2
- package/dist/cjs/VideoSeriesLandingPage.js +3 -3
- package/dist/cjs/VideoSeriesListing.js +1 -1
- package/dist/cjs/View.js +5 -7
- package/dist/cjs/faundadb.js +321 -4
- package/dist/cjs/getRelatedArticle.js +21 -438
- package/dist/cjs/getSerializers.js +2 -3
- package/dist/cjs/{index-bc88f898.js → index-4151deb3.js} +587 -15
- package/dist/cjs/index.js +6 -13
- package/dist/cjs/{inherits-452ff02c.js → inherits-9953db94.js} +4 -4
- package/dist/esm/AdSlotsProvider.js +6 -898
- package/dist/esm/AlgoliaSearch.js +100 -1
- package/dist/esm/ArticleProgramLandingPage.js +1 -2
- package/dist/esm/ExternalResources.js +1 -2
- package/dist/esm/HorizontalArticleListing.js +1 -2
- package/dist/esm/IssueLanding.js +1 -2
- package/dist/esm/MediaSeriesLanding.js +1 -2
- package/dist/esm/PartnerDetailListing.js +5 -1103
- package/dist/esm/QueueDeckExpanded.js +1098 -113
- package/dist/esm/TaxonomyDescription.js +1 -2
- package/dist/esm/VideoProgramLandingPage.js +1 -2
- package/dist/esm/View.js +2 -4
- package/dist/esm/faundadb.js +319 -2
- package/dist/esm/getRelatedArticle.js +21 -438
- package/dist/esm/getSerializers.js +1 -2
- package/dist/esm/{index-f2a0d400.js → index-d6dc592a.js} +575 -3
- package/dist/esm/index.js +4 -10
- package/package.json +8 -3
- package/dist/cjs/Auth.js +0 -3428
- package/dist/cjs/index-bd6c9f56.js +0 -211
- package/dist/cjs/inherits-8d29278d.js +0 -110
- package/dist/cjs/md5-5039b1a6.js +0 -323
- package/dist/cjs/util-f2c1b65b.js +0 -576
- package/dist/esm/Auth.js +0 -3412
- package/dist/esm/index-db3bb315.js +0 -207
- package/dist/esm/inherits-77d5e4fc.js +0 -101
- package/dist/esm/md5-9be0e905.js +0 -321
- package/dist/esm/util-7700fc59.js +0 -574
|
@@ -52,6 +52,1055 @@ var reactPaginate = _commonjsHelpers.createCommonjsModule(function (module, expo
|
|
|
52
52
|
var ReactPaginate = _commonjsHelpers.unwrapExports(reactPaginate);
|
|
53
53
|
var reactPaginate_1 = reactPaginate.ReactPaginate;
|
|
54
54
|
|
|
55
|
+
var has = Object.prototype.hasOwnProperty;
|
|
56
|
+
|
|
57
|
+
function dequal(foo, bar) {
|
|
58
|
+
var ctor, len;
|
|
59
|
+
if (foo === bar) return true;
|
|
60
|
+
|
|
61
|
+
if (foo && bar && (ctor=foo.constructor) === bar.constructor) {
|
|
62
|
+
if (ctor === Date) return foo.getTime() === bar.getTime();
|
|
63
|
+
if (ctor === RegExp) return foo.toString() === bar.toString();
|
|
64
|
+
|
|
65
|
+
if (ctor === Array) {
|
|
66
|
+
if ((len=foo.length) === bar.length) {
|
|
67
|
+
while (len-- && dequal(foo[len], bar[len]));
|
|
68
|
+
}
|
|
69
|
+
return len === -1;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!ctor || typeof foo === 'object') {
|
|
73
|
+
len = 0;
|
|
74
|
+
for (ctor in foo) {
|
|
75
|
+
if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;
|
|
76
|
+
if (!(ctor in bar) || !dequal(foo[ctor], bar[ctor])) return false;
|
|
77
|
+
}
|
|
78
|
+
return Object.keys(bar).length === len;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return foo !== foo && bar !== bar;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// use WeakMap to store the object->key mapping
|
|
86
|
+
// so the objects can be garbage collected.
|
|
87
|
+
// WeakMap uses a hashtable under the hood, so the lookup
|
|
88
|
+
// complexity is almost O(1).
|
|
89
|
+
var table = new WeakMap();
|
|
90
|
+
// counter of the key
|
|
91
|
+
var counter = 0;
|
|
92
|
+
// hashes an array of objects and returns a string
|
|
93
|
+
function hash(args) {
|
|
94
|
+
if (!args.length)
|
|
95
|
+
return '';
|
|
96
|
+
var key = 'arg';
|
|
97
|
+
for (var i = 0; i < args.length; ++i) {
|
|
98
|
+
if (args[i] === null) {
|
|
99
|
+
key += '@null';
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
var _hash = void 0;
|
|
103
|
+
if (typeof args[i] !== 'object' && typeof args[i] !== 'function') {
|
|
104
|
+
// need to consider the case that args[i] is a string:
|
|
105
|
+
// args[i] _hash
|
|
106
|
+
// "undefined" -> '"undefined"'
|
|
107
|
+
// undefined -> 'undefined'
|
|
108
|
+
// 123 -> '123'
|
|
109
|
+
// "null" -> '"null"'
|
|
110
|
+
if (typeof args[i] === 'string') {
|
|
111
|
+
_hash = '"' + args[i] + '"';
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
_hash = String(args[i]);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
if (!table.has(args[i])) {
|
|
119
|
+
_hash = counter;
|
|
120
|
+
table.set(args[i], counter++);
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
_hash = table.get(args[i]);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
key += '@' + _hash;
|
|
127
|
+
}
|
|
128
|
+
return key;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
var Cache = /** @class */ (function () {
|
|
132
|
+
function Cache(initialData) {
|
|
133
|
+
if (initialData === void 0) { initialData = {}; }
|
|
134
|
+
this.cache = new Map(Object.entries(initialData));
|
|
135
|
+
this.subs = [];
|
|
136
|
+
}
|
|
137
|
+
Cache.prototype.get = function (key) {
|
|
138
|
+
var _key = this.serializeKey(key)[0];
|
|
139
|
+
return this.cache.get(_key);
|
|
140
|
+
};
|
|
141
|
+
Cache.prototype.set = function (key, value) {
|
|
142
|
+
var _key = this.serializeKey(key)[0];
|
|
143
|
+
this.cache.set(_key, value);
|
|
144
|
+
this.notify();
|
|
145
|
+
};
|
|
146
|
+
Cache.prototype.keys = function () {
|
|
147
|
+
return Array.from(this.cache.keys());
|
|
148
|
+
};
|
|
149
|
+
Cache.prototype.has = function (key) {
|
|
150
|
+
var _key = this.serializeKey(key)[0];
|
|
151
|
+
return this.cache.has(_key);
|
|
152
|
+
};
|
|
153
|
+
Cache.prototype.clear = function () {
|
|
154
|
+
this.cache.clear();
|
|
155
|
+
this.notify();
|
|
156
|
+
};
|
|
157
|
+
Cache.prototype.delete = function (key) {
|
|
158
|
+
var _key = this.serializeKey(key)[0];
|
|
159
|
+
this.cache.delete(_key);
|
|
160
|
+
this.notify();
|
|
161
|
+
};
|
|
162
|
+
// TODO: introduce namespace for the cache
|
|
163
|
+
Cache.prototype.serializeKey = function (key) {
|
|
164
|
+
var args = null;
|
|
165
|
+
if (typeof key === 'function') {
|
|
166
|
+
try {
|
|
167
|
+
key = key();
|
|
168
|
+
}
|
|
169
|
+
catch (err) {
|
|
170
|
+
// dependencies not ready
|
|
171
|
+
key = '';
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
if (Array.isArray(key)) {
|
|
175
|
+
// args array
|
|
176
|
+
args = key;
|
|
177
|
+
key = hash(key);
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
// convert null to ''
|
|
181
|
+
key = String(key || '');
|
|
182
|
+
}
|
|
183
|
+
var errorKey = key ? 'err@' + key : '';
|
|
184
|
+
var isValidatingKey = key ? 'validating@' + key : '';
|
|
185
|
+
return [key, args, errorKey, isValidatingKey];
|
|
186
|
+
};
|
|
187
|
+
Cache.prototype.subscribe = function (listener) {
|
|
188
|
+
var _this = this;
|
|
189
|
+
if (typeof listener !== 'function') {
|
|
190
|
+
throw new Error('Expected the listener to be a function.');
|
|
191
|
+
}
|
|
192
|
+
var isSubscribed = true;
|
|
193
|
+
this.subs.push(listener);
|
|
194
|
+
return function () {
|
|
195
|
+
if (!isSubscribed)
|
|
196
|
+
return;
|
|
197
|
+
isSubscribed = false;
|
|
198
|
+
var index = _this.subs.indexOf(listener);
|
|
199
|
+
if (index > -1) {
|
|
200
|
+
_this.subs[index] = _this.subs[_this.subs.length - 1];
|
|
201
|
+
_this.subs.length--;
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
};
|
|
205
|
+
// Notify Cache subscribers about a change in the cache
|
|
206
|
+
Cache.prototype.notify = function () {
|
|
207
|
+
for (var _i = 0, _a = this.subs; _i < _a.length; _i++) {
|
|
208
|
+
var listener = _a[_i];
|
|
209
|
+
listener();
|
|
210
|
+
}
|
|
211
|
+
};
|
|
212
|
+
return Cache;
|
|
213
|
+
}());
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Due to bug https://bugs.chromium.org/p/chromium/issues/detail?id=678075,
|
|
217
|
+
* it's not reliable to detect if the browser is currently online or offline
|
|
218
|
+
* based on `navigator.onLine`.
|
|
219
|
+
* As a work around, we always assume it's online on first load, and change
|
|
220
|
+
* the status upon `online` or `offline` events.
|
|
221
|
+
*/
|
|
222
|
+
var online = true;
|
|
223
|
+
var isOnline = function () { return online; };
|
|
224
|
+
var isDocumentVisible = function () {
|
|
225
|
+
if (typeof document !== 'undefined' &&
|
|
226
|
+
document.visibilityState !== undefined) {
|
|
227
|
+
return document.visibilityState !== 'hidden';
|
|
228
|
+
}
|
|
229
|
+
// always assume it's visible
|
|
230
|
+
return true;
|
|
231
|
+
};
|
|
232
|
+
var fetcher = function (url) { return fetch(url).then(function (res) { return res.json(); }); };
|
|
233
|
+
var registerOnFocus = function (cb) {
|
|
234
|
+
if (typeof window !== 'undefined' &&
|
|
235
|
+
window.addEventListener !== undefined &&
|
|
236
|
+
typeof document !== 'undefined' &&
|
|
237
|
+
document.addEventListener !== undefined) {
|
|
238
|
+
// focus revalidate
|
|
239
|
+
document.addEventListener('visibilitychange', function () { return cb(); }, false);
|
|
240
|
+
window.addEventListener('focus', function () { return cb(); }, false);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
var registerOnReconnect = function (cb) {
|
|
244
|
+
if (typeof window !== 'undefined' && window.addEventListener !== undefined) {
|
|
245
|
+
// reconnect revalidate
|
|
246
|
+
window.addEventListener('online', function () {
|
|
247
|
+
online = true;
|
|
248
|
+
cb();
|
|
249
|
+
}, false);
|
|
250
|
+
// nothing to revalidate, just update the status
|
|
251
|
+
window.addEventListener('offline', function () { return (online = false); }, false);
|
|
252
|
+
}
|
|
253
|
+
};
|
|
254
|
+
var webPreset = {
|
|
255
|
+
isOnline: isOnline,
|
|
256
|
+
isDocumentVisible: isDocumentVisible,
|
|
257
|
+
fetcher: fetcher,
|
|
258
|
+
registerOnFocus: registerOnFocus,
|
|
259
|
+
registerOnReconnect: registerOnReconnect
|
|
260
|
+
};
|
|
261
|
+
|
|
262
|
+
var __assign = (undefined && undefined.__assign) || function () {
|
|
263
|
+
__assign = Object.assign || function(t) {
|
|
264
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
265
|
+
s = arguments[i];
|
|
266
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
267
|
+
t[p] = s[p];
|
|
268
|
+
}
|
|
269
|
+
return t;
|
|
270
|
+
};
|
|
271
|
+
return __assign.apply(this, arguments);
|
|
272
|
+
};
|
|
273
|
+
// cache
|
|
274
|
+
var cache = new Cache();
|
|
275
|
+
// error retry
|
|
276
|
+
function onErrorRetry(_, __, config, revalidate, opts) {
|
|
277
|
+
if (!config.isDocumentVisible()) {
|
|
278
|
+
// if it's hidden, stop
|
|
279
|
+
// it will auto revalidate when focus
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
if (typeof config.errorRetryCount === 'number' &&
|
|
283
|
+
opts.retryCount > config.errorRetryCount) {
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
// exponential backoff
|
|
287
|
+
var count = Math.min(opts.retryCount, 8);
|
|
288
|
+
var timeout = ~~((Math.random() + 0.5) * (1 << count)) * config.errorRetryInterval;
|
|
289
|
+
setTimeout(revalidate, timeout, opts);
|
|
290
|
+
}
|
|
291
|
+
// client side: need to adjust the config
|
|
292
|
+
// based on the browser status
|
|
293
|
+
// slow connection (<= 70Kbps)
|
|
294
|
+
var slowConnection = typeof window !== 'undefined' &&
|
|
295
|
+
// @ts-ignore
|
|
296
|
+
navigator['connection'] &&
|
|
297
|
+
// @ts-ignore
|
|
298
|
+
['slow-2g', '2g'].indexOf(navigator['connection'].effectiveType) !== -1;
|
|
299
|
+
// config
|
|
300
|
+
var defaultConfig = __assign({
|
|
301
|
+
// events
|
|
302
|
+
onLoadingSlow: function () { }, onSuccess: function () { }, onError: function () { }, onErrorRetry: onErrorRetry, errorRetryInterval: (slowConnection ? 10 : 5) * 1000, focusThrottleInterval: 5 * 1000, dedupingInterval: 2 * 1000, loadingTimeout: (slowConnection ? 5 : 3) * 1000, refreshInterval: 0, revalidateOnFocus: true, revalidateOnReconnect: true, refreshWhenHidden: false, refreshWhenOffline: false, shouldRetryOnError: true, suspense: false, compare: dequal, isPaused: function () { return false; } }, webPreset);
|
|
303
|
+
|
|
304
|
+
var IS_SERVER = typeof window === 'undefined' ||
|
|
305
|
+
// @ts-ignore
|
|
306
|
+
!!(typeof Deno !== 'undefined' && Deno && Deno.version && Deno.version.deno);
|
|
307
|
+
// polyfill for requestAnimationFrame
|
|
308
|
+
var rAF = IS_SERVER
|
|
309
|
+
? null
|
|
310
|
+
: window['requestAnimationFrame']
|
|
311
|
+
? function (f) { return window['requestAnimationFrame'](f); }
|
|
312
|
+
: function (f) { return setTimeout(f, 1); };
|
|
313
|
+
// React currently throws a warning when using useLayoutEffect on the server.
|
|
314
|
+
// To get around it, we can conditionally useEffect on the server (no-op) and
|
|
315
|
+
// useLayoutEffect in the browser.
|
|
316
|
+
var useIsomorphicLayoutEffect = IS_SERVER ? React.useEffect : React.useLayoutEffect;
|
|
317
|
+
|
|
318
|
+
var SWRConfigContext = React.createContext({});
|
|
319
|
+
SWRConfigContext.displayName = 'SWRConfigContext';
|
|
320
|
+
|
|
321
|
+
var __awaiter = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
322
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
323
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
324
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
325
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
326
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
327
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
328
|
+
});
|
|
329
|
+
};
|
|
330
|
+
var __generator = (undefined && undefined.__generator) || function (thisArg, body) {
|
|
331
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
332
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
333
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
334
|
+
function step(op) {
|
|
335
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
336
|
+
while (_) try {
|
|
337
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
338
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
339
|
+
switch (op[0]) {
|
|
340
|
+
case 0: case 1: t = op; break;
|
|
341
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
342
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
343
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
344
|
+
default:
|
|
345
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
346
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
347
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
348
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
349
|
+
if (t[2]) _.ops.pop();
|
|
350
|
+
_.trys.pop(); continue;
|
|
351
|
+
}
|
|
352
|
+
op = body.call(thisArg, _);
|
|
353
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
354
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
// global state managers
|
|
358
|
+
var CONCURRENT_PROMISES = {};
|
|
359
|
+
var CONCURRENT_PROMISES_TS = {};
|
|
360
|
+
var FOCUS_REVALIDATORS = {};
|
|
361
|
+
var RECONNECT_REVALIDATORS = {};
|
|
362
|
+
var CACHE_REVALIDATORS = {};
|
|
363
|
+
var MUTATION_TS = {};
|
|
364
|
+
var MUTATION_END_TS = {};
|
|
365
|
+
// generate strictly increasing timestamps
|
|
366
|
+
var now = (function () {
|
|
367
|
+
var ts = 0;
|
|
368
|
+
return function () { return ++ts; };
|
|
369
|
+
})();
|
|
370
|
+
// setup DOM events listeners for `focus` and `reconnect` actions
|
|
371
|
+
if (!IS_SERVER) {
|
|
372
|
+
var revalidate_1 = function (revalidators) {
|
|
373
|
+
if (!defaultConfig.isDocumentVisible() || !defaultConfig.isOnline())
|
|
374
|
+
return;
|
|
375
|
+
for (var key in revalidators) {
|
|
376
|
+
if (revalidators[key][0])
|
|
377
|
+
revalidators[key][0]();
|
|
378
|
+
}
|
|
379
|
+
};
|
|
380
|
+
if (typeof defaultConfig.registerOnFocus === 'function') {
|
|
381
|
+
defaultConfig.registerOnFocus(function () { return revalidate_1(FOCUS_REVALIDATORS); });
|
|
382
|
+
}
|
|
383
|
+
if (typeof defaultConfig.registerOnReconnect === 'function') {
|
|
384
|
+
defaultConfig.registerOnReconnect(function () { return revalidate_1(RECONNECT_REVALIDATORS); });
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
var trigger = function (_key, shouldRevalidate) {
|
|
388
|
+
if (shouldRevalidate === void 0) { shouldRevalidate = true; }
|
|
389
|
+
// we are ignoring the second argument which correspond to the arguments
|
|
390
|
+
// the fetcher will receive when key is an array
|
|
391
|
+
var _a = cache.serializeKey(_key), key = _a[0], keyErr = _a[2], keyValidating = _a[3];
|
|
392
|
+
if (!key)
|
|
393
|
+
return Promise.resolve();
|
|
394
|
+
var updaters = CACHE_REVALIDATORS[key];
|
|
395
|
+
if (key && updaters) {
|
|
396
|
+
var currentData = cache.get(key);
|
|
397
|
+
var currentError = cache.get(keyErr);
|
|
398
|
+
var currentIsValidating = cache.get(keyValidating);
|
|
399
|
+
var promises = [];
|
|
400
|
+
for (var i = 0; i < updaters.length; ++i) {
|
|
401
|
+
promises.push(updaters[i](shouldRevalidate, currentData, currentError, currentIsValidating, i > 0));
|
|
402
|
+
}
|
|
403
|
+
// return new updated value
|
|
404
|
+
return Promise.all(promises).then(function () { return cache.get(key); });
|
|
405
|
+
}
|
|
406
|
+
return Promise.resolve(cache.get(key));
|
|
407
|
+
};
|
|
408
|
+
var broadcastState = function (key, data, error, isValidating) {
|
|
409
|
+
var updaters = CACHE_REVALIDATORS[key];
|
|
410
|
+
if (key && updaters) {
|
|
411
|
+
for (var i = 0; i < updaters.length; ++i) {
|
|
412
|
+
updaters[i](false, data, error, isValidating);
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
};
|
|
416
|
+
var mutate = function (_key, _data, shouldRevalidate) {
|
|
417
|
+
if (shouldRevalidate === void 0) { shouldRevalidate = true; }
|
|
418
|
+
return __awaiter(void 0, void 0, void 0, function () {
|
|
419
|
+
var _a, key, keyErr, beforeMutationTs, beforeConcurrentPromisesTs, data, error, isAsyncMutation, err_1, shouldAbort, updaters, promises, i;
|
|
420
|
+
return __generator(this, function (_b) {
|
|
421
|
+
switch (_b.label) {
|
|
422
|
+
case 0:
|
|
423
|
+
_a = cache.serializeKey(_key), key = _a[0], keyErr = _a[2];
|
|
424
|
+
if (!key)
|
|
425
|
+
return [2 /*return*/];
|
|
426
|
+
// if there is no new data to update, let's just revalidate the key
|
|
427
|
+
if (typeof _data === 'undefined')
|
|
428
|
+
return [2 /*return*/, trigger(_key, shouldRevalidate)
|
|
429
|
+
// update global timestamps
|
|
430
|
+
];
|
|
431
|
+
// update global timestamps
|
|
432
|
+
MUTATION_TS[key] = now() - 1;
|
|
433
|
+
MUTATION_END_TS[key] = 0;
|
|
434
|
+
beforeMutationTs = MUTATION_TS[key];
|
|
435
|
+
beforeConcurrentPromisesTs = CONCURRENT_PROMISES_TS[key];
|
|
436
|
+
isAsyncMutation = false;
|
|
437
|
+
if (_data && typeof _data === 'function') {
|
|
438
|
+
// `_data` is a function, call it passing current cache value
|
|
439
|
+
try {
|
|
440
|
+
_data = _data(cache.get(key));
|
|
441
|
+
}
|
|
442
|
+
catch (err) {
|
|
443
|
+
// if `_data` function throws an error synchronously, it shouldn't be cached
|
|
444
|
+
_data = undefined;
|
|
445
|
+
error = err;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
if (!(_data && typeof _data.then === 'function')) return [3 /*break*/, 5];
|
|
449
|
+
// `_data` is a promise
|
|
450
|
+
isAsyncMutation = true;
|
|
451
|
+
_b.label = 1;
|
|
452
|
+
case 1:
|
|
453
|
+
_b.trys.push([1, 3, , 4]);
|
|
454
|
+
return [4 /*yield*/, _data];
|
|
455
|
+
case 2:
|
|
456
|
+
data = _b.sent();
|
|
457
|
+
return [3 /*break*/, 4];
|
|
458
|
+
case 3:
|
|
459
|
+
err_1 = _b.sent();
|
|
460
|
+
error = err_1;
|
|
461
|
+
return [3 /*break*/, 4];
|
|
462
|
+
case 4: return [3 /*break*/, 6];
|
|
463
|
+
case 5:
|
|
464
|
+
data = _data;
|
|
465
|
+
_b.label = 6;
|
|
466
|
+
case 6:
|
|
467
|
+
shouldAbort = function () {
|
|
468
|
+
// check if other mutations have occurred since we've started this mutation
|
|
469
|
+
if (beforeMutationTs !== MUTATION_TS[key] ||
|
|
470
|
+
beforeConcurrentPromisesTs !== CONCURRENT_PROMISES_TS[key]) {
|
|
471
|
+
if (error)
|
|
472
|
+
throw error;
|
|
473
|
+
return true;
|
|
474
|
+
}
|
|
475
|
+
};
|
|
476
|
+
// if there's a race we don't update cache or broadcast change, just return the data
|
|
477
|
+
if (shouldAbort())
|
|
478
|
+
return [2 /*return*/, data];
|
|
479
|
+
if (typeof data !== 'undefined') {
|
|
480
|
+
// update cached data
|
|
481
|
+
cache.set(key, data);
|
|
482
|
+
}
|
|
483
|
+
// always update or reset the error
|
|
484
|
+
cache.set(keyErr, error);
|
|
485
|
+
// reset the timestamp to mark the mutation has ended
|
|
486
|
+
MUTATION_END_TS[key] = now() - 1;
|
|
487
|
+
if (!isAsyncMutation) {
|
|
488
|
+
// we skip broadcasting if there's another mutation happened synchronously
|
|
489
|
+
if (shouldAbort())
|
|
490
|
+
return [2 /*return*/, data];
|
|
491
|
+
}
|
|
492
|
+
updaters = CACHE_REVALIDATORS[key];
|
|
493
|
+
if (updaters) {
|
|
494
|
+
promises = [];
|
|
495
|
+
for (i = 0; i < updaters.length; ++i) {
|
|
496
|
+
promises.push(updaters[i](!!shouldRevalidate, data, error, undefined, i > 0));
|
|
497
|
+
}
|
|
498
|
+
// return new updated value
|
|
499
|
+
return [2 /*return*/, Promise.all(promises).then(function () {
|
|
500
|
+
if (error)
|
|
501
|
+
throw error;
|
|
502
|
+
return cache.get(key);
|
|
503
|
+
})];
|
|
504
|
+
}
|
|
505
|
+
// throw error or return data to be used by caller of mutate
|
|
506
|
+
if (error)
|
|
507
|
+
throw error;
|
|
508
|
+
return [2 /*return*/, data];
|
|
509
|
+
}
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
};
|
|
513
|
+
function useSWR() {
|
|
514
|
+
var _this = this;
|
|
515
|
+
var args = [];
|
|
516
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
517
|
+
args[_i] = arguments[_i];
|
|
518
|
+
}
|
|
519
|
+
var _key = args[0];
|
|
520
|
+
var config = Object.assign({}, defaultConfig, React.useContext(SWRConfigContext), args.length > 2
|
|
521
|
+
? args[2]
|
|
522
|
+
: args.length === 2 && typeof args[1] === 'object'
|
|
523
|
+
? args[1]
|
|
524
|
+
: {});
|
|
525
|
+
// in typescript args.length > 2 is not same as args.lenth === 3
|
|
526
|
+
// we do a safe type assertion here
|
|
527
|
+
// args.length === 3
|
|
528
|
+
var fn = (args.length > 2
|
|
529
|
+
? args[1]
|
|
530
|
+
: args.length === 2 && typeof args[1] === 'function'
|
|
531
|
+
? args[1]
|
|
532
|
+
: /**
|
|
533
|
+
pass fn as null will disable revalidate
|
|
534
|
+
https://paco.sh/blog/shared-hook-state-with-swr
|
|
535
|
+
*/
|
|
536
|
+
args[1] === null
|
|
537
|
+
? args[1]
|
|
538
|
+
: config.fetcher);
|
|
539
|
+
// we assume `key` as the identifier of the request
|
|
540
|
+
// `key` can change but `fn` shouldn't
|
|
541
|
+
// (because `revalidate` only depends on `key`)
|
|
542
|
+
// `keyErr` is the cache key for error objects
|
|
543
|
+
var _a = cache.serializeKey(_key), key = _a[0], fnArgs = _a[1], keyErr = _a[2], keyValidating = _a[3];
|
|
544
|
+
var configRef = React.useRef(config);
|
|
545
|
+
useIsomorphicLayoutEffect(function () {
|
|
546
|
+
configRef.current = config;
|
|
547
|
+
});
|
|
548
|
+
var willRevalidateOnMount = function () {
|
|
549
|
+
return (config.revalidateOnMount ||
|
|
550
|
+
(!config.initialData && config.revalidateOnMount === undefined));
|
|
551
|
+
};
|
|
552
|
+
var resolveData = function () {
|
|
553
|
+
var cachedData = cache.get(key);
|
|
554
|
+
return typeof cachedData === 'undefined' ? config.initialData : cachedData;
|
|
555
|
+
};
|
|
556
|
+
var resolveIsValidating = function () {
|
|
557
|
+
return !!cache.get(keyValidating) || (key && willRevalidateOnMount());
|
|
558
|
+
};
|
|
559
|
+
var initialData = resolveData();
|
|
560
|
+
var initialError = cache.get(keyErr);
|
|
561
|
+
var initialIsValidating = resolveIsValidating();
|
|
562
|
+
// if a state is accessed (data, error or isValidating),
|
|
563
|
+
// we add the state to dependencies so if the state is
|
|
564
|
+
// updated in the future, we can trigger a rerender
|
|
565
|
+
var stateDependencies = React.useRef({
|
|
566
|
+
data: false,
|
|
567
|
+
error: false,
|
|
568
|
+
isValidating: false
|
|
569
|
+
});
|
|
570
|
+
var stateRef = React.useRef({
|
|
571
|
+
data: initialData,
|
|
572
|
+
error: initialError,
|
|
573
|
+
isValidating: initialIsValidating
|
|
574
|
+
});
|
|
575
|
+
// display the data label in the React DevTools next to SWR hooks
|
|
576
|
+
React.useDebugValue(stateRef.current.data);
|
|
577
|
+
var rerender = React.useState({})[1];
|
|
578
|
+
var dispatch = React.useCallback(function (payload) {
|
|
579
|
+
var shouldUpdateState = false;
|
|
580
|
+
for (var k in payload) {
|
|
581
|
+
// @ts-ignore
|
|
582
|
+
if (stateRef.current[k] === payload[k]) {
|
|
583
|
+
continue;
|
|
584
|
+
}
|
|
585
|
+
// @ts-ignore
|
|
586
|
+
stateRef.current[k] = payload[k];
|
|
587
|
+
// @ts-ignore
|
|
588
|
+
if (stateDependencies.current[k]) {
|
|
589
|
+
shouldUpdateState = true;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
if (shouldUpdateState) {
|
|
593
|
+
// if component is unmounted, should skip rerender
|
|
594
|
+
// if component is not mounted, should skip rerender
|
|
595
|
+
if (unmountedRef.current || !initialMountedRef.current)
|
|
596
|
+
return;
|
|
597
|
+
rerender({});
|
|
598
|
+
}
|
|
599
|
+
},
|
|
600
|
+
// config.suspense isn't allowed to change during the lifecycle
|
|
601
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
602
|
+
[]);
|
|
603
|
+
// error ref inside revalidate (is last request errored?)
|
|
604
|
+
var unmountedRef = React.useRef(false);
|
|
605
|
+
var keyRef = React.useRef(key);
|
|
606
|
+
// check if component is mounted in suspense mode
|
|
607
|
+
var initialMountedRef = React.useRef(false);
|
|
608
|
+
// do unmount check for callbacks
|
|
609
|
+
var eventsCallback = React.useCallback(function (event) {
|
|
610
|
+
var _a;
|
|
611
|
+
var params = [];
|
|
612
|
+
for (var _i = 1; _i < arguments.length; _i++) {
|
|
613
|
+
params[_i - 1] = arguments[_i];
|
|
614
|
+
}
|
|
615
|
+
if (unmountedRef.current)
|
|
616
|
+
return;
|
|
617
|
+
if (!initialMountedRef.current)
|
|
618
|
+
return;
|
|
619
|
+
if (key !== keyRef.current)
|
|
620
|
+
return;
|
|
621
|
+
// @ts-ignore
|
|
622
|
+
(_a = configRef.current)[event].apply(_a, params);
|
|
623
|
+
}, [key]);
|
|
624
|
+
var boundMutate = React.useCallback(function (data, shouldRevalidate) {
|
|
625
|
+
return mutate(keyRef.current, data, shouldRevalidate);
|
|
626
|
+
}, []);
|
|
627
|
+
var addRevalidator = function (revalidators, callback) {
|
|
628
|
+
if (!revalidators[key]) {
|
|
629
|
+
revalidators[key] = [callback];
|
|
630
|
+
}
|
|
631
|
+
else {
|
|
632
|
+
revalidators[key].push(callback);
|
|
633
|
+
}
|
|
634
|
+
return function () {
|
|
635
|
+
var keyedRevalidators = revalidators[key];
|
|
636
|
+
var index = keyedRevalidators.indexOf(callback);
|
|
637
|
+
if (index >= 0) {
|
|
638
|
+
// O(1): faster than splice
|
|
639
|
+
keyedRevalidators[index] =
|
|
640
|
+
keyedRevalidators[keyedRevalidators.length - 1];
|
|
641
|
+
keyedRevalidators.pop();
|
|
642
|
+
}
|
|
643
|
+
};
|
|
644
|
+
};
|
|
645
|
+
// start a revalidation
|
|
646
|
+
var revalidate = React.useCallback(function (revalidateOpts) {
|
|
647
|
+
if (revalidateOpts === void 0) { revalidateOpts = {}; }
|
|
648
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
649
|
+
var _a, retryCount, _b, dedupe, loading, shouldDeduping, newData, startAt, newState, err_2;
|
|
650
|
+
return __generator(this, function (_c) {
|
|
651
|
+
switch (_c.label) {
|
|
652
|
+
case 0:
|
|
653
|
+
if (!key || !fn)
|
|
654
|
+
return [2 /*return*/, false];
|
|
655
|
+
if (unmountedRef.current)
|
|
656
|
+
return [2 /*return*/, false];
|
|
657
|
+
if (configRef.current.isPaused())
|
|
658
|
+
return [2 /*return*/, false];
|
|
659
|
+
_a = revalidateOpts.retryCount, retryCount = _a === void 0 ? 0 : _a, _b = revalidateOpts.dedupe, dedupe = _b === void 0 ? false : _b;
|
|
660
|
+
loading = true;
|
|
661
|
+
shouldDeduping = typeof CONCURRENT_PROMISES[key] !== 'undefined' && dedupe;
|
|
662
|
+
_c.label = 1;
|
|
663
|
+
case 1:
|
|
664
|
+
_c.trys.push([1, 6, , 7]);
|
|
665
|
+
dispatch({
|
|
666
|
+
isValidating: true
|
|
667
|
+
});
|
|
668
|
+
cache.set(keyValidating, true);
|
|
669
|
+
if (!shouldDeduping) {
|
|
670
|
+
// also update other hooks
|
|
671
|
+
broadcastState(key, stateRef.current.data, stateRef.current.error, true);
|
|
672
|
+
}
|
|
673
|
+
newData = void 0;
|
|
674
|
+
startAt = void 0;
|
|
675
|
+
if (!shouldDeduping) return [3 /*break*/, 3];
|
|
676
|
+
// there's already an ongoing request,
|
|
677
|
+
// this one needs to be deduplicated.
|
|
678
|
+
startAt = CONCURRENT_PROMISES_TS[key];
|
|
679
|
+
return [4 /*yield*/, CONCURRENT_PROMISES[key]];
|
|
680
|
+
case 2:
|
|
681
|
+
newData = _c.sent();
|
|
682
|
+
return [3 /*break*/, 5];
|
|
683
|
+
case 3:
|
|
684
|
+
// if no cache being rendered currently (it shows a blank page),
|
|
685
|
+
// we trigger the loading slow event.
|
|
686
|
+
if (config.loadingTimeout && !cache.get(key)) {
|
|
687
|
+
setTimeout(function () {
|
|
688
|
+
if (loading)
|
|
689
|
+
eventsCallback('onLoadingSlow', key, config);
|
|
690
|
+
}, config.loadingTimeout);
|
|
691
|
+
}
|
|
692
|
+
if (fnArgs !== null) {
|
|
693
|
+
CONCURRENT_PROMISES[key] = fn.apply(void 0, fnArgs);
|
|
694
|
+
}
|
|
695
|
+
else {
|
|
696
|
+
CONCURRENT_PROMISES[key] = fn(key);
|
|
697
|
+
}
|
|
698
|
+
CONCURRENT_PROMISES_TS[key] = startAt = now();
|
|
699
|
+
return [4 /*yield*/, CONCURRENT_PROMISES[key]];
|
|
700
|
+
case 4:
|
|
701
|
+
newData = _c.sent();
|
|
702
|
+
setTimeout(function () {
|
|
703
|
+
delete CONCURRENT_PROMISES[key];
|
|
704
|
+
delete CONCURRENT_PROMISES_TS[key];
|
|
705
|
+
}, config.dedupingInterval);
|
|
706
|
+
// trigger the success event,
|
|
707
|
+
// only do this for the original request.
|
|
708
|
+
eventsCallback('onSuccess', newData, key, config);
|
|
709
|
+
_c.label = 5;
|
|
710
|
+
case 5:
|
|
711
|
+
// if there're other ongoing request(s), started after the current one,
|
|
712
|
+
// we need to ignore the current one to avoid possible race conditions:
|
|
713
|
+
// req1------------------>res1 (current one)
|
|
714
|
+
// req2---------------->res2
|
|
715
|
+
// the request that fired later will always be kept.
|
|
716
|
+
if (CONCURRENT_PROMISES_TS[key] > startAt) {
|
|
717
|
+
return [2 /*return*/, false];
|
|
718
|
+
}
|
|
719
|
+
// if there're other mutations(s), overlapped with the current revalidation:
|
|
720
|
+
// case 1:
|
|
721
|
+
// req------------------>res
|
|
722
|
+
// mutate------>end
|
|
723
|
+
// case 2:
|
|
724
|
+
// req------------>res
|
|
725
|
+
// mutate------>end
|
|
726
|
+
// case 3:
|
|
727
|
+
// req------------------>res
|
|
728
|
+
// mutate-------...---------->
|
|
729
|
+
// we have to ignore the revalidation result (res) because it's no longer fresh.
|
|
730
|
+
// meanwhile, a new revalidation should be triggered when the mutation ends.
|
|
731
|
+
if (MUTATION_TS[key] &&
|
|
732
|
+
// case 1
|
|
733
|
+
(startAt <= MUTATION_TS[key] ||
|
|
734
|
+
// case 2
|
|
735
|
+
startAt <= MUTATION_END_TS[key] ||
|
|
736
|
+
// case 3
|
|
737
|
+
MUTATION_END_TS[key] === 0)) {
|
|
738
|
+
dispatch({ isValidating: false });
|
|
739
|
+
return [2 /*return*/, false];
|
|
740
|
+
}
|
|
741
|
+
cache.set(keyErr, undefined);
|
|
742
|
+
cache.set(keyValidating, false);
|
|
743
|
+
newState = {
|
|
744
|
+
isValidating: false
|
|
745
|
+
};
|
|
746
|
+
if (typeof stateRef.current.error !== 'undefined') {
|
|
747
|
+
// we don't have an error
|
|
748
|
+
newState.error = undefined;
|
|
749
|
+
}
|
|
750
|
+
if (!config.compare(stateRef.current.data, newData)) {
|
|
751
|
+
// deep compare to avoid extra re-render
|
|
752
|
+
// data changed
|
|
753
|
+
newState.data = newData;
|
|
754
|
+
}
|
|
755
|
+
if (!config.compare(cache.get(key), newData)) {
|
|
756
|
+
cache.set(key, newData);
|
|
757
|
+
}
|
|
758
|
+
// merge the new state
|
|
759
|
+
dispatch(newState);
|
|
760
|
+
if (!shouldDeduping) {
|
|
761
|
+
// also update other hooks
|
|
762
|
+
broadcastState(key, newData, newState.error, false);
|
|
763
|
+
}
|
|
764
|
+
return [3 /*break*/, 7];
|
|
765
|
+
case 6:
|
|
766
|
+
err_2 = _c.sent();
|
|
767
|
+
delete CONCURRENT_PROMISES[key];
|
|
768
|
+
delete CONCURRENT_PROMISES_TS[key];
|
|
769
|
+
if (configRef.current.isPaused()) {
|
|
770
|
+
dispatch({
|
|
771
|
+
isValidating: false
|
|
772
|
+
});
|
|
773
|
+
return [2 /*return*/, false];
|
|
774
|
+
}
|
|
775
|
+
cache.set(keyErr, err_2);
|
|
776
|
+
// get a new error
|
|
777
|
+
// don't use deep equal for errors
|
|
778
|
+
if (stateRef.current.error !== err_2) {
|
|
779
|
+
// we keep the stale data
|
|
780
|
+
dispatch({
|
|
781
|
+
isValidating: false,
|
|
782
|
+
error: err_2
|
|
783
|
+
});
|
|
784
|
+
if (!shouldDeduping) {
|
|
785
|
+
// also broadcast to update other hooks
|
|
786
|
+
broadcastState(key, undefined, err_2, false);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
// events and retry
|
|
790
|
+
eventsCallback('onError', err_2, key, config);
|
|
791
|
+
if (config.shouldRetryOnError) {
|
|
792
|
+
// when retrying, we always enable deduping
|
|
793
|
+
eventsCallback('onErrorRetry', err_2, key, config, revalidate, {
|
|
794
|
+
retryCount: retryCount + 1,
|
|
795
|
+
dedupe: true
|
|
796
|
+
});
|
|
797
|
+
}
|
|
798
|
+
return [3 /*break*/, 7];
|
|
799
|
+
case 7:
|
|
800
|
+
loading = false;
|
|
801
|
+
return [2 /*return*/, true];
|
|
802
|
+
}
|
|
803
|
+
});
|
|
804
|
+
});
|
|
805
|
+
},
|
|
806
|
+
// dispatch is immutable, and `eventsCallback`, `fnArgs`, `keyErr`, and `keyValidating` are based on `key`,
|
|
807
|
+
// so we can them from the deps array.
|
|
808
|
+
//
|
|
809
|
+
// FIXME:
|
|
810
|
+
// `fn` and `config` might be changed during the lifecycle,
|
|
811
|
+
// but they might be changed every render like this.
|
|
812
|
+
// useSWR('key', () => fetch('/api/'), { suspense: true })
|
|
813
|
+
// So we omit the values from the deps array
|
|
814
|
+
// even though it might cause unexpected behaviors.
|
|
815
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
816
|
+
[key]);
|
|
817
|
+
// mounted (client side rendering)
|
|
818
|
+
useIsomorphicLayoutEffect(function () {
|
|
819
|
+
if (!key)
|
|
820
|
+
return undefined;
|
|
821
|
+
// after `key` updates, we need to mark it as mounted
|
|
822
|
+
unmountedRef.current = false;
|
|
823
|
+
var isUpdating = initialMountedRef.current;
|
|
824
|
+
initialMountedRef.current = true;
|
|
825
|
+
// after the component is mounted (hydrated),
|
|
826
|
+
// we need to update the data from the cache
|
|
827
|
+
// and trigger a revalidation
|
|
828
|
+
var currentHookData = stateRef.current.data;
|
|
829
|
+
var latestKeyedData = resolveData();
|
|
830
|
+
// update the state if the key changed (not the inital render) or cache updated
|
|
831
|
+
keyRef.current = key;
|
|
832
|
+
if (!config.compare(currentHookData, latestKeyedData)) {
|
|
833
|
+
dispatch({ data: latestKeyedData });
|
|
834
|
+
}
|
|
835
|
+
// revalidate with deduping
|
|
836
|
+
var softRevalidate = function () { return revalidate({ dedupe: true }); };
|
|
837
|
+
// trigger a revalidation
|
|
838
|
+
if (isUpdating || willRevalidateOnMount()) {
|
|
839
|
+
if (typeof latestKeyedData !== 'undefined' && !IS_SERVER) {
|
|
840
|
+
// delay revalidate if there's cache
|
|
841
|
+
// to not block the rendering
|
|
842
|
+
// @ts-ignore it's safe to use requestAnimationFrame in browser
|
|
843
|
+
rAF(softRevalidate);
|
|
844
|
+
}
|
|
845
|
+
else {
|
|
846
|
+
softRevalidate();
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
var pending = false;
|
|
850
|
+
var onFocus = function () {
|
|
851
|
+
if (pending || !configRef.current.revalidateOnFocus)
|
|
852
|
+
return;
|
|
853
|
+
pending = true;
|
|
854
|
+
softRevalidate();
|
|
855
|
+
setTimeout(function () { return (pending = false); }, configRef.current.focusThrottleInterval);
|
|
856
|
+
};
|
|
857
|
+
var onReconnect = function () {
|
|
858
|
+
if (configRef.current.revalidateOnReconnect) {
|
|
859
|
+
softRevalidate();
|
|
860
|
+
}
|
|
861
|
+
};
|
|
862
|
+
// register global cache update listener
|
|
863
|
+
var onUpdate = function (shouldRevalidate, updatedData, updatedError, updatedIsValidating, dedupe) {
|
|
864
|
+
if (shouldRevalidate === void 0) { shouldRevalidate = true; }
|
|
865
|
+
if (dedupe === void 0) { dedupe = true; }
|
|
866
|
+
// update hook state
|
|
867
|
+
var newState = {};
|
|
868
|
+
var needUpdate = false;
|
|
869
|
+
if (typeof updatedData !== 'undefined' &&
|
|
870
|
+
!config.compare(stateRef.current.data, updatedData)) {
|
|
871
|
+
newState.data = updatedData;
|
|
872
|
+
needUpdate = true;
|
|
873
|
+
}
|
|
874
|
+
// always update error
|
|
875
|
+
// because it can be `undefined`
|
|
876
|
+
if (stateRef.current.error !== updatedError) {
|
|
877
|
+
newState.error = updatedError;
|
|
878
|
+
needUpdate = true;
|
|
879
|
+
}
|
|
880
|
+
if (typeof updatedIsValidating !== 'undefined' &&
|
|
881
|
+
stateRef.current.isValidating !== updatedIsValidating) {
|
|
882
|
+
newState.isValidating = updatedIsValidating;
|
|
883
|
+
needUpdate = true;
|
|
884
|
+
}
|
|
885
|
+
if (needUpdate) {
|
|
886
|
+
dispatch(newState);
|
|
887
|
+
}
|
|
888
|
+
if (shouldRevalidate) {
|
|
889
|
+
if (dedupe) {
|
|
890
|
+
return softRevalidate();
|
|
891
|
+
}
|
|
892
|
+
else {
|
|
893
|
+
return revalidate();
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
return false;
|
|
897
|
+
};
|
|
898
|
+
var unsubFocus = addRevalidator(FOCUS_REVALIDATORS, onFocus);
|
|
899
|
+
var unsubReconnect = addRevalidator(RECONNECT_REVALIDATORS, onReconnect);
|
|
900
|
+
var unsubUpdate = addRevalidator(CACHE_REVALIDATORS, onUpdate);
|
|
901
|
+
return function () {
|
|
902
|
+
// cleanup
|
|
903
|
+
dispatch = function () { return null; };
|
|
904
|
+
// mark it as unmounted
|
|
905
|
+
unmountedRef.current = true;
|
|
906
|
+
unsubFocus();
|
|
907
|
+
unsubReconnect();
|
|
908
|
+
unsubUpdate();
|
|
909
|
+
};
|
|
910
|
+
}, [key, revalidate]);
|
|
911
|
+
useIsomorphicLayoutEffect(function () {
|
|
912
|
+
var timer = null;
|
|
913
|
+
var tick = function () { return __awaiter(_this, void 0, void 0, function () {
|
|
914
|
+
return __generator(this, function (_a) {
|
|
915
|
+
switch (_a.label) {
|
|
916
|
+
case 0:
|
|
917
|
+
if (!(!stateRef.current.error &&
|
|
918
|
+
(configRef.current.refreshWhenHidden ||
|
|
919
|
+
configRef.current.isDocumentVisible()) &&
|
|
920
|
+
(configRef.current.refreshWhenOffline || configRef.current.isOnline()))) return [3 /*break*/, 2];
|
|
921
|
+
// only revalidate when the page is visible
|
|
922
|
+
// if API request errored, we stop polling in this round
|
|
923
|
+
// and let the error retry function handle it
|
|
924
|
+
return [4 /*yield*/, revalidate({ dedupe: true })];
|
|
925
|
+
case 1:
|
|
926
|
+
// only revalidate when the page is visible
|
|
927
|
+
// if API request errored, we stop polling in this round
|
|
928
|
+
// and let the error retry function handle it
|
|
929
|
+
_a.sent();
|
|
930
|
+
_a.label = 2;
|
|
931
|
+
case 2:
|
|
932
|
+
// Read the latest refreshInterval
|
|
933
|
+
if (configRef.current.refreshInterval && timer) {
|
|
934
|
+
timer = setTimeout(tick, configRef.current.refreshInterval);
|
|
935
|
+
}
|
|
936
|
+
return [2 /*return*/];
|
|
937
|
+
}
|
|
938
|
+
});
|
|
939
|
+
}); };
|
|
940
|
+
if (configRef.current.refreshInterval) {
|
|
941
|
+
timer = setTimeout(tick, configRef.current.refreshInterval);
|
|
942
|
+
}
|
|
943
|
+
return function () {
|
|
944
|
+
if (timer) {
|
|
945
|
+
clearTimeout(timer);
|
|
946
|
+
timer = null;
|
|
947
|
+
}
|
|
948
|
+
};
|
|
949
|
+
}, [
|
|
950
|
+
config.refreshInterval,
|
|
951
|
+
config.refreshWhenHidden,
|
|
952
|
+
config.refreshWhenOffline,
|
|
953
|
+
revalidate
|
|
954
|
+
]);
|
|
955
|
+
// suspense
|
|
956
|
+
var latestData;
|
|
957
|
+
var latestError;
|
|
958
|
+
if (config.suspense) {
|
|
959
|
+
// in suspense mode, we can't return empty state
|
|
960
|
+
// (it should be suspended)
|
|
961
|
+
// try to get data and error from cache
|
|
962
|
+
latestData = cache.get(key);
|
|
963
|
+
latestError = cache.get(keyErr);
|
|
964
|
+
if (typeof latestData === 'undefined') {
|
|
965
|
+
latestData = initialData;
|
|
966
|
+
}
|
|
967
|
+
if (typeof latestError === 'undefined') {
|
|
968
|
+
latestError = initialError;
|
|
969
|
+
}
|
|
970
|
+
if (typeof latestData === 'undefined' &&
|
|
971
|
+
typeof latestError === 'undefined') {
|
|
972
|
+
// need to start the request if it hasn't
|
|
973
|
+
if (!CONCURRENT_PROMISES[key]) {
|
|
974
|
+
// trigger revalidate immediately
|
|
975
|
+
// to get the promise
|
|
976
|
+
// in this revalidate, should not rerender
|
|
977
|
+
revalidate();
|
|
978
|
+
}
|
|
979
|
+
if (CONCURRENT_PROMISES[key] &&
|
|
980
|
+
typeof CONCURRENT_PROMISES[key].then === 'function') {
|
|
981
|
+
// if it is a promise
|
|
982
|
+
throw CONCURRENT_PROMISES[key];
|
|
983
|
+
}
|
|
984
|
+
// it's a value, return it directly (override)
|
|
985
|
+
latestData = CONCURRENT_PROMISES[key];
|
|
986
|
+
}
|
|
987
|
+
if (typeof latestData === 'undefined' && latestError) {
|
|
988
|
+
// in suspense mode, throw error if there's no content
|
|
989
|
+
throw latestError;
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
// define returned state
|
|
993
|
+
// can be memorized since the state is a ref
|
|
994
|
+
var memoizedState = React.useMemo(function () {
|
|
995
|
+
// revalidate will be deprecated in the 1.x release
|
|
996
|
+
// because mutate() covers the same use case of revalidate().
|
|
997
|
+
// This remains only for backward compatibility
|
|
998
|
+
var state = { revalidate: revalidate, mutate: boundMutate };
|
|
999
|
+
Object.defineProperties(state, {
|
|
1000
|
+
error: {
|
|
1001
|
+
// `key` might be changed in the upcoming hook re-render,
|
|
1002
|
+
// but the previous state will stay
|
|
1003
|
+
// so we need to match the latest key and data (fallback to `initialData`)
|
|
1004
|
+
get: function () {
|
|
1005
|
+
stateDependencies.current.error = true;
|
|
1006
|
+
if (config.suspense) {
|
|
1007
|
+
return latestError;
|
|
1008
|
+
}
|
|
1009
|
+
return keyRef.current === key ? stateRef.current.error : initialError;
|
|
1010
|
+
},
|
|
1011
|
+
enumerable: true
|
|
1012
|
+
},
|
|
1013
|
+
data: {
|
|
1014
|
+
get: function () {
|
|
1015
|
+
stateDependencies.current.data = true;
|
|
1016
|
+
if (config.suspense) {
|
|
1017
|
+
return latestData;
|
|
1018
|
+
}
|
|
1019
|
+
return keyRef.current === key ? stateRef.current.data : initialData;
|
|
1020
|
+
},
|
|
1021
|
+
enumerable: true
|
|
1022
|
+
},
|
|
1023
|
+
isValidating: {
|
|
1024
|
+
get: function () {
|
|
1025
|
+
stateDependencies.current.isValidating = true;
|
|
1026
|
+
return key ? stateRef.current.isValidating : false;
|
|
1027
|
+
},
|
|
1028
|
+
enumerable: true
|
|
1029
|
+
}
|
|
1030
|
+
});
|
|
1031
|
+
return state;
|
|
1032
|
+
// `config.suspense` isn't allowed to change during the lifecycle.
|
|
1033
|
+
// `boundMutate` is immutable, and the immutability of `revalidate` depends on `key`
|
|
1034
|
+
// so we can omit them from the deps array,
|
|
1035
|
+
// but we put it to enable react-hooks/exhaustive-deps rule.
|
|
1036
|
+
// `initialData` and `initialError` are not initial values
|
|
1037
|
+
// because they are changed during the lifecycle
|
|
1038
|
+
// so we should add them in the deps array.
|
|
1039
|
+
}, [
|
|
1040
|
+
revalidate,
|
|
1041
|
+
initialData,
|
|
1042
|
+
initialError,
|
|
1043
|
+
boundMutate,
|
|
1044
|
+
key,
|
|
1045
|
+
config.suspense,
|
|
1046
|
+
latestError,
|
|
1047
|
+
latestData
|
|
1048
|
+
]);
|
|
1049
|
+
return memoizedState;
|
|
1050
|
+
}
|
|
1051
|
+
Object.defineProperty(SWRConfigContext.Provider, 'default', {
|
|
1052
|
+
value: defaultConfig
|
|
1053
|
+
});
|
|
1054
|
+
var SWRConfig = SWRConfigContext.Provider;
|
|
1055
|
+
|
|
1056
|
+
var __awaiter$1 = (undefined && undefined.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
1057
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
1058
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
1059
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
1060
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
1061
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
1062
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
1063
|
+
});
|
|
1064
|
+
};
|
|
1065
|
+
var __generator$1 = (undefined && undefined.__generator) || function (thisArg, body) {
|
|
1066
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
1067
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
1068
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
1069
|
+
function step(op) {
|
|
1070
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
1071
|
+
while (_) try {
|
|
1072
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
1073
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
1074
|
+
switch (op[0]) {
|
|
1075
|
+
case 0: case 1: t = op; break;
|
|
1076
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
1077
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
1078
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
1079
|
+
default:
|
|
1080
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
1081
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
1082
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
1083
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
1084
|
+
if (t[2]) _.ops.pop();
|
|
1085
|
+
_.trys.pop(); continue;
|
|
1086
|
+
}
|
|
1087
|
+
op = body.call(thisArg, _);
|
|
1088
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
1089
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1092
|
+
var __rest = (undefined && undefined.__rest) || function (s, e) {
|
|
1093
|
+
var t = {};
|
|
1094
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
1095
|
+
t[p] = s[p];
|
|
1096
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
1097
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
1098
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
1099
|
+
t[p[i]] = s[p[i]];
|
|
1100
|
+
}
|
|
1101
|
+
return t;
|
|
1102
|
+
};
|
|
1103
|
+
|
|
55
1104
|
var _this = undefined;
|
|
56
1105
|
|
|
57
1106
|
var renderCardImage = function renderCardImage(row, client) {
|
|
@@ -1378,64 +2427,56 @@ var QueueDeckExpanded = function QueueDeckExpanded(props) {
|
|
|
1378
2427
|
contentCategoryMapping = _props$contentCategor === undefined ? [] : _props$contentCategor,
|
|
1379
2428
|
showPublished = props.showPublished;
|
|
1380
2429
|
|
|
1381
|
-
var _useState3 = React.useState(
|
|
2430
|
+
var _useState3 = React.useState(initialCurrentPage || 1),
|
|
1382
2431
|
_useState4 = slicedToArray._slicedToArray(_useState3, 2),
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
var _useState5 = React.useState(initialCurrentPage || 1),
|
|
1387
|
-
_useState6 = slicedToArray._slicedToArray(_useState5, 2),
|
|
1388
|
-
currentPage = _useState6[0],
|
|
1389
|
-
setCurrentPage = _useState6[1];
|
|
1390
|
-
// const [lastDataSize, setLastDataSize] = useState(initialData ? initialData.length : 0)
|
|
1391
|
-
// const [values, setValues] = useState({
|
|
1392
|
-
// from: params ? params.from : 0,
|
|
1393
|
-
// to: params ? params.to : 0,
|
|
1394
|
-
// page: initialCurrentPage ? initialCurrentPage : 1
|
|
1395
|
-
// })
|
|
2432
|
+
currentPage = _useState4[0],
|
|
2433
|
+
setCurrentPage = _useState4[1];
|
|
1396
2434
|
|
|
1397
2435
|
var itemsPerPage = params && params.itemsPerPage ? params.itemsPerPage : params && params.from && params.to ? params.to - params.from : 10;
|
|
1398
2436
|
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
// setScrolling(true)
|
|
1413
|
-
// }
|
|
1414
|
-
// }
|
|
1415
|
-
// }, [initialData])
|
|
2437
|
+
var fetcher = function () {
|
|
2438
|
+
var _ref4 = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(currentPage) {
|
|
2439
|
+
var start;
|
|
2440
|
+
return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
|
|
2441
|
+
while (1) {
|
|
2442
|
+
switch (_context.prev = _context.next) {
|
|
2443
|
+
case 0:
|
|
2444
|
+
start = (currentPage - 1) * itemsPerPage;
|
|
2445
|
+
_context.next = 3;
|
|
2446
|
+
return client.fetch(query, _extends._extends({}, params, {
|
|
2447
|
+
from: start,
|
|
2448
|
+
to: start + itemsPerPage
|
|
2449
|
+
}));
|
|
1416
2450
|
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
// if (values.page !== prevValues.page && values.from !== prevValues.from && values.to !== prevValues.to) {
|
|
1420
|
-
// loadData(values, query, client, params, setData, setScrolling, setLastDataSize, pointer, pointerArray)
|
|
1421
|
-
// }
|
|
1422
|
-
// }
|
|
1423
|
-
// }, [values, prevValues, query, client, params, setData, setScrolling, setLastDataSize, pointer, pointerArray])
|
|
2451
|
+
case 3:
|
|
2452
|
+
return _context.abrupt('return', _context.sent);
|
|
1424
2453
|
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
2454
|
+
case 4:
|
|
2455
|
+
case 'end':
|
|
2456
|
+
return _context.stop();
|
|
2457
|
+
}
|
|
2458
|
+
}
|
|
2459
|
+
}, _callee, _this);
|
|
2460
|
+
}));
|
|
1431
2461
|
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
2462
|
+
return function fetcher(_x5) {
|
|
2463
|
+
return _ref4.apply(this, arguments);
|
|
2464
|
+
};
|
|
2465
|
+
}();
|
|
2466
|
+
|
|
2467
|
+
var onLoadDataSuccess = function onLoadDataSuccess() {
|
|
2468
|
+
document.body.scrollTop = 0;
|
|
2469
|
+
document.documentElement.scrollTop = 0;
|
|
2470
|
+
seoPaginate && seoPagination(currentPage);
|
|
2471
|
+
index.lib_3.refresh();
|
|
2472
|
+
};
|
|
2473
|
+
|
|
2474
|
+
var _useSWR = useSWR([currentPage], fetcher, { initialData: initialData, onSuccess: onLoadDataSuccess }),
|
|
2475
|
+
data = _useSWR.data;
|
|
2476
|
+
|
|
2477
|
+
var handlePageChange = function handlePageChange(selectedPage) {
|
|
2478
|
+
setCurrentPage(selectedPage);
|
|
2479
|
+
};
|
|
1439
2480
|
|
|
1440
2481
|
var seoPagination = function seoPagination(pageNumber) {
|
|
1441
2482
|
var path = router.asPath;
|
|
@@ -1456,69 +2497,13 @@ var QueueDeckExpanded = function QueueDeckExpanded(props) {
|
|
|
1456
2497
|
}
|
|
1457
2498
|
if (queryString.length > 0) path += '?' + queryString;
|
|
1458
2499
|
pageNumber = parseInt(pageNumber);
|
|
1459
|
-
if (
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
});
|
|
1465
|
-
}
|
|
2500
|
+
if (path[0] !== '/') path = '/' + path;
|
|
2501
|
+
var newPath = pageNumber === 1 ? '' + path : '' + path + (queryString.length > 0 ? '&' : '?') + 'page=' + pageNumber;
|
|
2502
|
+
router.push(pathname, newPath, {
|
|
2503
|
+
shallow: true
|
|
2504
|
+
});
|
|
1466
2505
|
};
|
|
1467
2506
|
|
|
1468
|
-
var handlePageChange = function () {
|
|
1469
|
-
var _ref4 = asyncToGenerator._asyncToGenerator( /*#__PURE__*/asyncToGenerator.regenerator.mark(function _callee(selectedPage) {
|
|
1470
|
-
var start, newParams, newData;
|
|
1471
|
-
return asyncToGenerator.regenerator.wrap(function _callee$(_context) {
|
|
1472
|
-
while (1) {
|
|
1473
|
-
switch (_context.prev = _context.next) {
|
|
1474
|
-
case 0:
|
|
1475
|
-
if (!(selectedPage && selectedPage != currentPage)) {
|
|
1476
|
-
_context.next = 13;
|
|
1477
|
-
break;
|
|
1478
|
-
}
|
|
1479
|
-
|
|
1480
|
-
start = (selectedPage - 1) * itemsPerPage;
|
|
1481
|
-
newParams = _extends._extends({}, params, {
|
|
1482
|
-
from: start,
|
|
1483
|
-
to: start + itemsPerPage
|
|
1484
|
-
});
|
|
1485
|
-
_context.prev = 3;
|
|
1486
|
-
_context.next = 6;
|
|
1487
|
-
return client.fetch(query, newParams);
|
|
1488
|
-
|
|
1489
|
-
case 6:
|
|
1490
|
-
newData = _context.sent;
|
|
1491
|
-
|
|
1492
|
-
if (newData && newData.length > 0) {
|
|
1493
|
-
setData(newData);
|
|
1494
|
-
document.body.scrollTop = 0;
|
|
1495
|
-
document.documentElement.scrollTop = 0;
|
|
1496
|
-
seoPaginate && seoPagination(selectedPage);
|
|
1497
|
-
setCurrentPage(selectedPage);
|
|
1498
|
-
index.lib_3.refresh();
|
|
1499
|
-
}
|
|
1500
|
-
_context.next = 13;
|
|
1501
|
-
break;
|
|
1502
|
-
|
|
1503
|
-
case 10:
|
|
1504
|
-
_context.prev = 10;
|
|
1505
|
-
_context.t0 = _context['catch'](3);
|
|
1506
|
-
|
|
1507
|
-
console.error(_context.t0);
|
|
1508
|
-
|
|
1509
|
-
case 13:
|
|
1510
|
-
case 'end':
|
|
1511
|
-
return _context.stop();
|
|
1512
|
-
}
|
|
1513
|
-
}
|
|
1514
|
-
}, _callee, _this, [[3, 10]]);
|
|
1515
|
-
}));
|
|
1516
|
-
|
|
1517
|
-
return function handlePageChange(_x5) {
|
|
1518
|
-
return _ref4.apply(this, arguments);
|
|
1519
|
-
};
|
|
1520
|
-
}();
|
|
1521
|
-
|
|
1522
2507
|
return React__default.createElement(
|
|
1523
2508
|
'div',
|
|
1524
2509
|
null,
|