@lowentry/react-redux 1.9.2 → 1.10.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 CHANGED
@@ -399,7 +399,7 @@ var LeRed = function () {
399
399
  case 0:
400
400
  _context5.next = 2;
401
401
  return ReduxSagaEffects.takeEvery(reducerAction, /*#__PURE__*/_regeneratorRuntime.mark(function _callee4(action) {
402
- var promiseResolve, promiseReject, promise, result;
402
+ var promiseResolve, promiseReject, _promise, result;
403
403
  return _regeneratorRuntime.wrap(function _callee4$(_context4) {
404
404
  while (1) switch (_context4.prev = _context4.next) {
405
405
  case 0:
@@ -407,16 +407,16 @@ var LeRed = function () {
407
407
  promiseReject = null;
408
408
  _context4.prev = 2;
409
409
  if (action.__lowentry_dispatch__ === true) {
410
- promise = new Promise(function (resolve, reject) {
410
+ _promise = new Promise(function (resolve, reject) {
411
411
  promiseResolve = resolve;
412
412
  promiseReject = reject;
413
413
  });
414
414
  if (Array.isArray(action.__lowentry_dispatch_result__)) {
415
- if (typeof promise !== 'undefined') {
416
- action.__lowentry_dispatch_result__.push(promise);
415
+ if (typeof _promise !== 'undefined') {
416
+ action.__lowentry_dispatch_result__.push(_promise);
417
417
  }
418
418
  } else {
419
- action.__lowentry_dispatch_result__ = promise;
419
+ action.__lowentry_dispatch_result__ = _promise;
420
420
  }
421
421
  }
422
422
  _context4.next = 6;
@@ -618,7 +618,7 @@ var LeRed = function () {
618
618
  return LeRed.useEffect(function () {
619
619
  var stop = false;
620
620
  _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee5() {
621
- var _iterator, _step, promise;
621
+ var _iterator, _step, _promise2;
622
622
  return _regeneratorRuntime.wrap(function _callee5$(_context6) {
623
623
  while (1) switch (_context6.prev = _context6.next) {
624
624
  case 0:
@@ -630,7 +630,7 @@ var LeRed = function () {
630
630
  _context6.next = 13;
631
631
  break;
632
632
  }
633
- promise = _step.value;
633
+ _promise2 = _step.value;
634
634
  if (!stop) {
635
635
  _context6.next = 7;
636
636
  break;
@@ -638,7 +638,7 @@ var LeRed = function () {
638
638
  return _context6.abrupt("return");
639
639
  case 7:
640
640
  _context6.next = 9;
641
- return promise;
641
+ return _promise2;
642
642
  case 9:
643
643
  if (!stop) {
644
644
  _context6.next = 11;
@@ -674,7 +674,7 @@ var LeRed = function () {
674
674
  return LeRed.useEffect(function () {
675
675
  var stop = false;
676
676
  _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee6() {
677
- var _iterator2, _step2, promise;
677
+ var _iterator2, _step2, _promise3;
678
678
  return _regeneratorRuntime.wrap(function _callee6$(_context7) {
679
679
  while (1) switch (_context7.prev = _context7.next) {
680
680
  case 0:
@@ -690,7 +690,7 @@ var LeRed = function () {
690
690
  _context7.next = 14;
691
691
  break;
692
692
  }
693
- promise = _step2.value;
693
+ _promise3 = _step2.value;
694
694
  if (!stop) {
695
695
  _context7.next = 8;
696
696
  break;
@@ -698,7 +698,7 @@ var LeRed = function () {
698
698
  return _context7.abrupt("return");
699
699
  case 8:
700
700
  _context7.next = 10;
701
- return promise;
701
+ return _promise3;
702
702
  case 10:
703
703
  if (!stop) {
704
704
  _context7.next = 12;
@@ -1100,7 +1100,7 @@ var LeRed = function () {
1100
1100
  setImageUrl(url + (urlHasQ ? '&' : '?') + ((options === null || options === void 0 ? void 0 : options.queryParam) || 'lowentryretryingimgversion') + '=' + retries.current++);
1101
1101
  }, 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));
1102
1102
  }
1103
- }, [url]);
1103
+ }, [url, options]);
1104
1104
  var onImageLoadErrorIgnored = LeRed.useCallback(function () {}, []);
1105
1105
  if (!url) {
1106
1106
  return [url, onImageLoadErrorIgnored];
@@ -1109,13 +1109,17 @@ var LeRed = function () {
1109
1109
  };
1110
1110
 
1111
1111
  /**
1112
- * Allows you to easily obtain external data.
1112
+ * Allows you to easily convert promises to react hooks.
1113
+ *
1114
+ * 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.
1115
+ *
1116
+ * 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.
1113
1117
  */
1114
- LeRed.useExternal = function (url, options, hardcodedResponseFunction) {
1115
- var paramsRef = LeRed.useRef({
1116
- url: url,
1117
- options: options
1118
- });
1118
+ LeRed.usePromises = function (callable, comparingValues) {
1119
+ var comparingValuesClone = LeUtils.clone(comparingValues);
1120
+ var comparingValuesRef = LeRed.useRef(comparingValuesClone);
1121
+ var latestComparingValuesRef = LeRed.useRef();
1122
+ latestComparingValuesRef.current = comparingValuesClone;
1119
1123
  var _LeRed$useState11 = LeRed.useState(null),
1120
1124
  _LeRed$useState12 = _slicedToArray(_LeRed$useState11, 2),
1121
1125
  data = _LeRed$useState12[0],
@@ -1132,158 +1136,228 @@ var LeRed = function () {
1132
1136
  setLoading(true);
1133
1137
  setData(null);
1134
1138
  setError(null);
1135
- var urlStrings = [];
1136
- if (IS_OBJECT(url) || IS_ARRAY(url)) {
1137
- LeUtils.each(url, function (urlString, key) {
1138
- urlStrings.push({
1139
- urlString: STRING(urlString),
1140
- key: key
1139
+ try {
1140
+ var promises = callable();
1141
+ var promisesKeyed = [];
1142
+ if (IS_OBJECT(promises) || IS_ARRAY(promises)) {
1143
+ LeUtils.each(promises, function (urlString, key) {
1144
+ promisesKeyed.push({
1145
+ promise: promise,
1146
+ key: key
1147
+ });
1141
1148
  });
1149
+ } else {
1150
+ promisesKeyed.push({
1151
+ promises: promises,
1152
+ key: undefined
1153
+ });
1154
+ }
1155
+ var wrappedPromises = [];
1156
+ LeUtils.each(promisesKeyed, function (_ref3) {
1157
+ var promise = _ref3.promise,
1158
+ key = _ref3.key;
1159
+ wrappedPromises.push(promise.then( /*#__PURE__*/function () {
1160
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(result) {
1161
+ return _regeneratorRuntime.wrap(function _callee7$(_context8) {
1162
+ while (1) switch (_context8.prev = _context8.next) {
1163
+ case 0:
1164
+ return _context8.abrupt("return", {
1165
+ result: result,
1166
+ key: key
1167
+ });
1168
+ case 1:
1169
+ case "end":
1170
+ return _context8.stop();
1171
+ }
1172
+ }, _callee7);
1173
+ }));
1174
+ return function (_x) {
1175
+ return _ref4.apply(this, arguments);
1176
+ };
1177
+ }()));
1142
1178
  });
1143
- } else {
1144
- urlStrings.push({
1145
- urlString: STRING(url),
1146
- key: undefined
1179
+ Promise.all(wrappedPromises).then(function (resultObjects) {
1180
+ var _resultObjects$pop;
1181
+ if (IS_OBJECT(promises)) {
1182
+ var results = {};
1183
+ LeUtils.each(resultObjects, function (_ref5) {
1184
+ var result = _ref5.result,
1185
+ key = _ref5.key;
1186
+ results[key] = result;
1187
+ });
1188
+ return results;
1189
+ } else if (IS_ARRAY(promises)) {
1190
+ var _results = [];
1191
+ LeUtils.each(resultObjects, function (_ref6) {
1192
+ var result = _ref6.result,
1193
+ key = _ref6.key;
1194
+ _results[key] = result;
1195
+ });
1196
+ return _results;
1197
+ }
1198
+ return (_resultObjects$pop = resultObjects.pop()) === null || _resultObjects$pop === void 0 ? void 0 : _resultObjects$pop.result;
1199
+ }).then(function (results) {
1200
+ if (!LeUtils.equals(latestComparingValuesRef.current, comparingValuesClone)) {
1201
+ // canceled
1202
+ return;
1203
+ }
1204
+ comparingValuesRef.current = comparingValuesClone;
1205
+ setLoading(false);
1206
+ setData(results);
1207
+ setError(null);
1208
+ })["catch"](function (error) {
1209
+ if (!LeUtils.equals(latestComparingValuesRef.current, comparingValuesClone)) {
1210
+ // canceled
1211
+ return;
1212
+ }
1213
+ comparingValuesRef.current = comparingValuesClone;
1214
+ setLoading(false);
1215
+ setData(null);
1216
+ setError(LeUtils.purgeErrorMessage(error));
1217
+ });
1218
+ return function () {
1219
+ LeUtils.each(wrappedPromises, function (promise) {
1220
+ try {
1221
+ var _promise$cancel;
1222
+ promise === null || promise === void 0 || (_promise$cancel = promise.cancel) === null || _promise$cancel === void 0 || _promise$cancel.call(promise);
1223
+ } catch (e) {
1224
+ console.error('Failed to cancel the given promise:', e);
1225
+ }
1226
+ try {
1227
+ var _promise$remove;
1228
+ promise === null || promise === void 0 || (_promise$remove = promise.remove) === null || _promise$remove === void 0 || _promise$remove.call(promise);
1229
+ } catch (e) {
1230
+ console.error('Failed to remove the given promise:', e);
1231
+ }
1232
+ });
1233
+ };
1234
+ } catch (error) {
1235
+ LeUtils.setAnimationFrameTimeout(function () {
1236
+ if (!LeUtils.equals(latestComparingValuesRef.current, comparingValuesClone)) {
1237
+ // canceled
1238
+ return;
1239
+ }
1240
+ comparingValuesRef.current = comparingValuesClone;
1241
+ setLoading(false);
1242
+ setData(null);
1243
+ setError(LeUtils.purgeErrorMessage(error));
1147
1244
  });
1148
1245
  }
1149
- var fetches = [];
1150
- LeUtils.each(urlStrings, function (_ref3) {
1151
- var urlString = _ref3.urlString,
1152
- key = _ref3.key;
1153
- fetches.push(LeUtils.fetch(urlString, _objectSpread({
1246
+ }, [comparingValuesClone]);
1247
+ if (!LeUtils.equals(comparingValuesRef.current, comparingValuesClone)) {
1248
+ return [null, true, null];
1249
+ }
1250
+ return [data, loading, error];
1251
+ };
1252
+
1253
+ /**
1254
+ * Allows you to easily obtain external data.
1255
+ */
1256
+ LeRed.useExternal = function (url, options, responseFunction) {
1257
+ return LeRed.usePromises(function () {
1258
+ var createFetch = function createFetch(urlString) {
1259
+ return LeUtils.fetch(STRING(urlString), _objectSpread({
1154
1260
  retries: 3
1155
1261
  }, options !== null && options !== void 0 ? options : {})).then( /*#__PURE__*/function () {
1156
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee7(response) {
1262
+ var _ref7 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8(response) {
1157
1263
  var data;
1158
- return _regeneratorRuntime.wrap(function _callee7$(_context8) {
1159
- while (1) switch (_context8.prev = _context8.next) {
1264
+ return _regeneratorRuntime.wrap(function _callee8$(_context9) {
1265
+ while (1) switch (_context9.prev = _context9.next) {
1160
1266
  case 0:
1161
- _context8.next = 2;
1162
- return hardcodedResponseFunction(response);
1267
+ _context9.next = 2;
1268
+ return responseFunction(response);
1163
1269
  case 2:
1164
- data = _context8.sent;
1270
+ data = _context9.sent;
1165
1271
  if (!(typeof (options === null || options === void 0 ? void 0 : options.verify) === 'function')) {
1166
- _context8.next = 6;
1272
+ _context9.next = 6;
1167
1273
  break;
1168
1274
  }
1169
- _context8.next = 6;
1275
+ _context9.next = 6;
1170
1276
  return options.verify(data, response);
1171
1277
  case 6:
1172
- return _context8.abrupt("return", {
1173
- data: data,
1174
- key: key
1175
- });
1278
+ return _context9.abrupt("return", data);
1176
1279
  case 7:
1177
1280
  case "end":
1178
- return _context8.stop();
1281
+ return _context9.stop();
1179
1282
  }
1180
- }, _callee7);
1283
+ }, _callee8);
1181
1284
  }));
1182
- return function (_x) {
1183
- return _ref4.apply(this, arguments);
1285
+ return function (_x2) {
1286
+ return _ref7.apply(this, arguments);
1184
1287
  };
1185
- }()));
1186
- });
1187
- Promise.all(fetches).then(function (values) {
1188
- var _values$pop;
1189
- if (IS_OBJECT(url)) {
1190
- var result = {};
1191
- LeUtils.each(values, function (_ref5) {
1192
- var data = _ref5.data,
1193
- key = _ref5.key;
1194
- result[key] = data;
1195
- });
1196
- return result;
1197
- } else if (IS_ARRAY(url)) {
1198
- var _result = [];
1199
- LeUtils.each(values, function (_ref6) {
1200
- var data = _ref6.data,
1201
- key = _ref6.key;
1202
- _result[key] = data;
1203
- });
1204
- return _result;
1205
- }
1206
- return (_values$pop = values.pop()) === null || _values$pop === void 0 ? void 0 : _values$pop.data;
1207
- }).then(function (data) {
1208
- paramsRef.current = {
1209
- url: url,
1210
- options: options
1211
- };
1212
- setLoading(false);
1213
- setData(data);
1214
- setError(null);
1215
- })["catch"](function (error) {
1216
- paramsRef.current = {
1217
- url: url,
1218
- options: options
1219
- };
1220
- setLoading(false);
1221
- setData(null);
1222
- setError(LeUtils.purgeErrorMessage(error));
1223
- });
1224
- return function () {
1225
- LeUtils.each(fetches, function (fetch) {
1226
- fetch.remove();
1227
- });
1288
+ }());
1228
1289
  };
1229
- }, [url, options]);
1230
- if (!LeUtils.equals(paramsRef.current, {
1231
- url: url,
1232
- options: options
1233
- })) {
1234
- return [null, true, null];
1235
- }
1236
- return [data, loading, error];
1290
+ if (IS_OBJECT(url)) {
1291
+ var promises = {};
1292
+ LeUtils.each(url, function (urlString, key) {
1293
+ promises[key] = createFetch(urlString);
1294
+ });
1295
+ return promises;
1296
+ }
1297
+ if (IS_ARRAY(url)) {
1298
+ var _promises = [];
1299
+ LeUtils.each(url, function (urlString) {
1300
+ _promises.push(createFetch(urlString));
1301
+ });
1302
+ return _promises;
1303
+ }
1304
+ return createFetch(url);
1305
+ }, [url, options, responseFunction]);
1237
1306
  };
1238
1307
 
1239
1308
  /**
1240
1309
  * Allows you to easily obtain external JSON data.
1241
1310
  */
1242
1311
  LeRed.useExternalJson = function (url, options) {
1243
- return LeRed.useExternal(url, options, function (response) {
1312
+ var responseFunction = LeRed.useCallback(function (response) {
1244
1313
  return response.json();
1245
- });
1314
+ }, []);
1315
+ return LeRed.useExternal(url, options, responseFunction);
1246
1316
  };
1247
1317
 
1248
1318
  /**
1249
1319
  * Allows you to easily obtain external Blob data.
1250
1320
  */
1251
1321
  LeRed.useExternalBlob = function (url, options) {
1252
- return LeRed.useExternal(url, options, function (response) {
1322
+ var responseFunction = LeRed.useCallback(function (response) {
1253
1323
  return response.blob();
1254
- });
1324
+ }, []);
1325
+ return LeRed.useExternal(url, options, responseFunction);
1255
1326
  };
1256
1327
 
1257
1328
  /**
1258
1329
  * Allows you to easily obtain external ArrayBuffer data.
1259
1330
  */
1260
1331
  LeRed.useExternalArrayBuffer = function (url, options) {
1261
- return LeRed.useExternal(url, options, function (response) {
1332
+ var responseFunction = LeRed.useCallback(function (response) {
1262
1333
  return response.arrayBuffer();
1263
- });
1334
+ }, []);
1335
+ return LeRed.useExternal(url, options, responseFunction);
1264
1336
  };
1265
1337
 
1266
1338
  /**
1267
1339
  * Allows you to easily obtain external string data.
1268
1340
  */
1269
1341
  LeRed.useExternalString = function (url, options) {
1270
- return LeRed.useExternal(url, options, function (response) {
1342
+ var responseFunction = LeRed.useCallback(function (response) {
1271
1343
  return response.text();
1272
- });
1344
+ }, []);
1345
+ return LeRed.useExternal(url, options, responseFunction);
1273
1346
  };
1274
1347
 
1275
1348
  /**
1276
1349
  * Allows you to easily obtain external form data.
1277
1350
  */
1278
1351
  LeRed.useExternalFormData = function (url, options) {
1279
- return LeRed.useExternal(url, options, function (response) {
1352
+ var responseFunction = LeRed.useCallback(function (response) {
1280
1353
  return response.formData();
1281
- });
1354
+ }, []);
1355
+ return LeRed.useExternal(url, options, responseFunction);
1282
1356
  };
1283
- LeRed.Root = LeRed.memo(function (_ref7) {
1284
- var store = _ref7.store,
1285
- children = _ref7.children,
1286
- other = _objectWithoutProperties(_ref7, _excluded);
1357
+ LeRed.Root = LeRed.memo(function (_ref8) {
1358
+ var store = _ref8.store,
1359
+ children = _ref8.children,
1360
+ other = _objectWithoutProperties(_ref8, _excluded);
1287
1361
  if (ISSET(store)) {
1288
1362
  store = LeRed.configureStore(store);
1289
1363
  return /*#__PURE__*/React.createElement(ReactRedux.Provider, _extends({
@@ -1294,57 +1368,57 @@ var LeRed = function () {
1294
1368
  });
1295
1369
  LeRed.PreloadComponent = function (load) {
1296
1370
  if (typeof window !== 'undefined') {
1297
- var promise = load(); // start loading already, before it's being rendered in React
1371
+ var _promise4 = load(); // start loading already, before it's being rendered in React
1298
1372
  return function () {
1299
- return promise;
1373
+ return _promise4;
1300
1374
  };
1301
1375
  }
1302
1376
  return load;
1303
1377
  };
1304
- LeRed.LoadComponent = LeRed.memo(function (_ref8) {
1305
- var loading = _ref8.loading,
1306
- load = _ref8.load,
1307
- other = _objectWithoutProperties(_ref8, _excluded2);
1378
+ LeRed.LoadComponent = LeRed.memo(function (_ref9) {
1379
+ var loading = _ref9.loading,
1380
+ load = _ref9.load,
1381
+ other = _objectWithoutProperties(_ref9, _excluded2);
1308
1382
  var _LeRed$useState17 = LeRed.useState(loading !== null && loading !== void 0 ? loading : null),
1309
1383
  _LeRed$useState18 = _slicedToArray(_LeRed$useState17, 2),
1310
1384
  Component = _LeRed$useState18[0],
1311
1385
  setComponent = _LeRed$useState18[1];
1312
1386
  LeRed.useEffect(function () {
1313
- _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee8() {
1387
+ _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee9() {
1314
1388
  var LoadedComponent;
1315
- return _regeneratorRuntime.wrap(function _callee8$(_context9) {
1316
- while (1) switch (_context9.prev = _context9.next) {
1389
+ return _regeneratorRuntime.wrap(function _callee9$(_context10) {
1390
+ while (1) switch (_context10.prev = _context10.next) {
1317
1391
  case 0:
1318
1392
  if (!(typeof load === 'function')) {
1319
- _context9.next = 6;
1393
+ _context10.next = 6;
1320
1394
  break;
1321
1395
  }
1322
- _context9.next = 3;
1396
+ _context10.next = 3;
1323
1397
  return load();
1324
1398
  case 3:
1325
- _context9.t0 = _context9.sent;
1326
- _context9.next = 9;
1399
+ _context10.t0 = _context10.sent;
1400
+ _context10.next = 9;
1327
1401
  break;
1328
1402
  case 6:
1329
- _context9.next = 8;
1403
+ _context10.next = 8;
1330
1404
  return load;
1331
1405
  case 8:
1332
- _context9.t0 = _context9.sent;
1406
+ _context10.t0 = _context10.sent;
1333
1407
  case 9:
1334
- LoadedComponent = _context9.t0;
1408
+ LoadedComponent = _context10.t0;
1335
1409
  if (LoadedComponent) {
1336
- _context9.next = 13;
1410
+ _context10.next = 13;
1337
1411
  break;
1338
1412
  }
1339
1413
  setComponent(null);
1340
- return _context9.abrupt("return");
1414
+ return _context10.abrupt("return");
1341
1415
  case 13:
1342
1416
  setComponent( /*#__PURE__*/React.createElement(LoadedComponent, other));
1343
1417
  case 14:
1344
1418
  case "end":
1345
- return _context9.stop();
1419
+ return _context10.stop();
1346
1420
  }
1347
- }, _callee8);
1421
+ }, _callee9);
1348
1422
  }))();
1349
1423
  }, []);
1350
1424
  return Component;