@lowentry/utils 1.13.5 → 1.14.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
@@ -1427,7 +1427,7 @@ var LeUtils = {
1427
1427
  * Allows you to do a fetch, with built-in retry and abort functionality.
1428
1428
  *
1429
1429
  * @param {string} url
1430
- * @param {Object} [options]
1430
+ * @param {{[retries]:number|null, [delay]:number|((attempt:number)=>number)|null}|null} [options]
1431
1431
  * @returns {{then:Function, catch:Function, finally:Function, remove:Function, isRemoved:Function}}
1432
1432
  */
1433
1433
  fetch: function (_fetch) {
@@ -1554,6 +1554,120 @@ var LeUtils = {
1554
1554
  };
1555
1555
  return result;
1556
1556
  }),
1557
+ /**
1558
+ * Allows you to do a fetch, with built-in retry functionality. Caches on the requested URL, so that the same URL will not be fetched multiple times.
1559
+ *
1560
+ * @param {string} url
1561
+ * @param {{[retries]:number|null, [delay]:number|((attempt:number)=>number)|null, [verify]:((data:*, response:*)=>void)|null}|null} [options]
1562
+ * @param {((response:*) => *)|null} [responseFunction] A function that will be called with the response object, and should return the data to be cached.
1563
+ * @returns {Promise<*>}
1564
+ */
1565
+ cachedFetch: function () {
1566
+ var cache = new Map();
1567
+ return /*#__PURE__*/function () {
1568
+ var _ref8 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee10(url, options, responseFunction) {
1569
+ var result, promise;
1570
+ return _regeneratorRuntime.wrap(function _callee10$(_context11) {
1571
+ while (1) switch (_context11.prev = _context11.next) {
1572
+ case 0:
1573
+ if (!cache.has(url)) {
1574
+ _context11.next = 12;
1575
+ break;
1576
+ }
1577
+ result = cache.get(url);
1578
+ if (!result.data) {
1579
+ _context11.next = 4;
1580
+ break;
1581
+ }
1582
+ return _context11.abrupt("return", result.data);
1583
+ case 4:
1584
+ if (!result.promise) {
1585
+ _context11.next = 8;
1586
+ break;
1587
+ }
1588
+ _context11.next = 7;
1589
+ return result.promise;
1590
+ case 7:
1591
+ return _context11.abrupt("return", _context11.sent);
1592
+ case 8:
1593
+ if (!result.error) {
1594
+ _context11.next = 10;
1595
+ break;
1596
+ }
1597
+ throw result.error;
1598
+ case 10:
1599
+ console.warn('Failed to use the cachedFetch cache, for URL: ', url, ', it is in an unexpected state: ', result);
1600
+ return _context11.abrupt("return", null);
1601
+ case 12:
1602
+ promise = LeUtils.fetch(url, options).then(/*#__PURE__*/function () {
1603
+ var _ref9 = _asyncToGenerator(/*#__PURE__*/_regeneratorRuntime.mark(function _callee9(response) {
1604
+ var data;
1605
+ return _regeneratorRuntime.wrap(function _callee9$(_context10) {
1606
+ while (1) switch (_context10.prev = _context10.next) {
1607
+ case 0:
1608
+ if (!responseFunction) {
1609
+ _context10.next = 6;
1610
+ break;
1611
+ }
1612
+ _context10.next = 3;
1613
+ return responseFunction(response);
1614
+ case 3:
1615
+ _context10.t0 = _context10.sent;
1616
+ _context10.next = 7;
1617
+ break;
1618
+ case 6:
1619
+ _context10.t0 = response;
1620
+ case 7:
1621
+ data = _context10.t0;
1622
+ if (!(typeof (options === null || options === void 0 ? void 0 : options.verify) === 'function')) {
1623
+ _context10.next = 11;
1624
+ break;
1625
+ }
1626
+ _context10.next = 11;
1627
+ return options.verify(data, response);
1628
+ case 11:
1629
+ return _context10.abrupt("return", data);
1630
+ case 12:
1631
+ case "end":
1632
+ return _context10.stop();
1633
+ }
1634
+ }, _callee9);
1635
+ }));
1636
+ return function (_x14) {
1637
+ return _ref9.apply(this, arguments);
1638
+ };
1639
+ }()).then(function (data) {
1640
+ cache.set(url, {
1641
+ data: data
1642
+ });
1643
+ return data;
1644
+ })["catch"](function (error) {
1645
+ cache.set(url, {
1646
+ error: error
1647
+ });
1648
+ console.error('Failed to fetch: ', error);
1649
+ throw error;
1650
+ });
1651
+ if (!cache.has(url)) {
1652
+ cache.set(url, {
1653
+ promise: promise
1654
+ });
1655
+ }
1656
+ _context11.next = 16;
1657
+ return promise;
1658
+ case 16:
1659
+ return _context11.abrupt("return", _context11.sent);
1660
+ case 17:
1661
+ case "end":
1662
+ return _context11.stop();
1663
+ }
1664
+ }, _callee10);
1665
+ }));
1666
+ return function (_x11, _x12, _x13) {
1667
+ return _ref8.apply(this, arguments);
1668
+ };
1669
+ }();
1670
+ }(),
1557
1671
  /**
1558
1672
  * Returns true if the user is on a smartphone device (mobile).
1559
1673
  * Will return false if the user is on a tablet or on a desktop.
@@ -1826,7 +1940,7 @@ var LeUtils = {
1826
1940
  */
