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.
@@ -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
- constructor() {
999
- super('Throttled function aborted');
1000
- this.name = 'AbortError';
1001
- }
998
+ name = 'AbortError';
999
+ constructor() {
1000
+ super('Throttled function aborted');
1001
+ }
1002
1002
  }
1003
-
1004
- function pThrottle({limit, interval, strict, onDelay}) {
1005
- if (!Number.isFinite(limit)) {
1006
- throw new TypeError('Expected `limit` to be a finite number');
1007
- }
1008
-
1009
- if (!Number.isFinite(interval)) {
1010
- throw new TypeError('Expected `interval` to be a finite number');
1011
- }
1012
-
1013
- const queue = new Map();
1014
-
1015
- let currentTick = 0;
1016
- let activeCount = 0;
1017
-
1018
- function windowedDelay() {
1019
- const now = Date.now();
1020
-
1021
- if ((now - currentTick) > interval) {
1022
- activeCount = 1;
1023
- currentTick = now;
1024
- return 0;
1025
- }
1026
-
1027
- if (activeCount < limit) {
1028
- activeCount++;
1029
- } else {
1030
- currentTick += interval;
1031
- activeCount = 1;
1032
- }
1033
-
1034
- return currentTick - now;
1035
- }
1036
-
1037
- const getDelay = windowedDelay;
1038
-
1039
- return function_ => {
1040
- const throttled = function (...arguments_) {
1041
- if (!throttled.isEnabled) {
1042
- return (async () => function_.apply(this, arguments_))();
1043
- }
1044
-
1045
- let timeoutId;
1046
- return new Promise((resolve, reject) => {
1047
- const execute = () => {
1048
- resolve(function_.apply(this, arguments_));
1049
- queue.delete(timeoutId);
1050
- };
1051
-
1052
- const delay = getDelay();
1053
- if (delay > 0) {
1054
- timeoutId = setTimeout(execute, delay);
1055
- queue.set(timeoutId, reject);
1056
- onDelay?.(...arguments_);
1057
- } else {
1058
- execute();
1059
- }
1060
- });
1061
- };
1062
-
1063
- throttled.abort = () => {
1064
- for (const timeout of queue.keys()) {
1065
- clearTimeout(timeout);
1066
- queue.get(timeout)(new AbortError());
1067
- }
1068
-
1069
- queue.clear();
1070
- };
1071
-
1072
- throttled.isEnabled = true;
1073
-
1074
- Object.defineProperty(throttled, 'queueSize', {
1075
- get() {
1076
- return queue.size;
1077
- },
1078
- });
1079
-
1080
- return throttled;
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+)(%)/;