@lowentry/utils 1.13.4 → 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 +145 -46
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeUtils.js +86 -23
package/index.js
CHANGED
|
@@ -1198,15 +1198,7 @@ var LeUtils = {
|
|
|
1198
1198
|
* @param {number} ms
|
|
1199
1199
|
* @returns {{remove:Function}}
|
|
1200
1200
|
*/
|
|
1201
|
-
setTimeout: function (
|
|
1202
|
-
function setTimeout(_x9, _x10) {
|
|
1203
|
-
return _setTimeout.apply(this, arguments);
|
|
1204
|
-
}
|
|
1205
|
-
setTimeout.toString = function () {
|
|
1206
|
-
return _setTimeout.toString();
|
|
1207
|
-
};
|
|
1208
|
-
return setTimeout;
|
|
1209
|
-
}(function (callback, ms) {
|
|
1201
|
+
setTimeout: function setTimeout(callback, ms) {
|
|
1210
1202
|
if (typeof window === 'undefined') {
|
|
1211
1203
|
return {
|
|
1212
1204
|
remove: function remove() {}
|
|
@@ -1214,7 +1206,7 @@ var LeUtils = {
|
|
|
1214
1206
|
}
|
|
1215
1207
|
ms = FLOAT_LAX(ms);
|
|
1216
1208
|
var lastTime = performance.now();
|
|
1217
|
-
var handler = setTimeout(function () {
|
|
1209
|
+
var handler = window.setTimeout(function () {
|
|
1218
1210
|
var currentTime = performance.now();
|
|
1219
1211
|
try {
|
|
1220
1212
|
callback((currentTime - lastTime) / 1000);
|
|
@@ -1226,12 +1218,12 @@ var LeUtils = {
|
|
|
1226
1218
|
return {
|
|
1227
1219
|
remove: function remove() {
|
|
1228
1220
|
if (handler !== null) {
|
|
1229
|
-
clearTimeout(handler);
|
|
1221
|
+
window.clearTimeout(handler);
|
|
1230
1222
|
handler = null;
|
|
1231
1223
|
}
|
|
1232
1224
|
}
|
|
1233
1225
|
};
|
|
1234
|
-
}
|
|
1226
|
+
},
|
|
1235
1227
|
/**
|
|
1236
1228
|
* @callback LeUtils~__setIntervalCallback
|
|
1237
1229
|
* @param {number} deltaTime
|
|
@@ -1246,15 +1238,7 @@ var LeUtils = {
|
|
|
1246
1238
|
* @param {boolean} [fireImmediately]
|
|
1247
1239
|
* @returns {{remove:Function}}
|
|
1248
1240
|
*/
|
|
1249
|
-
setInterval: function (
|
|
1250
|
-
function setInterval(_x11) {
|
|
1251
|
-
return _setInterval.apply(this, arguments);
|
|
1252
|
-
}
|
|
1253
|
-
setInterval.toString = function () {
|
|
1254
|
-
return _setInterval.toString();
|
|
1255
|
-
};
|
|
1256
|
-
return setInterval;
|
|
1257
|
-
}(function (callback) {
|
|
1241
|
+
setInterval: function setInterval(callback) {
|
|
1258
1242
|
var intervalMs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1000;
|
|
1259
1243
|
var fireImmediately = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
1260
1244
|
intervalMs = FLOAT_LAX_ANY(intervalMs, 1000);
|
|
@@ -1271,7 +1255,7 @@ var LeUtils = {
|
|
|
1271
1255
|
};
|
|
1272
1256
|
}
|
|
1273
1257
|
var lastTime = performance.now();
|
|
1274
|
-
var handler = setInterval(function () {
|
|
1258
|
+
var handler = window.setInterval(function () {
|
|
1275
1259
|
var currentTime = performance.now();
|
|
1276
1260
|
try {
|
|
1277
1261
|
callback((currentTime - lastTime) / 1000);
|
|
@@ -1283,12 +1267,12 @@ var LeUtils = {
|
|
|
1283
1267
|
return {
|
|
1284
1268
|
remove: function remove() {
|
|
1285
1269
|
if (handler !== null) {
|
|
1286
|
-
clearInterval(handler);
|
|
1270
|
+
window.clearInterval(handler);
|
|
1287
1271
|
handler = null;
|
|
1288
1272
|
}
|
|
1289
1273
|
}
|
|
1290
1274
|
};
|
|
1291
|
-
}
|
|
1275
|
+
},
|
|
1292
1276
|
/**
|
|
1293
1277
|
* @callback LeUtils~__setAnimationFrameTimeoutCallback
|
|
1294
1278
|
* @param {number} deltaTime
|
|
@@ -1328,7 +1312,7 @@ var LeUtils = {
|
|
|
1328
1312
|
return;
|
|
1329
1313
|
}
|
|
1330
1314
|
frames--;
|
|
1331
|
-
requestAnimationFrameId =
|
|
1315
|
+
requestAnimationFrameId = window.requestAnimationFrame(_tick);
|
|
1332
1316
|
}
|
|
1333
1317
|
};
|
|
1334
1318
|
_tick();
|
|
@@ -1336,7 +1320,7 @@ var LeUtils = {
|
|
|
1336
1320
|
remove: function remove() {
|
|
1337
1321
|
run = false;
|
|
1338
1322
|
if (requestAnimationFrameId !== null) {
|
|
1339
|
-
|
|
1323
|
+
window.cancelAnimationFrame(requestAnimationFrameId);
|
|
1340
1324
|
requestAnimationFrameId = null;
|
|
1341
1325
|
}
|
|
1342
1326
|
}
|
|
@@ -1390,16 +1374,16 @@ var LeUtils = {
|
|
|
1390
1374
|
}
|
|
1391
1375
|
frames--;
|
|
1392
1376
|
if (run) {
|
|
1393
|
-
requestAnimationFrameId =
|
|
1377
|
+
requestAnimationFrameId = window.requestAnimationFrame(_tick2);
|
|
1394
1378
|
}
|
|
1395
1379
|
}
|
|
1396
1380
|
};
|
|
1397
|
-
|
|
1381
|
+
window.requestAnimationFrame(_tick2);
|
|
1398
1382
|
return {
|
|
1399
1383
|
remove: function remove() {
|
|
1400
1384
|
run = false;
|
|
1401
1385
|
if (requestAnimationFrameId !== null) {
|
|
1402
|
-
|
|
1386
|
+
window.cancelAnimationFrame(requestAnimationFrameId);
|
|
1403
1387
|
requestAnimationFrameId = null;
|
|
1404
1388
|
}
|
|
1405
1389
|
}
|
|
@@ -1419,7 +1403,7 @@ var LeUtils = {
|
|
|
1419
1403
|
});
|
|
1420
1404
|
}
|
|
1421
1405
|
return new Promise(function (resolve) {
|
|
1422
|
-
return setTimeout(resolve, ms);
|
|
1406
|
+
return LeUtils.setTimeout(resolve, ms);
|
|
1423
1407
|
});
|
|
1424
1408
|
},
|
|
1425
1409
|
/**
|
|
@@ -1443,11 +1427,11 @@ var LeUtils = {
|
|
|
1443
1427
|
* Allows you to do a fetch, with built-in retry and abort functionality.
|
|
1444
1428
|
*
|
|
1445
1429
|
* @param {string} url
|
|
1446
|
-
* @param {
|
|
1430
|
+
* @param {{[retries]:number|null, [delay]:number|((attempt:number)=>number)|null}|null} [options]
|
|
1447
1431
|
* @returns {{then:Function, catch:Function, finally:Function, remove:Function, isRemoved:Function}}
|
|
1448
1432
|
*/
|
|
1449
1433
|
fetch: function (_fetch) {
|
|
1450
|
-
function fetch(
|
|
1434
|
+
function fetch(_x9, _x10) {
|
|
1451
1435
|
return _fetch.apply(this, arguments);
|
|
1452
1436
|
}
|
|
1453
1437
|
fetch.toString = function () {
|
|
@@ -1570,6 +1554,120 @@ var LeUtils = {
|
|
|
1570
1554
|
};
|
|
1571
1555
|
return result;
|
|
1572
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
|
+
}(),
|
|
1573
1671
|
/**
|
|
1574
1672
|
* Returns true if the user is on a smartphone device (mobile).
|
|
1575
1673
|
* Will return false if the user is on a tablet or on a desktop.
|
|
@@ -1842,7 +1940,7 @@ var LeUtils = {
|
|
|
1842
1940
|
*/
|
|
1843
1941
|
uniqueId: function () {
|
|
1844
1942
|
var previousUniqueIdsTime = null;
|
|
1845
|
-
var previousUniqueIds =
|
|
1943
|
+
var previousUniqueIds = new Map();
|
|
1846
1944
|
var numberToBytes = function numberToBytes(number) {
|
|
1847
1945
|
var size = number === 0 ? 0 : Math.ceil((Math.floor(Math.log2(number)) + 1) / 8);
|
|
1848
1946
|
var bytes = new Uint8ClampedArray(size);
|
|
@@ -1902,10 +2000,11 @@ var LeUtils = {
|
|
|
1902
2000
|
var result = generateUniqueId();
|
|
1903
2001
|
if (previousUniqueIdsTime !== result.time) {
|
|
1904
2002
|
previousUniqueIdsTime = result.time;
|
|
1905
|
-
previousUniqueIds
|
|
2003
|
+
previousUniqueIds.clear();
|
|
2004
|
+
previousUniqueIds.set(result.id, true);
|
|
1906
2005
|
return result.id;
|
|
1907
|
-
} else if (previousUniqueIds
|
|
1908
|
-
previousUniqueIds
|
|
2006
|
+
} else if (previousUniqueIds.get(result.id) !== true) {
|
|
2007
|
+
previousUniqueIds.set(result.id, true);
|
|
1909
2008
|
return result.id;
|
|
1910
2009
|
}
|
|
1911
2010
|
}
|
|
@@ -2331,7 +2430,7 @@ var LeUtils = {
|
|
|
2331
2430
|
* @returns {string}
|
|
2332
2431
|
*/
|
|
2333
2432
|
btoa: function (_btoa) {
|
|
2334
|
-
function btoa(
|
|
2433
|
+
function btoa(_x15) {
|
|
2335
2434
|
return _btoa.apply(this, arguments);
|
|
2336
2435
|
}
|
|
2337
2436
|
btoa.toString = function () {
|
|
@@ -2351,7 +2450,7 @@ var LeUtils = {
|
|
|
2351
2450
|
* @returns {string}
|
|
2352
2451
|
*/
|
|
2353
2452
|
atob: function (_atob) {
|
|
2354
|
-
function atob(
|
|
2453
|
+
function atob(_x16) {
|
|
2355
2454
|
return _atob.apply(this, arguments);
|
|
2356
2455
|
}
|
|
2357
2456
|
atob.toString = function () {
|
|
@@ -2945,12 +3044,12 @@ var LeUtils = {
|
|
|
2945
3044
|
};
|
|
2946
3045
|
}
|
|
2947
3046
|
var worker = new Worker('/workers/' + name + '.worker.js');
|
|
2948
|
-
var listeners =
|
|
3047
|
+
var listeners = new Map();
|
|
2949
3048
|
var addListener = function addListener(id, callback) {
|
|
2950
|
-
listeners
|
|
3049
|
+
listeners.set(id, callback);
|
|
2951
3050
|
};
|
|
2952
3051
|
var removeListener = function removeListener(id) {
|
|
2953
|
-
|
|
3052
|
+
listeners["delete"](id);
|
|
2954
3053
|
};
|
|
2955
3054
|
var sendMessage = function sendMessage(data, options) {
|
|
2956
3055
|
return new Promise(function (resolve, reject) {
|
|
@@ -2972,7 +3071,7 @@ var LeUtils = {
|
|
|
2972
3071
|
worker.onmessage = function (message) {
|
|
2973
3072
|
var data = message.data;
|
|
2974
3073
|
if (data !== null && data !== void 0 && data.id) {
|
|
2975
|
-
var callback = listeners
|
|
3074
|
+
var callback = listeners.get(data.id);
|
|
2976
3075
|
if (callback) {
|
|
2977
3076
|
removeListener(data.id);
|
|
2978
3077
|
callback(data);
|
|
@@ -2995,12 +3094,12 @@ var LeUtils = {
|
|
|
2995
3094
|
* @returns {Promise<Object>}
|
|
2996
3095
|
*/
|
|
2997
3096
|
sendWorkerMessage: function () {
|
|
2998
|
-
var workers =
|
|
3097
|
+
var workers = new Map();
|
|
2999
3098
|
return function (workerName, data, options) {
|
|
3000
|
-
if (!workers
|
|
3001
|
-
workers
|
|
3099
|
+
if (!workers.has(workerName)) {
|
|
3100
|
+
workers.set(workerName, LeUtils.createWorkerThread(workerName));
|
|
3002
3101
|
}
|
|
3003
|
-
return workers
|
|
3102
|
+
return workers.get(workerName).sendMessage(data, options);
|
|
3004
3103
|
};
|
|
3005
3104
|
}(),
|
|
3006
3105
|
/**
|