@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 +130 -15
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/src/LeUtils.js +76 -13
package/package.json
CHANGED
package/src/LeUtils.js
CHANGED
|
@@ -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
|
|