@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/package.json
CHANGED
package/src/LeUtils.js
CHANGED
|
@@ -1091,7 +1091,7 @@ export const LeUtils = {
|
|
|
1091
1091
|
ms = FLOAT_LAX(ms);
|
|
1092
1092
|
|
|
1093
1093
|
let lastTime = performance.now();
|
|
1094
|
-
let handler = setTimeout(() =>
|
|
1094
|
+
let handler = window.setTimeout(() =>
|
|
1095
1095
|
{
|
|
1096
1096
|
const currentTime = performance.now();
|
|
1097
1097
|
try
|
|
@@ -1111,7 +1111,7 @@ export const LeUtils = {
|
|
|
1111
1111
|
{
|
|
1112
1112
|
if(handler !== null)
|
|
1113
1113
|
{
|
|
1114
|
-
clearTimeout(handler);
|
|
1114
|
+
window.clearTimeout(handler);
|
|
1115
1115
|
handler = null;
|
|
1116
1116
|
}
|
|
1117
1117
|
},
|
|
@@ -1159,7 +1159,7 @@ export const LeUtils = {
|
|
|
1159
1159
|
}
|
|
1160
1160
|
|
|
1161
1161
|
let lastTime = performance.now();
|
|
1162
|
-
let handler = setInterval(() =>
|
|
1162
|
+
let handler = window.setInterval(() =>
|
|
1163
1163
|
{
|
|
1164
1164
|
const currentTime = performance.now();
|
|
1165
1165
|
try
|
|
@@ -1179,7 +1179,7 @@ export const LeUtils = {
|
|
|
1179
1179
|
{
|
|
1180
1180
|
if(handler !== null)
|
|
1181
1181
|
{
|
|
1182
|
-
clearInterval(handler);
|
|
1182
|
+
window.clearInterval(handler);
|
|
1183
1183
|
handler = null;
|
|
1184
1184
|
}
|
|
1185
1185
|
},
|
|
@@ -1237,7 +1237,7 @@ export const LeUtils = {
|
|
|
1237
1237
|
return;
|
|
1238
1238
|
}
|
|
1239
1239
|
frames--;
|
|
1240
|
-
requestAnimationFrameId =
|
|
1240
|
+
requestAnimationFrameId = window.requestAnimationFrame(tick);
|
|
1241
1241
|
}
|
|
1242
1242
|
};
|
|
1243
1243
|
tick();
|
|
@@ -1249,7 +1249,7 @@ export const LeUtils = {
|
|
|
1249
1249
|
run = false;
|
|
1250
1250
|
if(requestAnimationFrameId !== null)
|
|
1251
1251
|
{
|
|
1252
|
-
|
|
1252
|
+
window.cancelAnimationFrame(requestAnimationFrameId);
|
|
1253
1253
|
requestAnimationFrameId = null;
|
|
1254
1254
|
}
|
|
1255
1255
|
},
|
|
@@ -1322,11 +1322,11 @@ export const LeUtils = {
|
|
|
1322
1322
|
|
|
1323
1323
|
if(run)
|
|
1324
1324
|
{
|
|
1325
|
-
requestAnimationFrameId =
|
|
1325
|
+
requestAnimationFrameId = window.requestAnimationFrame(tick);
|
|
1326
1326
|
}
|
|
1327
1327
|
}
|
|
1328
1328
|
};
|
|
1329
|
-
|
|
1329
|
+
window.requestAnimationFrame(tick);
|
|
1330
1330
|
|
|
1331
1331
|
return {
|
|
1332
1332
|
remove:
|
|
@@ -1335,7 +1335,7 @@ export const LeUtils = {
|
|
|
1335
1335
|
run = false;
|
|
1336
1336
|
if(requestAnimationFrameId !== null)
|
|
1337
1337
|
{
|
|
1338
|
-
|
|
1338
|
+
window.cancelAnimationFrame(requestAnimationFrameId);
|
|
1339
1339
|
requestAnimationFrameId = null;
|
|
1340
1340
|
}
|
|
1341
1341
|
},
|
|
@@ -1356,7 +1356,7 @@ export const LeUtils = {
|
|
|
1356
1356
|
{
|
|
1357
1357
|
return new Promise(resolve => resolve(undefined));
|
|
1358
1358
|
}
|
|
1359
|
-
return new Promise(resolve => setTimeout(resolve, ms));
|
|
1359
|
+
return new Promise(resolve => LeUtils.setTimeout(resolve, ms));
|
|
1360
1360
|
},
|
|
1361
1361
|
|
|
1362
1362
|
/**
|
|
@@ -1380,7 +1380,7 @@ export const LeUtils = {
|
|
|
1380
1380
|
* Allows you to do a fetch, with built-in retry and abort functionality.
|
|
1381
1381
|
*
|
|
1382
1382
|
* @param {string} url
|
|
1383
|
-
* @param {
|
|
1383
|
+
* @param {{[retries]:number|null, [delay]:number|((attempt:number)=>number)|null}|null} [options]
|
|
1384
1384
|
* @returns {{then:Function, catch:Function, finally:Function, remove:Function, isRemoved:Function}}
|
|
1385
1385
|
*/
|
|
1386
1386
|
fetch:
|
|
@@ -1466,6 +1466,68 @@ export const LeUtils = {
|
|
|
1466
1466
|
return result;
|
|
1467
1467
|
},
|
|
1468
1468
|
|
|
1469
|
+
/**
|
|
1470
|
+
* 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.
|
|
1471
|
+
*
|
|
1472
|
+
* @param {string} url
|
|
1473
|
+
* @param {{[retries]:number|null, [delay]:number|((attempt:number)=>number)|null, [verify]:((data:*, response:*)=>void)|null}|null} [options]
|
|
1474
|
+
* @param {((response:*) => *)|null} [responseFunction] A function that will be called with the response object, and should return the data to be cached.
|
|
1475
|
+
* @returns {Promise<*>}
|
|
1476
|
+
*/
|
|
1477
|
+
cachedFetch:
|
|
1478
|
+
(() =>
|
|
1479
|
+
{
|
|
1480
|
+
const cache = new Map();
|
|
1481
|
+
return async (url, options, responseFunction) =>
|
|
1482
|
+
{
|
|
1483
|
+
if(cache.has(url))
|
|
1484
|
+
{
|
|
1485
|
+
const result = cache.get(url);
|
|
1486
|
+
if(result.data)
|
|
1487
|
+
{
|
|
1488
|
+
return result.data;
|
|
1489
|
+
}
|
|
1490
|
+
if(result.promise)
|
|
1491
|
+
{
|
|
1492
|
+
return await result.promise;
|
|
1493
|
+
}
|
|
1494
|
+
if(result.error)
|
|
1495
|
+
{
|
|
1496
|
+
throw result.error;
|
|
1497
|
+
}
|
|
1498
|
+
console.warn('Failed to use the cachedFetch cache, for URL: ', url, ', it is in an unexpected state: ', result);
|
|
1499
|
+
return null;
|
|
1500
|
+
}
|
|
1501
|
+
|
|
1502
|
+
const promise = LeUtils.fetch(url, options)
|
|
1503
|
+
.then(async response =>
|
|
1504
|
+
{
|
|
1505
|
+
const data = responseFunction ? (await responseFunction(response)) : response;
|
|
1506
|
+
if(typeof options?.verify === 'function')
|
|
1507
|
+
{
|
|
1508
|
+
await options.verify(data, response);
|
|
1509
|
+
}
|
|
1510
|
+
return data;
|
|
1511
|
+
})
|
|
1512
|
+
.then(data =>
|
|
1513
|
+
{
|
|
1514
|
+
cache.set(url, {data});
|
|
1515
|
+
return data;
|
|
1516
|
+
})
|
|
1517
|
+
.catch(error =>
|
|
1518
|
+
{
|
|
1519
|
+
cache.set(url, {error});
|
|
1520
|
+
console.error('Failed to fetch: ', error);
|
|
1521
|
+
throw error;
|
|
1522
|
+
});
|
|
1523
|
+
if(!cache.has(url))
|
|
1524
|
+
{
|
|
1525
|
+
cache.set(url, {promise});
|
|
1526
|
+
}
|
|
1527
|
+
return await promise;
|
|
1528
|
+
};
|
|
1529
|
+
})(),
|
|
1530
|
+
|
|
1469
1531
|
/**
|
|
1470
1532
|
* Returns true if the user is on a smartphone device (mobile).
|
|
1471
1533
|
* Will return false if the user is on a tablet or on a desktop.
|
|
@@ -1812,7 +1874,7 @@ export const LeUtils = {
|
|
|
1812
1874
|
(() =>
|
|
1813
1875
|
{
|
|
1814
1876
|
let previousUniqueIdsTime = null;
|
|
1815
|
-
let previousUniqueIds =
|
|
1877
|
+
let previousUniqueIds = new Map();
|
|
1816
1878
|
|
|
1817
1879
|
const numberToBytes = (number) =>
|
|
1818
1880
|
{
|
|
@@ -1896,12 +1958,13 @@ export const LeUtils = {
|
|
|
1896
1958
|
if(previousUniqueIdsTime !== result.time)
|
|
1897
1959
|
{
|
|
1898
1960
|
previousUniqueIdsTime = result.time;
|
|
1899
|
-
previousUniqueIds
|
|
1961
|
+
previousUniqueIds.clear();
|
|
1962
|
+
previousUniqueIds.set(result.id, true);
|
|
1900
1963
|
return result.id;
|
|
1901
1964
|
}
|
|
1902
|
-
else if(previousUniqueIds
|
|
1965
|
+
else if(previousUniqueIds.get(result.id) !== true)
|
|
1903
1966
|
{
|
|
1904
|
-
previousUniqueIds
|
|
1967
|
+
previousUniqueIds.set(result.id, true);
|
|
1905
1968
|
return result.id;
|
|
1906
1969
|
}
|
|
1907
1970
|
}
|
|
@@ -3146,16 +3209,16 @@ export const LeUtils = {
|
|
|
3146
3209
|
}
|
|
3147
3210
|
|
|
3148
3211
|
const worker = new Worker('/workers/' + name + '.worker.js');
|
|
3149
|
-
let listeners =
|
|
3212
|
+
let listeners = new Map();
|
|
3150
3213
|
|
|
3151
3214
|
const addListener = (id, callback) =>
|
|
3152
3215
|
{
|
|
3153
|
-
listeners
|
|
3216
|
+
listeners.set(id, callback);
|
|
3154
3217
|
};
|
|
3155
3218
|
|
|
3156
3219
|
const removeListener = (id) =>
|
|
3157
3220
|
{
|
|
3158
|
-
delete
|
|
3221
|
+
listeners.delete(id);
|
|
3159
3222
|
};
|
|
3160
3223
|
|
|
3161
3224
|
const sendMessage = (data, options) =>
|
|
@@ -3186,7 +3249,7 @@ export const LeUtils = {
|
|
|
3186
3249
|
const data = message.data;
|
|
3187
3250
|
if(data?.id)
|
|
3188
3251
|
{
|
|
3189
|
-
const callback = listeners
|
|
3252
|
+
const callback = listeners.get(data.id);
|
|
3190
3253
|
if(callback)
|
|
3191
3254
|
{
|
|
3192
3255
|
removeListener(data.id);
|
|
@@ -3211,14 +3274,14 @@ export const LeUtils = {
|
|
|
3211
3274
|
sendWorkerMessage:
|
|
3212
3275
|
(() =>
|
|
3213
3276
|
{
|
|
3214
|
-
const workers =
|
|
3277
|
+
const workers = new Map();
|
|
3215
3278
|
return (workerName, data, options) =>
|
|
3216
3279
|
{
|
|
3217
|
-
if(!workers
|
|
3280
|
+
if(!workers.has(workerName))
|
|
3218
3281
|
{
|
|
3219
|
-
workers
|
|
3282
|
+
workers.set(workerName, LeUtils.createWorkerThread(workerName));
|
|
3220
3283
|
}
|
|
3221
|
-
return workers
|
|
3284
|
+
return workers.get(workerName).sendMessage(data, options);
|
|
3222
3285
|
};
|
|
3223
3286
|
})(),
|
|
3224
3287
|
|