@budibase/shared-core 3.1.0 → 3.1.2

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.
@@ -0,0 +1 @@
1
+ export declare function wait(ms: number): Promise<unknown>;
@@ -1,5 +1,7 @@
1
1
  export * from "./helpers";
2
2
  export * from "./integrations";
3
+ export * from "./async";
4
+ export * from "./retry";
3
5
  export * as cron from "./cron";
4
6
  export * as schema from "./schema";
5
7
  export * as views from "./views";
@@ -0,0 +1,5 @@
1
+ interface RetryOpts {
2
+ times?: number;
3
+ }
4
+ export declare function retry<T>(fn: () => Promise<T>, opts?: RetryOpts): Promise<T>;
5
+ export {};
package/dist/index.js CHANGED
@@ -1538,13 +1538,13 @@ var require_lodash = __commonJS({
1538
1538
  }
1539
1539
  return true;
1540
1540
  }
1541
- function baseDelay(func, wait, args) {
1541
+ function baseDelay(func, wait2, args) {
1542
1542
  if (typeof func != "function") {
1543
1543
  throw new TypeError2(FUNC_ERROR_TEXT);
1544
1544
  }
1545
1545
  return setTimeout2(function() {
1546
1546
  func.apply(undefined2, args);
1547
- }, wait);
1547
+ }, wait2);
1548
1548
  }