1827
1941
  uniqueId: function () {
1828
1942
  var previousUniqueIdsTime = null;
1829
- var previousUniqueIds = {};
1943
+ var previousUniqueIds = new Map();
1830
1944
  var numberToBytes = function numberToBytes(number) {
1831
1945
  var size = number === 0 ? 0 : Math.ceil((Math.floor(Math.log2(number)) + 1) / 8);
1832
1946
  var bytes = new Uint8ClampedArray(size);
@@ -1886,10 +2000,11 @@ var LeUtils = {
1886
2000
  var result = generateUniqueId();
1887
2001
  if (previousUniqueIdsTime !== result.time) {
1888
2002
  previousUniqueIdsTime = result.time;
1889
- previousUniqueIds = _defineProperty({}, result.id, true);
2003
+ previousUniqueIds.clear();
2004
+ previousUniqueIds.set(result.id, true);
1890
2005
  return result.id;
1891
- } else if (previousUniqueIds[result.id] !== true) {
1892
- previousUniqueIds[result.id] = true;
2006
+ } else if (previousUniqueIds.get(result.id) !== true) {
2007
+ previousUniqueIds.set(result.id, true);
1893
2008
  return result.id;
1894
2009
  }
1895
2010
  }
@@ -2315,7 +2430,7 @@ var LeUtils = {
2315
2430
  * @returns {string}
2316
2431
  */
2317
2432
  btoa: function (_btoa) {
2318
- function btoa(_x11) {
2433
+ function btoa(_x15) {
2319
2434
  return _btoa.apply(this, arguments);
2320
2435
  }
2321
2436
  btoa.toString = function () {
@@ -2335,7 +2450,7 @@ var LeUtils = {
2335
2450
  * @returns {string}
2336
2451
  */
2337
2452
  atob: function (_atob) {
2338
- function atob(_x12) {
2453
+ function atob(_x16) {
2339
2454
  return _atob.apply(this, arguments);
2340
2455
  }
2341
2456
  atob.toString = function () {
@@ -2929,12 +3044,12 @@ var LeUtils = {
2929
3044
  };
2930
3045
  }
2931
3046
  var worker = new Worker('/workers/' + name + '.worker.js');
2932
- var listeners = {};
3047
+ var listeners = new Map();
2933
3048
  var addListener = function addListener(id, callback) {
2934
- listeners[id] = callback;
3049
+ listeners.set(id, callback);
2935
3050
  };
2936
3051
  var removeListener = function removeListener(id) {
2937
- delete listeners[id];
3052
+ listeners["delete"](id);
2938
3053
  };
2939
3054
  var sendMessage = function sendMessage(data, options) {
2940
3055
  return new Promise(function (resolve, reject) {
@@ -2956,7 +3071,7 @@ var LeUtils = {
2956
3071
  worker.onmessage = function (message) {
2957
3072
  var data = message.data;
2958
3073
  if (data !== null && data !== void 0 && data.id) {
2959
- var callback = listeners[data.id];
3074
+ var callback = listeners.get(data.id);
2960
3075
  if (callback) {
2961
3076
  removeListener(data.id);
2962
3077
  callback(data);
@@ -2979,12 +3094,12 @@ var LeUtils = {
2979
3094
  * @returns {Promise<Object>}
2980
3095
  */
2981
3096
  sendWorkerMessage: function () {
2982
- var workers = {};
3097
+ var workers = new Map();
2983
3098
  return function (workerName, data, options) {
2984
- if (!workers[workerName]) {
2985
- workers[workerName] = LeUtils.createWorkerThread(workerName);
3099
+ if (!workers.has(workerName)) {
3100
+ workers.set(workerName, LeUtils.createWorkerThread(workerName));
2986
3101
  }
2987
- return workers[workerName].sendMessage(data, options);
3102
+ return workers.get(workerName).sendMessage(data, options);
2988
3103
  };
2989
3104
  }(),
2990
3105
  /**