@lowentry/react-redux 1.16.1 → 2.0.1

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/index.js DELETED
@@ -1,1497 +0,0 @@
1
- import _extends from '@babel/runtime/helpers/extends';
2
- import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
3
- import _slicedToArray from '@babel/runtime/helpers/slicedToArray';
4
- import _asyncToGenerator from '@babel/runtime/helpers/asyncToGenerator';
5
- import _toConsumableArray from '@babel/runtime/helpers/toConsumableArray';
6
- import _typeof from '@babel/runtime/helpers/typeof';
7
- import _defineProperty from '@babel/runtime/helpers/defineProperty';
8
- import _regeneratorRuntime from '@babel/runtime/regenerator';
9
- import * as RTK from '@reduxjs/toolkit';
10
- import * as React from 'react';
11
- import * as ReactDOM from 'react-dom';
12
- import * as ReactRedux from 'react-redux';
13
- import * as ReduxSaga from 'redux-saga';
14
- import * as ReduxSagaEffects from 'redux-saga/effects';
15
- import { LeUtils, ISSET, ARRAY, STRING, INT_LAX_ANY, IS_OBJECT, IS_ARRAY } from '@lowentry/utils';
16
-
17
- var _excluded = ["store", "children"],
18
- _excluded2 = ["loading", "load"];
19
- function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
20
- function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
21
- function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
22
- function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
23
- function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
24
- var LeRed = function () {
25
- /** @type {Object} */
26
- var LeRed = {};
27
- try {
28
- var set = function set(key, value) {
29
- var ignoreOverrides = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
30
- var keyFirstChar = key.charAt(0);
31
- if (keyFirstChar === keyFirstChar.toLowerCase() && keyFirstChar !== keyFirstChar.toUpperCase()) {
32
- if (key === 'default' || key === 'version') {
33
- return;
34
- }
35
- if (key === 'set' || key === 'setAll') {
36
- console.error('tried to override LeRed["' + key + '"], which isn\'t allowed, to:');
37
- console.error(value);
38
- return;
39
- }
40
- if (ignoreOverrides !== true && key in LeRed) {
41
- console.warn('LeRed["' + key + '"] was overwritten, from:');
42
- console.warn(LeRed[key]);
43
- console.warn('to:');
44
- console.warn(value);
45
- }
46
- LeRed[key] = value;
47
- }
48
- };
49
- var setAll = function setAll(obj) {
50
- var ignoreOverrides = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
51
- var optionalSkipHasOwnPropertyCheck = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
52
- LeUtils.each(obj, function (value, key) {
53
- set(key, value, ignoreOverrides);
54
- }, optionalSkipHasOwnPropertyCheck);
55
- };
56
- LeRed.set = function (key, value) {
57
- return set(key, value, true);
58
- };
59
- LeRed.setAll = function (obj) {
60
- var optionalSkipHasOwnPropertyCheck = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
61
- return setAll(obj, true, optionalSkipHasOwnPropertyCheck);
62
- };
63
- setAll(ReactDOM);
64
- setAll(ReduxSaga);
65
- setAll({
66
- effects: _objectSpread({}, ReduxSagaEffects)
67
- });
68
- setAll(RTK);
69
- setAll(React);
70
- setAll(ReactRedux);
71
- LeRed.effects.delayFrames = function () {
72
- var frames = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
73
- return /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
74
- return _regeneratorRuntime.wrap(function (_context) {
75
- while (1) switch (_context.prev = _context.next) {
76
- case 0:
77
- _context.next = 1;
78
- return LeRed.effects.call(function () {
79
- return new Promise(function (resolve, reject) {
80
- try {
81
- LeUtils.setAnimationFrameTimeout(resolve, frames);
82
- } catch (e) {
83
- reject(e);
84
- }
85
- });
86
- });
87
- case 1:
88
- case "end":
89
- return _context.stop();
90
- }
91
- }, _callee);
92
- })();
93
- };
94
- LeRed.effects.interval = /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(callback, intervalMs) {
95
- var channel, stop, _t, _t2;
96
- return _regeneratorRuntime.wrap(function (_context2) {
97
- while (1) switch (_context2.prev = _context2.next) {
98
- case 0:
99
- channel = LeRed.eventChannel(function (emitter) {
100
- var interval = setInterval(function () {
101
- emitter({});
102
- }, intervalMs);
103
- return function () {
104
- clearInterval(interval);
105
- };
106
- });
107
- stop = function stop() {
108
- try {
109
- if (channel !== null) {
110
- channel.close();
111
- channel = null;
112
- }
113
- } catch (e) {
114
- console.error(e);
115
- }
116
- };
117
- case 1:
118
- if (!(channel !== null)) {
119
- _context2.next = 13;
120
- break;
121
- }
122
- _context2.prev = 2;
123
- _context2.next = 3;
124
- return LeRed.effects.take(channel);
125
- case 3:
126
- _context2.next = 4;
127
- return callback(stop);
128
- case 4:
129
- _context2.next = 6;
130
- break;
131
- case 5:
132
- _context2.prev = 5;
133
- _t = _context2["catch"](2);
134
- console.error(_t);
135
- case 6:
136
- _context2.prev = 6;
137
- _context2.prev = 7;
138
- _context2.next = 8;
139
- return LeRed.effects.cancelled();
140
- case 8:
141
- if (!_context2.sent) {
142
- _context2.next = 9;
143
- break;
144
- }
145
- channel.close();
146
- channel = null;
147
- case 9:
148
- _context2.next = 11;
149
- break;
150
- case 10:
151
- _context2.prev = 10;
152
- _t2 = _context2["catch"](7);
153
- console.error(_t2);
154
- case 11:
155
- return _context2.finish(6);
156
- case 12:
157
- _context2.next = 1;
158
- break;
159
- case 13:
160
- case "end":
161
- return _context2.stop();
162
- }
163
- }, _callee2, null, [[2, 5, 6, 12], [7, 10]]);
164
- });
165
- } catch (e) {
166
- console.error(e);
167
- }
168
- var fixEqualsComparator = function fixEqualsComparator(equalsComparator, errorMessage) {
169
- if (ISSET(equalsComparator)) {
170
- if (typeof equalsComparator !== 'function') {
171
- console.error(errorMessage);
172
- console.error(equalsComparator);
173
- return LeUtils.equals;
174
- }
175
- return equalsComparator;
176
- }
177
- return LeUtils.equals;
178
- };
179
- var useCompareMemoize = function useCompareMemoize(value, equalsComparator) {
180
- var ref = React.useRef();
181
- if (!equalsComparator(value, ref.current)) {
182
- ref.current = value;
183
- }
184
- return ref.current;
185
- };
186
- LeRed.configureStore = function (storeData) {
187
- if (storeData.__lowentry_store__ === true) {
188
- return storeData;
189
- }
190
- if (ISSET(storeData.slices)) {
191
- storeData.reducer = storeData.slices;
192
- delete storeData.slices;
193
- }
194
- var sagaListeners = [];
195
- if (ISSET(storeData.reducer)) {
196
- var slices = storeData.reducer;
197
- if (_typeof(slices) === 'object') {
198
- if (slices.name || slices.__lowentry_unfinished_slice) {
199
- slices = [slices];
200
- } else {
201
- var slicesArray = [];
202
- LeUtils.each(slices, function (slice, index) {
203
- if (!slice.name) {
204
- slice.name = index;
205
- }
206
- slicesArray.push(slice);
207
- });
208
- slices = slicesArray;
209
- }
210
- }
211
- slices = ARRAY(slices);
212
- var initialState = {};
213
- var reducerArrays = {};
214
- LeUtils.each(slices, function (slice, index) {
215
- if (!slice.name) {
216
- slice.name = 'slice_' + index;
217
- }
218
- if (slice.__lowentry_unfinished_slice) {
219
- delete slice.__lowentry_unfinished_slice;
220
- slice = LeRed.createSlice(slice);
221
- }
222
- initialState[slice.name] = typeof slice.state === 'function' ? slice.state() : slice.state;
223
- LeUtils.each(slice.reducers, function (reducer, reducerName) {
224
- var fullReducerName = reducerName.startsWith('lowentrystore/') ? reducerName.substring('lowentrystore/'.length) : slice.name + '/' + reducerName;
225
- if (typeof reducerArrays[fullReducerName] === 'undefined') {
226
- reducerArrays[fullReducerName] = [];
227
- }
228
- reducerArrays[fullReducerName].push(reducer);
229
- });
230
- LeUtils.each(slice.sagaListeners, function (sagaListener, reducerName) {
231
- LeUtils.each(LeUtils.flattenArray(sagaListener), function (listener) {
232
- try {
233
- sagaListeners.push(listener());
234
- } catch (e) {
235
- console.error('an error was thrown by your saga code, in slice "' + slice.name + '", action "' + reducerName + '":');
236
- console.error(e);
237
- }
238
- });
239
- });
240
- });
241
-
242
- /** @type {Object|*} */
243
- var reducers = {};
244
- LeUtils.each(reducerArrays, function (reducerArray, reducerName) {
245
- reducerArray = LeUtils.flattenArray(reducerArray);
246
- if (reducerArray.length <= 0) {
247
- reducers[reducerName] = reducerArray[0];
248
- } else {
249
- reducers[reducerName] = function () {
250
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
251
- args[_key] = arguments[_key];
252
- }
253
- LeUtils.each(reducerArray, function (reducer) {
254
- try {
255
- reducer.apply(void 0, args);
256
- } catch (e) {
257
- console.error(e);
258
- }
259
- });
260
- };
261
- }
262
- });
263
- storeData.reducer = RTK.createSlice({
264
- name: 'lowentrystore',
265
- initialState: initialState,
266
- reducers: reducers
267
- }).reducer;
268
- }
269
- if (ISSET(storeData.state)) {
270
- storeData.preloadedState = storeData.state;
271
- delete storeData.state;
272
- }
273
- if (ISSET(storeData.preloadedState)) {
274
- storeData.preloadedState = {
275
- reducer: storeData.preloadedState
276
- };
277
- }
278
- var middleware = ARRAY(storeData.middleware);
279
- var sagaMiddleware = null;
280
- if (sagaListeners.length > 0) {
281
- sagaMiddleware = ReduxSaga["default"]();
282
- middleware.push(sagaMiddleware);
283
- }
284
- storeData.middleware = function (getDefaultMiddleware) {
285
- var _getDefaultMiddleware;
286
- return (_getDefaultMiddleware = getDefaultMiddleware()).concat.apply(_getDefaultMiddleware, _toConsumableArray(middleware));
287
- };
288
-
289
- /** @type {RTK.EnhancedStore|*} */
290
- var store = RTK.configureStore(storeData);
291
- store.__lowentry_store__ = true;
292
- store.state = function () {
293
- return store.getState().reducer;
294
- };
295
- var dispatch = store.dispatch;
296
- // noinspection JSValidateTypes
297
- store.dispatch = function (action) {
298
- action.__lowentry_dispatch__ = true;
299
- if (STRING(action.type).startsWith('lowentrystore/lowentryaction/')) {
300
- action.__lowentry_dispatch_result__ = [];
301
- } else {
302
- delete action.__lowentry_dispatch_result__;
303
- }
304
- dispatch(action);
305
- var result = action.__lowentry_dispatch_result__;
306
- delete action.__lowentry_dispatch_result__;
307
- delete action.__lowentry_dispatch__;
308
- return result;
309
- };
310
- if (sagaMiddleware !== null) {
311
- sagaMiddleware.run(/*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
312
- return _regeneratorRuntime.wrap(function (_context3) {
313
- while (1) switch (_context3.prev = _context3.next) {
314
- case 0:
315
- _context3.next = 1;
316
- return ReduxSagaEffects.all(sagaListeners);
317
- case 1:
318
- case "end":
319
- return _context3.stop();
320
- }
321
- }, _callee3);
322
- }));
323
- }
324
- return store;
325
- };
326
- LeRed.createAction = function (id) {
327
- return RTK.createAction('lowentrystore/lowentryaction/' + id);
328
- };
329
- LeRed.createSelector = function (selectorsGenerator) {
330
- return function (stateOfSlice) {
331
- var state = this;
332
- var selectors = selectorsGenerator.apply(state, [stateOfSlice]);
333
- var selectorArgs = [];
334
- for (var i = 0; i < selectors.length - 1; i++) {
335
- var selectorsEntry = selectors[i];
336
- if (typeof selectorsEntry === 'function') {
337
- selectorsEntry = selectorsEntry.apply(state, [state]);
338
- }
339
- selectorArgs.push(selectorsEntry);
340
- }
341
- var finalSelector = selectors[selectors.length - 1];
342
- if (typeof finalSelector === 'function') {
343
- finalSelector = finalSelector.apply(state, selectorArgs);
344
- }
345
- return finalSelector;
346
- };
347
- };
348
- LeRed.createCachedSelector = function (selectorsGenerator, equalsComparator) {
349
- equalsComparator = fixEqualsComparator(equalsComparator, 'LeRed.createCachedSelector() was given an invalid comparator:');
350
- if (equalsComparator === false) {
351
- return;
352
- }
353
- var previousSelectorArgs = null;
354
- var previousFinalSelectorResult = null;
355
- return function (stateOfSlice) {
356
- var state = this;
357
- var selectors = selectorsGenerator.apply(state, [stateOfSlice]);
358
- var selectorArgs = [];
359
- for (var i = 0; i < selectors.length - 1; i++) {
360
- var selectorsEntry = selectors[i];
361
- if (typeof selectorsEntry === 'function') {
362
- selectorsEntry = selectorsEntry.apply(state, [state]);
363
- }
364
- selectorArgs.push(selectorsEntry);
365
- }
366
- var finalSelector = selectors[selectors.length - 1];
367
- if (typeof finalSelector === 'function') {
368
- if (equalsComparator(previousSelectorArgs, selectorArgs)) {
369
- finalSelector = previousFinalSelectorResult;
370
- } else {
371
- finalSelector = finalSelector.apply(state, selectorArgs);
372
- previousSelectorArgs = selectorArgs;
373
- previousFinalSelectorResult = finalSelector;
374
- }
375
- }
376
- return finalSelector;
377
- };
378
- };
379
- LeRed.createSlice = function (slice) {
380
- if (Array.isArray(slice)) {
381
- var e = new Error('the given slice is an array (instead of an object)');
382
- console.error('an error was thrown by your LeRed.createSlice(...) code:');
383
- console.error(e);
384
- throw e;
385
- }
386
- if (slice.name) {
387
- var actions = {};
388
- var reducers = {};
389
- var sagas = {};
390
- var sagaListeners = {};
391
- LeUtils.each(slice.actions, function (reducer, reducerNames) {
392
- LeUtils.each(reducerNames.split(','), function (reducerName) {
393
- if (reducerName.length <= 0) {
394
- return;
395
- }
396
- var reducerAction = RTK.createAction((reducerName.startsWith('lowentrystore/') ? '' : 'lowentrystore/' + slice.name + '/') + reducerName);
397
- actions[reducerName] = reducerAction;
398
- LeUtils.each(LeUtils.flattenArray(reducer), function (reducer) {
399
- if (LeUtils.isGeneratorFunction(reducer)) {
400
- var sagaListener = /*#__PURE__*/_regeneratorRuntime.mark(function sagaListener() {
401
- return _regeneratorRuntime.wrap(function (_context5) {
402
- while (1) switch (_context5.prev = _context5.next) {
403
- case 0:
404
- _context5.next = 1;
405
- return ReduxSagaEffects.takeEvery(reducerAction, /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(/** @type {RTK.Action|*} */action) {
406
- var promiseResolve, promiseReject, promise, result, _t3;
407
- return _regeneratorRuntime.wrap(function (_context4) {
408
- while (1) switch (_context4.prev = _context4.next) {
409
- case 0:
410
- /** @type {((value:*)=>void)|null|*} */
411
- promiseResolve = null;
412
- /** @type {((reason:*)=>void)|null|*} */
413
- promiseReject = null;
414
- _context4.prev = 1;
415
- if (action.__lowentry_dispatch__ === true) {
416
- promise = new Promise(function (resolve, reject) {
417
- promiseResolve = resolve;
418
- promiseReject = reject;
419
- });
420
- if (Array.isArray(action.__lowentry_dispatch_result__)) {
421
- if (typeof promise !== 'undefined') {
422
- action.__lowentry_dispatch_result__.push(promise);
423
- }
424
- } else {
425
- action.__lowentry_dispatch_result__ = promise;
426
- }
427
- }
428
- _context4.next = 2;
429
- return reducer.apply(slice, [action.payload]);
430
- case 2:
431
- result = _context4.sent;
432
- if (promiseResolve !== null) {
433
- promiseResolve(result);
434
- }
435
- _context4.next = 4;
436
- break;
437
- case 3:
438
- _context4.prev = 3;
439
- _t3 = _context4["catch"](1);
440
- console.error('an error was thrown by your LeRed.createSlice(...) code, by slice "' + slice.name + '", action "' + reducerName + '":');
441
- console.error(_t3);
442
- if (promiseReject !== null) {
443
- try {
444
- promiseReject(_t3);
445
- } catch (e2) {
446
- console.error(e2);
447
- }
448
- }
449
- case 4:
450
- case "end":
451
- return _context4.stop();
452
- }
453
- }, _callee4, null, [[1, 3]]);
454
- }));
455
- case 1:
456
- case "end":
457
- return _context5.stop();
458
- }
459
- }, sagaListener);
460
- });
461
- if (ISSET(sagas[reducerName])) {
462
- sagas[reducerName].push(reducer);
463
- } else {
464
- sagas[reducerName] = [reducer];
465
- }
466
- if (ISSET(sagaListeners[reducerName])) {
467
- sagaListeners[reducerName].push(sagaListener);
468
- } else {
469
- sagaListeners[reducerName] = [sagaListener];
470
- }
471
- } else {
472
- var reducerFunction = function reducerFunction(state, action) {
473
- try {
474
- var result = reducer.apply(state, [state[slice.name], action.payload]);
475
- if (action.__lowentry_dispatch__ === true) {
476
- if (Array.isArray(action.__lowentry_dispatch_result__)) {
477
- if (typeof result !== 'undefined') {
478
- action.__lowentry_dispatch_result__.push(result);
479
- }
480
- } else {
481
- action.__lowentry_dispatch_result__ = result;
482
- }
483
- }
484
- } catch (e) {
485
- console.error('an error was thrown by your LeRed.createSlice(...) code, by slice "' + slice.name + '", action "' + reducerName + '":');
486
- console.error(e);
487
- }
488
- };
489
- if (ISSET(reducers[reducerName])) {
490
- reducers[reducerName].push(reducerFunction);
491
- } else {
492
- reducers[reducerName] = [reducerFunction];
493
- }
494
- }
495
- });
496
- });
497
- });
498
- slice.actions = actions;
499
- slice.reducers = reducers;
500
- slice.sagas = sagas;
501
- slice.sagaListeners = sagaListeners;
502
- var selectors = {};
503
- LeUtils.each(slice.selectors, function (selector, selectorName) {
504
- selectors[selectorName] = function (state) {
505
- try {
506
- return selector.apply(state, [state[slice.name]]);
507
- } catch (e) {
508
- console.error('an error was thrown by your LeRed.createSlice(...) code, by slice "' + slice.name + '", selector "' + selectorName + '":');
509
- console.error(e);
510
- throw e;
511
- }
512
- };
513
- });
514
- slice.selectors = selectors;
515
- var getters = {};
516
- LeUtils.each(slice.getters, function (getter, getterName) {
517
- getters[getterName] = function () {
518
- try {
519
- var selector = getter.apply(void 0, arguments);
520
- return function (state) {
521
- try {
522
- return selector.apply(state, [state[slice.name]]);
523
- } catch (e) {
524
- console.error('an error was thrown by your LeRed.createSlice(...) code, by slice "' + slice.name + '", getter "' + getterName + '":');
525
- console.error(e);
526
- throw e;
527
- }
528
- };
529
- } catch (e) {
530
- console.error('an error was thrown by your LeRed.createSlice(...) code, by slice "' + slice.name + '", getter "' + getterName + '":');
531
- console.error(e);
532
- throw e;
533
- }
534
- };
535
- });
536
- slice.getters = getters;
537
- return slice;
538
- } else {
539
- slice.__lowentry_unfinished_slice = true;
540
- return slice;
541
- }
542
- };
543
- LeRed.createFastSlice = function (slice) {
544
- if (Array.isArray(slice)) {
545
- var e = new Error('the given slice is an array (instead of an object)');
546
- console.error('an error was thrown by your LeRed.createFastSlice(...) code:');
547
- console.error(e);
548
- throw e;
549
- }
550
- var actions = {};
551
- LeUtils.each(slice.actions, function (reducer, reducerName) {
552
- actions[reducerName] = function () {
553
- for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
554
- params[_key2] = arguments[_key2];
555
- }
556
- return reducer.apply(slice, [slice.state].concat(params));
557
- };
558
- });
559
- slice.actions = actions;
560
- var selectors = {};
561
- LeUtils.each(slice.selectors, function (selector, selectorName) {
562
- selectors[selectorName] = function () {
563
- return selector.apply(slice, [slice.state]);
564
- };
565
- });
566
- slice.selectors = new Proxy(selectors, {
567
- get: function get(target, key) {
568
- return key in target ? target[key]() : undefined;
569
- }
570
- });
571
- var getters = {};
572
- LeUtils.each(slice.getters, function (selector, selectorName) {
573
- getters[selectorName] = function () {
574
- for (var _len3 = arguments.length, params = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
575
- params[_key3] = arguments[_key3];
576
- }
577
- return selector.apply(slice, [slice.state].concat(params));
578
- };
579
- });
580
- slice.getters = getters;
581
- return slice;
582
- };
583
- LeRed.current = function (obj) {
584
- try {
585
- return RTK.current(obj);
586
- } catch (e) {}
587
- return obj;
588
- };
589
- LeRed.useConfigureStore = function (storeData) {
590
- return LeRed.useMemo(function () {
591
- return LeRed.configureStore(storeData);
592
- }, [storeData]);
593
- };
594
- LeRed.useSelector = function (selector, equalsComparator) {
595
- if (typeof selector !== 'function') {
596
- console.error('LeRed.useSelector() was given an invalid selector:');
597
- console.error(selector);
598
- selector = function selector() {};
599
- }
600
- equalsComparator = fixEqualsComparator(equalsComparator, 'LeRed.useSelector() was given an invalid comparator:');
601
- return ReactRedux.useSelector(selector, equalsComparator);
602
- };
603
- LeRed.useLayoutEffect = function (callable, comparingValues, equalsComparator) {
604
- equalsComparator = fixEqualsComparator(equalsComparator, 'LeRed.useLayoutEffect() was given an invalid comparator:');
605
- comparingValues = ARRAY(comparingValues);
606
- // eslint-disable-next-line react-hooks/rules-of-hooks
607
- comparingValues = comparingValues.map(function (value) {
608
- return useCompareMemoize(value, equalsComparator);
609
- });
610
- // eslint-disable-next-line react-hooks/exhaustive-deps
611
- return React.useLayoutEffect(callable, comparingValues);
612
- };
613
- LeRed.useEffect = function (callable, comparingValues, equalsComparator) {
614
- equalsComparator = fixEqualsComparator(equalsComparator, 'LeRed.useEffect() was given an invalid comparator:');
615
- comparingValues = ARRAY(comparingValues);
616
- // eslint-disable-next-line react-hooks/rules-of-hooks
617
- comparingValues = comparingValues.map(function (value) {
618
- return useCompareMemoize(value, equalsComparator);
619
- });
620
- // eslint-disable-next-line react-hooks/exhaustive-deps
621
- return React.useEffect(callable, comparingValues);
622
- };
623
- LeRed.useEffectInterval = function (callable, comparingValues, intervalMs, fireImmediately, equalsComparator) {
624
- return LeRed.useEffect(function () {
625
- return LeUtils.setInterval(callable, intervalMs, fireImmediately).remove;
626
- }, [comparingValues, equalsComparator]);
627
- };
628
- LeRed.useEffectAnimationFrameInterval = function (callable, comparingValues, intervalFrames, fireImmediately, equalsComparator) {
629
- return LeRed.useEffect(function () {
630
- return LeUtils.setAnimationFrameInterval(callable, intervalFrames, fireImmediately).remove;
631
- }, [comparingValues, equalsComparator]);
632
- };
633
- LeRed.useEffectGenerator = function (callable, comparingValues, intervalMs, fireImmediately, equalsComparator) {
634
- return LeRed.useEffect(function () {
635
- var stop = false;
636
- _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
637
- var _iterator, _step, promise, _t4;
638
- return _regeneratorRuntime.wrap(function (_context6) {
639
- while (1) switch (_context6.prev = _context6.next) {
640
- case 0:
641
- _iterator = _createForOfIteratorHelper(callable());
642
- _context6.prev = 1;
643
- _iterator.s();
644
- case 2:
645
- if ((_step = _iterator.n()).done) {
646
- _context6.next = 6;
647
- break;
648
- }
649
- promise = _step.value;
650
- if (!stop) {
651
- _context6.next = 3;
652
- break;
653
- }
654
- return _context6.abrupt("return");
655
- case 3:
656
- _context6.next = 4;
657
- return promise;
658
- case 4:
659
- if (!stop) {
660
- _context6.next = 5;
661
- break;
662
- }
663
- return _context6.abrupt("return");
664
- case 5:
665
- _context6.next = 2;
666
- break;
667
- case 6:
668
- _context6.next = 8;
669
- break;
670
- case 7:
671
- _context6.prev = 7;
672
- _t4 = _context6["catch"](1);
673
- _iterator.e(_t4);
674
- case 8:
675
- _context6.prev = 8;
676
- _iterator.f();
677
- return _context6.finish(8);
678
- case 9:
679
- case "end":
680
- return _context6.stop();
681
- }
682
- }, _callee5, null, [[1, 7, 8, 9]]);
683
- }))();
684
- return function () {
685
- stop = true;
686
- };
687
- }, [comparingValues, equalsComparator]);
688
- };
689
- LeRed.useEffectGeneratorLoop = function (callable, comparingValues, intervalMs, fireImmediately, equalsComparator) {
690
- return LeRed.useEffect(function () {
691
- var stop = false;
692
- _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
693
- var _iterator2, _step2, promise, _t5;
694
- return _regeneratorRuntime.wrap(function (_context7) {
695
- while (1) switch (_context7.prev = _context7.next) {
696
- case 0:
697
- if (stop) {
698
- _context7.next = 10;
699
- break;
700
- }
701
- _iterator2 = _createForOfIteratorHelper(callable());
702
- _context7.prev = 1;
703
- _iterator2.s();
704
- case 2:
705
- if ((_step2 = _iterator2.n()).done) {
706
- _context7.next = 6;
707
- break;
708
- }
709
- promise = _step2.value;
710
- if (!stop) {
711
- _context7.next = 3;
712
- break;
713
- }
714
- return _context7.abrupt("return");
715
- case 3:
716
- _context7.next = 4;
717
- return promise;
718
- case 4:
719
- if (!stop) {
720
- _context7.next = 5;
721
- break;
722
- }
723
- return _context7.abrupt("return");
724
- case 5:
725
- _context7.next = 2;
726
- break;
727
- case 6:
728
- _context7.next = 8;
729
- break;
730
- case 7:
731
- _context7.prev = 7;
732
- _t5 = _context7["catch"](1);
733
- _iterator2.e(_t5);
734
- case 8:
735
- _context7.prev = 8;
736
- _iterator2.f();
737
- return _context7.finish(8);
738
- case 9:
739
- _context7.next = 0;
740
- break;
741
- case 10:
742
- case "end":
743
- return _context7.stop();
744
- }
745
- }, _callee6, null, [[1, 7, 8, 9]]);
746
- }))();
747
- return function () {
748
- stop = true;
749
- };
750
- }, [comparingValues, equalsComparator]);
751
- };
752
- LeRed.useEffectShutdown = function (callable, comparingValues, equalsComparator) {
753
- return LeRed.useEffect(function () {
754
- var _globalThis$addEventL;
755
- var run = function run() {
756
- callable();
757
- };
758
- globalThis === null || globalThis === void 0 || (_globalThis$addEventL = globalThis.addEventListener) === null || _globalThis$addEventL === void 0 || _globalThis$addEventL.call(globalThis, 'beforeunload', run, {
759
- capture: true
760
- });
761
- return function () {
762
- var _globalThis$removeEve;
763
- globalThis === null || globalThis === void 0 || (_globalThis$removeEve = globalThis.removeEventListener) === null || _globalThis$removeEve === void 0 || _globalThis$removeEve.call(globalThis, 'beforeunload', run, {
764
- capture: true
765
- });
766
- run();
767
- };
768
- }, [comparingValues, equalsComparator]);
769
- };
770
- LeRed.useEffectPageFocusLost = function (callable, comparingValues, equalsComparator) {
771
- var events = ['pagehide', 'freeze', 'blur', 'visibilitychange'];
772
- return LeRed.useEffect(function () {
773
- var run = function run() {
774
- var _globalThis$document, _globalThis$document2, _globalThis$document3;
775
- if ((globalThis === null || globalThis === void 0 || (_globalThis$document = globalThis.document) === null || _globalThis$document === void 0 ? void 0 : _globalThis$document.visibilityState) !== 'hidden' && globalThis !== null && globalThis !== void 0 && (_globalThis$document2 = globalThis.document) !== null && _globalThis$document2 !== void 0 && (_globalThis$document3 = _globalThis$document2.hasFocus) !== null && _globalThis$document3 !== void 0 && _globalThis$document3.call(_globalThis$document2)) {
776
- return;
777
- }
778
- callable();
779
- };
780
- events.forEach(function (type) {
781
- var _globalThis$addEventL2;
782
- globalThis === null || globalThis === void 0 || (_globalThis$addEventL2 = globalThis.addEventListener) === null || _globalThis$addEventL2 === void 0 || _globalThis$addEventL2.call(globalThis, type, run, {
783
- capture: true
784
- });
785
- });
786
- return function () {
787
- events.forEach(function (type) {
788
- var _globalThis$removeEve2;
789
- globalThis === null || globalThis === void 0 || (_globalThis$removeEve2 = globalThis.removeEventListener) === null || _globalThis$removeEve2 === void 0 || _globalThis$removeEve2.call(globalThis, type, run, {
790
- capture: true
791
- });
792
- });
793
- };
794
- }, [comparingValues, equalsComparator]);
795
- };
796
- LeRed.memo = function (component, equalsComparator) {
797
- equalsComparator = fixEqualsComparator(equalsComparator, 'LeRed.memo() was given an invalid comparator:');
798
- return /*#__PURE__*/React.memo(component, equalsComparator);
799
- };
800
- LeRed.useMemo = function (callable, comparingValues, equalsComparator) {
801
- equalsComparator = fixEqualsComparator(equalsComparator, 'LeRed.useMemo() was given an invalid comparator:');
802
- comparingValues = ARRAY(comparingValues);
803
- // eslint-disable-next-line react-hooks/rules-of-hooks
804
- comparingValues = comparingValues.map(function (value) {
805
- return useCompareMemoize(value, equalsComparator);
806
- });
807
- // eslint-disable-next-line react-hooks/exhaustive-deps
808
- return React.useMemo(callable, comparingValues);
809
- };
810
- LeRed.useCallback = function (callable, comparingValues, equalsComparator) {
811
- equalsComparator = fixEqualsComparator(equalsComparator, 'LeRed.useCallback() was given an invalid comparator:');
812
- comparingValues = ARRAY(comparingValues);
813
- // eslint-disable-next-line react-hooks/rules-of-hooks
814
- comparingValues = comparingValues.map(function (value) {
815
- return useCompareMemoize(value, equalsComparator);
816
- });
817
- // eslint-disable-next-line react-hooks/exhaustive-deps
818
- return React.useCallback(callable, comparingValues);
819
- };
820
- LeRed.usePrevious = function (value, initialValue) {
821
- var ref = LeRed.useRef(initialValue);
822
- LeRed.useEffect(function () {
823
- ref.current = value;
824
- }, [value]);
825
- return ref.current;
826
- };
827
- LeRed.useFont = function (font) {
828
- font = '12px ' + STRING(font).trim();
829
- var _LeRed$useState = LeRed.useState(false),
830
- _LeRed$useState2 = _slicedToArray(_LeRed$useState, 2),
831
- hasFont = _LeRed$useState2[0],
832
- setHasFont = _LeRed$useState2[1];
833
- LeRed.useEffect(function () {
834
- if (!hasFont) {
835
- var _globalThis$document4;
836
- if (!(globalThis !== null && globalThis !== void 0 && (_globalThis$document4 = globalThis.document) !== null && _globalThis$document4 !== void 0 && (_globalThis$document4 = _globalThis$document4.fonts) !== null && _globalThis$document4 !== void 0 && _globalThis$document4.check)) {
837
- setHasFont(true);
838
- return;
839
- }
840
- var handler = setInterval(function () {
841
- try {
842
- if (globalThis.document.fonts.check(font)) {
843
- clearInterval(handler);
844
- setHasFont(true);
845
- }
846
- } catch (e) {
847
- console.error(e);
848
- clearInterval(handler);
849
- setHasFont(true);
850
- }
851
- }, 30);
852
- }
853
- }, [hasFont]);
854
- return hasFont;
855
- };
856
-
857
- /**
858
- * Adds a <script> tag to the <head> of the document.
859
- * Only for development and testing purposes.
860
- *
861
- * @param {string} url The URL of the js file to include.
862
- * @param {Object} props Additional props of the <script> tag.
863
- */
864
- LeRed.useScript = function (url) {
865
- var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
866
- return LeRed.useEffect(function () {
867
- var _globalThis$document5, _globalThis$document6, _globalThis$document7;
868
- if (!(globalThis !== null && globalThis !== void 0 && (_globalThis$document5 = globalThis.document) !== null && _globalThis$document5 !== void 0 && _globalThis$document5.createElement) || !(globalThis !== null && globalThis !== void 0 && (_globalThis$document6 = globalThis.document) !== null && _globalThis$document6 !== void 0 && (_globalThis$document6 = _globalThis$document6.head) !== null && _globalThis$document6 !== void 0 && _globalThis$document6.appendChild) || !(globalThis !== null && globalThis !== void 0 && (_globalThis$document7 = globalThis.document) !== null && _globalThis$document7 !== void 0 && (_globalThis$document7 = _globalThis$document7.head) !== null && _globalThis$document7 !== void 0 && _globalThis$document7.removeChild)) {
869
- return;
870
- }
871
- var script = globalThis.document.createElement('script');
872
- script.type = 'text/javascript';
873
- script.src = url;
874
- LeUtils.each(props, function (value, key) {
875
- script.setAttribute(key, value);
876
- });
877
- globalThis.document.head.appendChild(script);
878
- return function () {
879
- return globalThis.document.head.removeChild(script);
880
- };
881
- }, [url, props]);
882
- };
883
- LeRed.mergeRefs = function () {
884
- for (var _len4 = arguments.length, refs = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
885
- refs[_key4] = arguments[_key4];
886
- }
887
- refs = LeUtils.flattenArray(refs);
888
- if (!refs) {
889
- return null;
890
- }
891
- var newRefs = [];
892
- LeUtils.each(refs, function (ref) {
893
- if (ref) {
894
- newRefs.push(ref);
895
- }
896
- });
897
- refs = newRefs;
898
- if (refs.length <= 0) {
899
- return null;
900
- }
901
- if (refs.length === 1) {
902
- return refs[0];
903
- }
904
- return function (inst) {
905
- LeUtils.each(refs, function (ref) {
906
- try {
907
- if (typeof ref === 'function') {
908
- ref(inst);
909
- } else if (ref) {
910
- ref.current = inst;
911
- }
912
- } catch (e) {
913
- console.error(e);
914
- }
915
- });
916
- };
917
- };
918
- LeRed.useTriggerable = function (event) {
919
- var _LeRed$useState3 = LeRed.useState([null, LeUtils.uniqueId()]),
920
- _LeRed$useState4 = _slicedToArray(_LeRed$useState3, 2),
921
- _LeRed$useState4$ = _slicedToArray(_LeRed$useState4[0], 2),
922
- value = _LeRed$useState4$[0];
923
- _LeRed$useState4$[1];
924
- var setValue = _LeRed$useState4[1];
925
- LeRed.useEffect(function () {
926
- if (!(globalThis !== null && globalThis !== void 0 && globalThis.addEventListener) || !(globalThis !== null && globalThis !== void 0 && globalThis.removeEventListener)) {
927
- return;
928
- }
929
- var callback = function callback(e) {
930
- setValue([e === null || e === void 0 ? void 0 : e.detail, LeUtils.uniqueId()]);
931
- };
932
- var eventName = 'lowentrytriggerable_' + event;
933
- globalThis.addEventListener(eventName, callback);
934
- return function () {
935
- return globalThis.removeEventListener(eventName, callback);
936
- };
937
- }, [event]);
938
- return value;
939
- };
940
- LeRed.trigger = function (event, value) {
941
- if (!(globalThis !== null && globalThis !== void 0 && globalThis.dispatchEvent) || !(globalThis !== null && globalThis !== void 0 && globalThis.CustomEvent)) {
942
- return;
943
- }
944
- var eventName = 'lowentrytriggerable_' + event;
945
- globalThis.dispatchEvent(new globalThis.CustomEvent(eventName, {
946
- detail: value
947
- }));
948
- };
949
-
950
- /**
951
- * A useState() hook that automatically resets to the defaultValue after the given duration.
952
- *
953
- * Example:
954
- *
955
- * ```js
956
- * const [value, setValue] = LeRed.useTempState(true, 2000);
957
- * // somewhere in your code:
958
- * setValue(false); // value is now false, after 2 seconds it will be reset to true
959
- * ```
960
- *
961
- * Repeated calls cause the timer to reset, meaning each set value will always remain for the full given duration.
962
- */
963
- LeRed.useTempState = function (defaultValue, duration) {
964
- var _LeRed$useState5 = LeRed.useState(defaultValue),
965
- _LeRed$useState6 = _slicedToArray(_LeRed$useState5, 2),
966
- value = _LeRed$useState6[0],
967
- setValue = _LeRed$useState6[1];
968
- var timeoutHandle = LeRed.useRef(null);
969
- return [value, function (newValue) {
970
- if (timeoutHandle.current) {
971
- clearTimeout(timeoutHandle.current);
972
- }
973
- setValue(newValue);
974
- timeoutHandle.current = setTimeout(function () {
975
- timeoutHandle.current = null;
976
- setValue(defaultValue);
977
- }, duration);
978
- }];
979
- };
980
-
981
- /**
982
- * Allows you to listen to the browser history events (forwards, backwards) and execute a callback on those events.
983
- *
984
- * You pass 2 functions to it (the callbacks), and it also provides 2 functions (for manually going forwards and backwards).
985
- *
986
- * Usage:
987
- *
988
- * ```js
989
- * const [goForwards, goBackwards] = LeRed.useHistory(() => console.log('has gone forwards'), () => console.log('has gone backwards'));
990
- * ```
991
- */
992
- LeRed.useHistory = function (_globalThis$addEventL3) {
993
- var historyStateListeners = [];
994
- globalThis === null || globalThis === void 0 || (_globalThis$addEventL3 = globalThis.addEventListener) === null || _globalThis$addEventL3 === void 0 || _globalThis$addEventL3.call(globalThis, 'popstate', function () {
995
- var _historyStateListener;
996
- (_historyStateListener = historyStateListeners.pop()) === null || _historyStateListener === void 0 || _historyStateListener.callback();
997
- });
998
- var addListener = function addListener(callback) {
999
- var id = LeUtils.uniqueId();
1000
- historyStateListeners.push({
1001
- id: id,
1002
- callback: callback
1003
- });
1004
- return id;
1005
- };
1006
- var removeListener = function removeListener(id) {
1007
- if (!id) {
1008
- return;
1009
- }
1010
- historyStateListeners = historyStateListeners.filter(function (listener) {
1011
- return listener.id !== id;
1012
- });
1013
- };
1014
- return function (onForward, onBack) {
1015
- var remaining = LeRed.useRef(0);
1016
- var id = LeRed.useRef(null);
1017
- var goBack = LeRed.useCallback(function () {
1018
- if (remaining.current <= 0) {
1019
- return;
1020
- }
1021
- remaining.current--;
1022
- if (remaining.current === 0) {
1023
- if (id.current) {
1024
- removeListener(id.current);
1025
- }
1026
- id.current = null;
1027
- }
1028
- onBack();
1029
- }, [onBack]);
1030
- return [function () /** do **/
1031
- {
1032
- LeRed.navigate('#');
1033
- remaining.current++;
1034
- if (remaining.current === 1) {
1035
- if (id.current) {
1036
- removeListener(id.current);
1037
- }
1038
- id.current = addListener(goBack);
1039
- }
1040
- onForward();
1041
- }, function () /** undo **/
1042
- {
1043
- if (remaining.current > 0) {
1044
- LeRed.navigate(-1);
1045
- }
1046
- }];
1047
- };
1048
- }();
1049
-
1050
- /**
1051
- * Similar to {@link LeRed.useHistory}, but this is specifically for toggling a boolean state between true and false. For example, for a modal, which you'd like to be closed when the user goes back in history.
1052
- *
1053
- * Example:
1054
- *
1055
- * ```js
1056
- * const [isModalOpen, openModal, closeModal] = LeRed.useHistoryState(false); // you'd open it programmatically using openModal(), afterwards, if the user goes back in history, it will close again
1057
- * ```
1058
- *
1059
- * or, if you'd like it to be true by default:
1060
- *
1061
- * ```js
1062
- * const [isModalOpen, openModal, closeModal] = LeRed.useHistoryState(true); // you'd close it programmatically using closeModal(), afterwards, if the user goes back in history, it will open again
1063
- * ```
1064
- */
1065
- LeRed.useHistoryState = function (initialState) {
1066
- var _LeRed$useState7 = LeRed.useState(!!initialState),
1067
- _LeRed$useState8 = _slicedToArray(_LeRed$useState7, 2),
1068
- state = _LeRed$useState8[0],
1069
- setState = _LeRed$useState8[1];
1070
- var _LeRed$useHistory = LeRed.useHistory(function () {
1071
- return setState(!initialState);
1072
- }, function () {
1073
- return setState(!!initialState);
1074
- }),
1075
- _LeRed$useHistory2 = _slicedToArray(_LeRed$useHistory, 2),
1076
- forwards = _LeRed$useHistory2[0],
1077
- backwards = _LeRed$useHistory2[1];
1078
- if (!!initialState) {
1079
- return [state, backwards, forwards];
1080
- }
1081
- return [state, forwards, backwards];
1082
- };
1083
-
1084
- /**
1085
- * Allows you to listen to the hash of the URL (window.location.hash).
1086
- *
1087
- * The hash can be useful than the query, as changing the hash will not cause the page to reload. Plus there's a good way to listen to hash changes, using the `hashchange` event, which is lacking for the query.
1088
- *
1089
- * Example:
1090
- *
1091
- * ```js
1092
- * const [hashParams, hashString] = LeRed.useUrlHashParams();
1093
- * ```
1094
- */
1095
- LeRed.useUrlHashParams = function () {
1096
- var getHashString = function getHashString() {
1097
- var _globalThis$location$, _globalThis$location;
1098
- return (_globalThis$location$ = globalThis === null || globalThis === void 0 || (_globalThis$location = globalThis.location) === null || _globalThis$location === void 0 || (_globalThis$location = _globalThis$location.hash) === null || _globalThis$location === void 0 || (_globalThis$location = _globalThis$location.trim()) === null || _globalThis$location === void 0 ? void 0 : _globalThis$location.replace(/^#/, '')) !== null && _globalThis$location$ !== void 0 ? _globalThis$location$ : '';
1099
- };
1100
- var parseHashParams = function parseHashParams(hashString) {
1101
- return Object.fromEntries(new URLSearchParams(hashString));
1102
- };
1103
-
1104
- /**
1105
- * @returns {[hashParams:URLSearchParams, hashString:string]}
1106
- */
1107
- return function () {
1108
- var _LeRed$useState9 = LeRed.useState(getHashString),
1109
- _LeRed$useState0 = _slicedToArray(_LeRed$useState9, 2),
1110
- hashString = _LeRed$useState0[0],
1111
- setHashString = _LeRed$useState0[1];
1112
- var hashParams = LeRed.useMemo(function () {
1113
- return parseHashParams(hashString);
1114
- }, [hashString]);
1115
- LeRed.useEffect(function () {
1116
- var _globalThis$addEventL4;
1117
- var onUrlChanged = function onUrlChanged() {
1118
- setHashString(getHashString());
1119
- };
1120
- globalThis === null || globalThis === void 0 || (_globalThis$addEventL4 = globalThis.addEventListener) === null || _globalThis$addEventL4 === void 0 || _globalThis$addEventL4.call(globalThis, 'hashchange', onUrlChanged);
1121
- return function () {
1122
- var _globalThis$removeEve3;
1123
- globalThis === null || globalThis === void 0 || (_globalThis$removeEve3 = globalThis.removeEventListener) === null || _globalThis$removeEve3 === void 0 || _globalThis$removeEve3.call(globalThis, 'hashchange', onUrlChanged);
1124
- };
1125
- }, []);
1126
- return [hashParams, hashString];
1127
- };
1128
- }();
1129
-
1130
- /**
1131
- * Allows you to easily create an <pre><img></pre> url and onError handler that will automatically retry loading the image if it fails.
1132
- */
1133
- LeRed.useRetryingImageUrl = function (url, options) {
1134
- url = STRING(url);
1135
- var urlHasQ = url.includes('?');
1136
- var _LeRed$useState1 = LeRed.useState(url),
1137
- _LeRed$useState10 = _slicedToArray(_LeRed$useState1, 2),
1138
- imageUrl = _LeRed$useState10[0],
1139
- setImageUrl = _LeRed$useState10[1];
1140
- var retries = LeRed.useRef(0);
1141
- var timeout = LeRed.useRef({
1142
- remove: function remove() {
1143
- return undefined;
1144
- }
1145
- });
1146
- LeRed.useEffect(function () {
1147
- timeout.current.remove();
1148
- retries.current = 0;
1149
- setImageUrl(url);
1150
- }, [url]);
1151
- var onImageLoadError = LeRed.useCallback(function () {
1152
- if (retries.current < INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.retries, 30)) {
1153
- var defaultDelay = 100 + 50 * retries.current;
1154
- timeout.current.remove();
1155
- timeout.current = LeUtils.setTimeout(function () {
1156
- setImageUrl(url + (urlHasQ ? '&' : '?') + ((options === null || options === void 0 ? void 0 : options.queryParam) || 'lowentryretryingimgversion') + '=' + retries.current++);
1157
- }, typeof (options === null || options === void 0 ? void 0 : options.delay) === 'function' ? INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay(retries.current), defaultDelay) : INT_LAX_ANY(options === null || options === void 0 ? void 0 : options.delay, defaultDelay));
1158
- }
1159
- }, [url, options]);
1160
- var onImageLoadErrorIgnored = LeRed.useCallback(function () {}, []);
1161
- if (!url) {
1162
- return [url, onImageLoadErrorIgnored];
1163
- }
1164
- return [imageUrl, onImageLoadError];
1165
- };
1166
-
1167
- /**
1168
- * Allows you to easily convert promises to react hooks.
1169
- *
1170
- * The given callable should return promises. The returned promises can be an array, an object, or even a single promise. The returned data of this hook will match the promises it has operated on.
1171
- *
1172
- * The given comparingValues can be anything, this is used to detect whether the given promises have changed or not, and so whether new promises have to be generated and executed again.
1173
- */
1174
- LeRed.usePromises = function (callable, comparingValues) {
1175
- var comparingValuesClone = LeUtils.clone(comparingValues);
1176
- var comparingValuesRef = LeRed.useRef(comparingValuesClone);
1177
- var latestComparingValuesRef = LeRed.useRef();
1178
- latestComparingValuesRef.current = comparingValuesClone;
1179
- var _LeRed$useState11 = LeRed.useState(null),
1180
- _LeRed$useState12 = _slicedToArray(_LeRed$useState11, 2),
1181
- data = _LeRed$useState12[0],
1182
- setData = _LeRed$useState12[1];
1183
- var _LeRed$useState13 = LeRed.useState(true),
1184
- _LeRed$useState14 = _slicedToArray(_LeRed$useState13, 2),
1185
- loading = _LeRed$useState14[0],
1186
- setLoading = _LeRed$useState14[1];
1187
- var _LeRed$useState15 = LeRed.useState(null),
1188
- _LeRed$useState16 = _slicedToArray(_LeRed$useState15, 2),
1189
- error = _LeRed$useState16[0],
1190
- setError = _LeRed$useState16[1];
1191
- LeRed.useEffect(function () {
1192
- setLoading(true);
1193
- setData(null);
1194
- setError(null);
1195
- try {
1196
- var promises = callable();
1197
- var promisesIsObject = IS_OBJECT(promises) && typeof (promises === null || promises === void 0 ? void 0 : promises.then) !== 'function';
1198
- var promisesIsArray = IS_ARRAY(promises);
1199
- var promisesKeyed = [];
1200
- if (promisesIsObject || promisesIsArray) {
1201
- LeUtils.each(promises, function (promise, key) {
1202
- promisesKeyed.push({
1203
- promise: promise,
1204
- key: key
1205
- });
1206
- });
1207
- } else {
1208
- promisesKeyed.push({
1209
- promise: promises,
1210
- key: undefined
1211
- });
1212
- }
1213
- var wrappedPromises = [];
1214
- LeUtils.each(promisesKeyed, function (_ref3) {
1215
- var promise = _ref3.promise,
1216
- key = _ref3.key;
1217
- wrappedPromises.push(promise.then(/*#__PURE__*/function () {
1218
- var _ref4 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee7(result) {
1219
- return _regeneratorRuntime.wrap(function (_context8) {
1220
- while (1) switch (_context8.prev = _context8.next) {
1221
- case 0:
1222
- return _context8.abrupt("return", {
1223
- result: result,
1224
- key: key
1225
- });
1226
- case 1:
1227
- case "end":
1228
- return _context8.stop();
1229
- }
1230
- }, _callee7);
1231
- }));
1232
- return function (_x) {
1233
- return _ref4.apply(this, arguments);
1234
- };
1235
- }()));
1236
- });
1237
- Promise.all(wrappedPromises).then(function (resultObjects) {
1238
- var _resultObjects$pop;
1239
- if (promisesIsObject) {
1240
- var results = {};
1241
- LeUtils.each(resultObjects, function (_ref5) {
1242
- var result = _ref5.result,
1243
- key = _ref5.key;
1244
- results[key] = result;
1245
- });
1246
- return results;
1247
- } else if (promisesIsArray) {
1248
- var _results = [];
1249
- LeUtils.each(resultObjects, function (_ref6) {
1250
- var result = _ref6.result,
1251
- key = _ref6.key;
1252
- _results[key] = result;
1253
- });
1254
- return _results;
1255
- }
1256
- return (_resultObjects$pop = resultObjects.pop()) === null || _resultObjects$pop === void 0 ? void 0 : _resultObjects$pop.result;
1257
- }).then(function (results) {
1258
- if (!LeUtils.equals(latestComparingValuesRef.current, comparingValuesClone)) {
1259
- // canceled
1260
- return;
1261
- }
1262
- comparingValuesRef.current = comparingValuesClone;
1263
- setLoading(false);
1264
- setData(results);
1265
- setError(null);
1266
- })["catch"](function (error) {
1267
- if (!LeUtils.equals(latestComparingValuesRef.current, comparingValuesClone)) {
1268
- // canceled
1269
- return;
1270
- }
1271
- comparingValuesRef.current = comparingValuesClone;
1272
- setLoading(false);
1273
- setData(null);
1274
- setError(LeUtils.purgeErrorMessage(error));
1275
- });
1276
- return function () {
1277
- LeUtils.each(wrappedPromises, function (promise) {
1278
- try {
1279
- var _promise$cancel;
1280
- promise === null || promise === void 0 || (_promise$cancel = promise.cancel) === null || _promise$cancel === void 0 || _promise$cancel.call(promise);
1281
- } catch (e) {
1282
- console.error('Failed to cancel the given promise:', e);
1283
- }
1284
- try {
1285
- var _promise$remove;
1286
- promise === null || promise === void 0 || (_promise$remove = promise.remove) === null || _promise$remove === void 0 || _promise$remove.call(promise);
1287
- } catch (e) {
1288
- console.error('Failed to remove the given promise:', e);
1289
- }
1290
- });
1291
- };
1292
- } catch (error) {
1293
- LeUtils.setAnimationFrameTimeout(function () {
1294
- if (!LeUtils.equals(latestComparingValuesRef.current, comparingValuesClone)) {
1295
- // canceled
1296
- return;
1297
- }
1298
- comparingValuesRef.current = comparingValuesClone;
1299
- setLoading(false);
1300
- setData(null);
1301
- setError(LeUtils.purgeErrorMessage(error));
1302
- });
1303
- }
1304
- }, [comparingValuesClone]);
1305
- if (!LeUtils.equals(comparingValuesRef.current, comparingValuesClone)) {
1306
- return [null, true, null];
1307
- }
1308
- return [data, loading, error];
1309
- };
1310
-
1311
- /**
1312
- * Allows you to easily obtain external data.
1313
- */
1314
- LeRed.useExternal = function (url, options, responseFunction) {
1315
- return LeRed.usePromises(function () {
1316
- var createFetch = function createFetch(urlString) {
1317
- return LeUtils.fetch(STRING(urlString), _objectSpread({
1318
- retries: 3
1319
- }, options !== null && options !== void 0 ? options : {})).then(/*#__PURE__*/function () {
1320
- var _ref7 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee8(response) {
1321
- var data;
1322
- return _regeneratorRuntime.wrap(function (_context9) {
1323
- while (1) switch (_context9.prev = _context9.next) {
1324
- case 0:
1325
- _context9.next = 1;
1326
- return responseFunction(response);
1327
- case 1:
1328
- data = _context9.sent;
1329
- if (!(typeof (options === null || options === void 0 ? void 0 : options.verify) === 'function')) {
1330
- _context9.next = 2;
1331
- break;
1332
- }
1333
- _context9.next = 2;
1334
- return options.verify(data, response);
1335
- case 2:
1336
- return _context9.abrupt("return", data);
1337
- case 3:
1338
- case "end":
1339
- return _context9.stop();
1340
- }
1341
- }, _callee8);
1342
- }));
1343
- return function (_x2) {
1344
- return _ref7.apply(this, arguments);
1345
- };
1346
- }());
1347
- };
1348
- if (IS_OBJECT(url)) {
1349
- var promises = {};
1350
- LeUtils.each(url, function (urlString, key) {
1351
- promises[key] = createFetch(urlString);
1352
- });
1353
- return promises;
1354
- }
1355
- if (IS_ARRAY(url)) {
1356
- var _promises = [];
1357
- LeUtils.each(url, function (urlString) {
1358
- _promises.push(createFetch(urlString));
1359
- });
1360
- return _promises;
1361
- }
1362
- return createFetch(url);
1363
- }, [url, options, responseFunction]);
1364
- };
1365
-
1366
- /**
1367
- * Allows you to easily obtain external JSON data.
1368
- */
1369
- LeRed.useExternalJson = function (url, options) {
1370
- var responseFunction = LeRed.useCallback(function (response) {
1371
- return response.json();
1372
- }, []);
1373
- return LeRed.useExternal(url, options, responseFunction);
1374
- };
1375
-
1376
- /**
1377
- * Allows you to easily obtain external Blob data.
1378
- */
1379
- LeRed.useExternalBlob = function (url, options) {
1380
- var responseFunction = LeRed.useCallback(function (response) {
1381
- return response.blob();
1382
- }, []);
1383
- return LeRed.useExternal(url, options, responseFunction);
1384
- };
1385
-
1386
- /**
1387
- * Allows you to easily obtain external ArrayBuffer data.
1388
- */
1389
- LeRed.useExternalArrayBuffer = function (url, options) {
1390
- var responseFunction = LeRed.useCallback(function (response) {
1391
- return response.arrayBuffer();
1392
- }, []);
1393
- return LeRed.useExternal(url, options, responseFunction);
1394
- };
1395
-
1396
- /**
1397
- * Allows you to easily obtain external string data.
1398
- */
1399
- LeRed.useExternalString = function (url, options) {
1400
- var responseFunction = LeRed.useCallback(function (response) {
1401
- return response.text();
1402
- }, []);
1403
- return LeRed.useExternal(url, options, responseFunction);
1404
- };
1405
-
1406
- /**
1407
- * Allows you to easily obtain external form data.
1408
- */
1409
- LeRed.useExternalFormData = function (url, options) {
1410
- var responseFunction = LeRed.useCallback(function (response) {
1411
- return response.formData();
1412
- }, []);
1413
- return LeRed.useExternal(url, options, responseFunction);
1414
- };
1415
- LeRed.Root = LeRed.memo(function (_ref8) {
1416
- var store = _ref8.store,
1417
- children = _ref8.children,
1418
- other = _objectWithoutProperties(_ref8, _excluded);
1419
- if (ISSET(store)) {
1420
- store = LeRed.configureStore(store);
1421
- return /*#__PURE__*/React.createElement(ReactRedux.Provider, _extends({
1422
- store: store,
1423
- children: children
1424
- }, other));
1425
- }
1426
- return children;
1427
- });
1428
- LeRed.PreloadComponent = function (load) {
1429
- if (typeof window !== 'undefined') {
1430
- var promise = load(); // in the browser, start loading already, before it's being rendered in React
1431
- return function () {
1432
- return promise;
1433
- };
1434
- }
1435
- return load;
1436
- };
1437
- LeRed.LoadComponent = LeRed.memo(function (_ref9) {
1438
- var loading = _ref9.loading,
1439
- load = _ref9.load,
1440
- other = _objectWithoutProperties(_ref9, _excluded2);
1441
- var _LeRed$useState17 = LeRed.useState(loading !== null && loading !== void 0 ? loading : null),
1442
- _LeRed$useState18 = _slicedToArray(_LeRed$useState17, 2),
1443
- Component = _LeRed$useState18[0],
1444
- setComponent = _LeRed$useState18[1];
1445
- LeRed.useEffect(function () {
1446
- _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
1447
- var LoadedComponent, _t6;
1448
- return _regeneratorRuntime.wrap(function (_context0) {
1449
- while (1) switch (_context0.prev = _context0.next) {
1450
- case 0:
1451
- if (!(typeof load === 'function')) {
1452
- _context0.next = 2;
1453
- break;
1454
- }
1455
- _context0.next = 1;
1456
- return load();
1457
- case 1:
1458
- _t6 = _context0.sent;
1459
- _context0.next = 4;
1460
- break;
1461
- case 2:
1462
- _context0.next = 3;
1463
- return load;
1464
- case 3:
1465
- _t6 = _context0.sent;
1466
- case 4:
1467
- LoadedComponent = _t6;
1468
- if (LoadedComponent) {
1469
- _context0.next = 5;
1470
- break;
1471
- }
1472
- setComponent(null);
1473
- return _context0.abrupt("return");
1474
- case 5:
1475
- setComponent(/*#__PURE__*/React.createElement(LoadedComponent, other));
1476
- case 6:
1477
- case "end":
1478
- return _context0.stop();
1479
- }
1480
- }, _callee9);
1481
- }))();
1482
- }, []);
1483
- return Component;
1484
- });
1485
- if (typeof Proxy === 'undefined') {
1486
- return LeRed;
1487
- }
1488
- return new Proxy(LeRed, {
1489
- set: function set(target, key, value) {
1490
- LeRed.set(key, value);
1491
- return true;
1492
- }
1493
- });
1494
- }();
1495
-
1496
- export { LeRed };
1497
- //# sourceMappingURL=index.js.map