1549
1549
  function baseDifference(array, values3, iteratee2, comparator) {
1550
1550
  var index = -1, includes2 = arrayIncludes, isCommon = true, length = array.length, result2 = [], valuesLength = values3.length;
@@ -3373,8 +3373,8 @@ var require_lodash = __commonJS({
3373
3373
  return object[key];
3374
3374
  }
3375
3375
  var setData = shortOut(baseSetData);
3376
- var setTimeout2 = ctxSetTimeout || function(func, wait) {
3377
- return root.setTimeout(func, wait);
3376
+ var setTimeout2 = ctxSetTimeout || function(func, wait2) {
3377
+ return root.setTimeout(func, wait2);
3378
3378
  };
3379
3379
  var setToString = shortOut(baseSetToString);
3380
3380
  function setWrapToString(wrapper, reference, bitmask) {
@@ -4144,16 +4144,16 @@ var require_lodash = __commonJS({
4144
4144
  result2.placeholder = curryRight.placeholder;
4145
4145
  return result2;
4146
4146
  }
4147
- function debounce(func, wait, options) {
4147
+ function debounce(func, wait2, options) {
4148
4148
  var lastArgs, lastThis, maxWait, result2, timerId, lastCallTime, lastInvokeTime = 0, leading = false, maxing = false, trailing = true;
4149
4149
  if (typeof func != "function") {
4150
4150
  throw new TypeError2(FUNC_ERROR_TEXT);
4151
4151
  }
4152
- wait = toNumber(wait) || 0;
4152
+ wait2 = toNumber(wait2) || 0;
4153
4153
  if (isObject(options)) {
4154
4154
  leading = !!options.leading;
4155
4155
  maxing = "maxWait" in options;
4156
- maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait) : maxWait;
4156
+ maxWait = maxing ? nativeMax(toNumber(options.maxWait) || 0, wait2) : maxWait;
4157
4157
  trailing = "trailing" in options ? !!options.trailing : trailing;
4158
4158
  }
4159
4159
  function invokeFunc(time) {
@@ -4165,16 +4165,16 @@ var require_lodash = __commonJS({
4165
4165
  }
4166
4166
  function leadingEdge(time) {
4167
4167
  lastInvokeTime = time;
4168
- timerId = setTimeout2(timerExpired, wait);
4168
+ timerId = setTimeout2(timerExpired, wait2);
4169
4169
  return leading ? invokeFunc(time) : result2;
4170
4170
  }
4171
4171
  function remainingWait(time) {
4172
- var timeSinceLastCall = time - lastCallTime, timeSinceLastInvoke = time - lastInvokeTime, timeWaiting = wait - timeSinceLastCall;
4172
+ var timeSinceLastCall = time - lastCallTime, timeSinceLastInvoke = time - lastInvokeTime, timeWaiting = wait2 - timeSinceLastCall;
4173
4173
  return maxing ? nativeMin(timeWaiting, maxWait - timeSinceLastInvoke) : timeWaiting;
4174
4174
  }
4175
4175
  function shouldInvoke(time) {
4176
4176
  var timeSinceLastCall = time - lastCallTime, timeSinceLastInvoke = time - lastInvokeTime;
4177
- return lastCallTime === undefined2 || timeSinceLastCall >= wait || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait;
4177
+ return lastCallTime === undefined2 || timeSinceLastCall >= wait2 || timeSinceLastCall < 0 || maxing && timeSinceLastInvoke >= maxWait;
4178
4178
  }
4179
4179
  function timerExpired() {
4180
4180
  var time = now();
@@ -4212,12 +4212,12 @@ var require_lodash = __commonJS({
4212
4212
  }
4213
4213
  if (maxing) {
4214
4214
  clearTimeout2(timerId);
4215
- timerId = setTimeout2(timerExpired, wait);
4215
+ timerId = setTimeout2(timerExpired, wait2);
4216
4216
  return invokeFunc(lastCallTime);
4217
4217
  }
4218
4218
  }
4219
4219
  if (timerId === undefined2) {
4220
- timerId = setTimeout2(timerExpired, wait);
4220
+ timerId = setTimeout2(timerExpired, wait2);
4221
4221
  }
4222
4222
  return result2;
4223
4223
  }
@@ -4228,8 +4228,8 @@ var require_lodash = __commonJS({
4228
4228
  var defer = baseRest(function(func, args) {
4229
4229
  return baseDelay(func, 1, args);
4230
4230
  });
4231
- var delay = baseRest(function(func, wait, args) {
4232
- return baseDelay(func, toNumber(wait) || 0, args);
4231
+ var delay = baseRest(function(func, wait2, args) {
4232
+ return baseDelay(func, toNumber(wait2) || 0, args);
4233
4233
  });
4234
4234
  function flip(func) {
4235
4235
  return createWrap(func, WRAP_FLIP_FLAG);
@@ -4315,7 +4315,7 @@ var require_lodash = __commonJS({
4315
4315
  return apply(func, this, otherArgs);
4316
4316
  });
4317
4317
  }
4318
- function throttle(func, wait, options) {
4318
+ function throttle(func, wait2, options) {
4319
4319
  var leading = true, trailing = true;
4320
4320
  if (typeof func != "function") {
4321
4321
  throw new TypeError2(FUNC_ERROR_TEXT);
@@ -4324,9 +4324,9 @@ var require_lodash = __commonJS({
4324
4324
  leading = "leading" in options ? !!options.leading : leading;
4325
4325
  trailing = "trailing" in options ? !!options.trailing : trailing;
4326
4326
  }
4327
- return debounce(func, wait, {
4327
+ return debounce(func, wait2, {
4328
4328
  "leading": leading,
4329
- "maxWait": wait,
4329
+ "maxWait": wait2,
4330
4330
  "trailing": trailing
4331
4331
  });
4332
4332
  }
@@ -17787,9 +17787,11 @@ __export(helpers_exports, {
17787
17787
  getUserLabel: () => getUserLabel,
17788
17788
  isGoogleSheets: () => isGoogleSheets,
17789
17789
  isSQL: () => isSQL,
17790
+ retry: () => retry,
17790
17791
  roles: () => roles_exports,
17791
17792
  schema: () => schema_exports,
17792
17793
  views: () => views_exports,
17794
+ wait: () => wait,
17793
17795
  withTimeout: () => withTimeout
17794
17796
  });
17795
17797
 
@@ -17887,6 +17889,30 @@ function isSQL(datasource) {
17887
17889
  return SQL.indexOf(datasource.source) !== -1 || datasource.isSQL === true;
17888
17890
  }
17889
17891
 
17892
+ // src/helpers/async.ts
17893
+ async function wait(ms) {
17894
+ return new Promise((resolve) => setTimeout(resolve, ms));
17895
+ }
17896
+
17897
+ // src/helpers/retry.ts
17898
+ async function retry(fn, opts) {
17899
+ const { times = 3 } = opts || {};
17900
+ if (times < 1) {
17901
+ throw new Error(`invalid retry count: ${times}`);
17902
+ }
17903
+ let lastError;
17904
+ for (let i = 0; i < times; i++) {
17905
+ const backoff = 1.5 ** i * 1e3 * (Math.random() + 0.5);
17906
+ await wait(backoff);
17907
+ try {
17908
+ return await fn();
17909
+ } catch (e) {
17910
+ lastError = e;
17911
+ }
17912
+ }
17913
+ throw lastError;
17914
+ }
17915
+
17890
17916
  // src/helpers/cron.ts
17891
17917
  var cron_exports = {};
17892
17918
  __export(cron_exports, {