contentful-management 12.0.0-new-beta.7 → 12.0.0-new-beta.9
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/dist/browser/index.js
CHANGED
|
@@ -995,90 +995,113 @@ var contentfulManagement = (function (exports) {
|
|
|
995
995
|
var isString$1 = /*@__PURE__*/getDefaultExportFromCjs(isStringExports);
|
|
996
996
|
|
|
997
997
|
class AbortError extends Error {
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
998
|
+
name = 'AbortError';
|
|
999
|
+
constructor() {
|
|
1000
|
+
super('Throttled function aborted');
|
|
1001
|
+
}
|
|
1002
1002
|
}
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1003
|
+
/**
|
|
1004
|
+
* Throttle promise-returning/async/normal functions.
|
|
1005
|
+
*
|
|
1006
|
+
* It rate-limits function calls without discarding them, making it ideal for external API interactions where avoiding call loss is crucial.
|
|
1007
|
+
*
|
|
1008
|
+
* @returns A throttle function.
|
|
1009
|
+
*
|
|
1010
|
+
* Both the `limit` and `interval` options must be specified.
|
|
1011
|
+
*
|
|
1012
|
+
* @example
|
|
1013
|
+
* ```
|
|
1014
|
+
* import pThrottle from './PThrottle';
|
|
1015
|
+
*
|
|
1016
|
+
* const now = Date.now();
|
|
1017
|
+
*
|
|
1018
|
+
* const throttle = pThrottle({
|
|
1019
|
+
* limit: 2,
|
|
1020
|
+
* interval: 1000
|
|
1021
|
+
* });
|
|
1022
|
+
*
|
|
1023
|
+
* const throttled = throttle(async index => {
|
|
1024
|
+
* const secDiff = ((Date.now() - now) / 1000).toFixed();
|
|
1025
|
+
* return `${index}: ${secDiff}s`;
|
|
1026
|
+
* });
|
|
1027
|
+
*
|
|
1028
|
+
* for (let index = 1; index <= 6; index++) {
|
|
1029
|
+
* (async () => {
|
|
1030
|
+
* console.log(await throttled(index));
|
|
1031
|
+
* })();
|
|
1032
|
+
* }
|
|
1033
|
+
* //=> 1: 0s
|
|
1034
|
+
* //=> 2: 0s
|
|
1035
|
+
* //=> 3: 1s
|
|
1036
|
+
* //=> 4: 1s
|
|
1037
|
+
* //=> 5: 2s
|
|
1038
|
+
* //=> 6: 2s
|
|
1039
|
+
* ```
|
|
1040
|
+
*/
|
|
1041
|
+
function pThrottle({ limit, interval, strict, onDelay }) {
|
|
1042
|
+
if (!Number.isFinite(limit)) {
|
|
1043
|
+
throw new TypeError('Expected `limit` to be a finite number');
|
|
1044
|
+
}
|
|
1045
|
+
if (!Number.isFinite(interval)) {
|
|
1046
|
+
throw new TypeError('Expected `interval` to be a finite number');
|
|
1047
|
+
}
|
|
1048
|
+
const queue = new Map();
|
|
1049
|
+
let currentTick = 0;
|
|
1050
|
+
let activeCount = 0;
|
|
1051
|
+
function windowedDelay() {
|
|
1052
|
+
const now = Date.now();
|
|
1053
|
+
if (now - currentTick > interval) {
|
|
1054
|
+
activeCount = 1;
|
|
1055
|
+
currentTick = now;
|
|
1056
|
+
return 0;
|
|
1057
|
+
}
|
|
1058
|
+
if (activeCount < limit) {
|
|
1059
|
+
activeCount++;
|
|
1060
|
+
}
|
|
1061
|
+
else {
|
|
1062
|
+
currentTick += interval;
|
|
1063
|
+
activeCount = 1;
|
|
1064
|
+
}
|
|
1065
|
+
return currentTick - now;
|
|
1066
|
+
}
|
|
1067
|
+
const getDelay = windowedDelay;
|
|
1068
|
+
return function (function_) {
|
|
1069
|
+
const throttled = function (...arguments_) {
|
|
1070
|
+
if (!throttled.isEnabled) {
|
|
1071
|
+
return (async () => function_.apply(this, arguments_))();
|
|
1072
|
+
}
|
|
1073
|
+
let timeoutId;
|
|
1074
|
+
return new Promise((resolve, reject) => {
|
|
1075
|
+
const execute = () => {
|
|
1076
|
+
resolve(function_.apply(this, arguments_));
|
|
1077
|
+
queue.delete(timeoutId);
|
|
1078
|
+
};
|
|
1079
|
+
const delay = getDelay();
|
|
1080
|
+
if (delay > 0) {
|
|
1081
|
+
timeoutId = setTimeout(execute, delay);
|
|
1082
|
+
queue.set(timeoutId, reject);
|
|
1083
|
+
onDelay?.();
|
|
1084
|
+
}
|
|
1085
|
+
else {
|
|
1086
|
+
execute();
|
|
1087
|
+
}
|
|
1088
|
+
});
|
|
1089
|
+
};
|
|
1090
|
+
throttled.abort = () => {
|
|
1091
|
+
for (const timeout of queue.keys()) {
|
|
1092
|
+
clearTimeout(timeout);
|
|
1093
|
+
queue.get(timeout)(new AbortError());
|
|
1094
|
+
}
|
|
1095
|
+
queue.clear();
|
|
1096
|
+
};
|
|
1097
|
+
throttled.isEnabled = true;
|
|
1098
|
+
Object.defineProperty(throttled, 'queueSize', {
|
|
1099
|
+
get() {
|
|
1100
|
+
return queue.size;
|
|
1101
|
+
},
|
|
1102
|
+
});
|
|
1103
|
+
return throttled;
|
|
1104
|
+
};
|
|
1082
1105
|
}
|
|
1083
1106
|
|
|
1084
1107
|
const PERCENTAGE_REGEX = /(?<value>\d+)(%)/;
|