@orioro/util 0.5.0 → 0.6.0

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/index.mjs CHANGED
@@ -483,6 +483,38 @@ function arrayChunk(array, chunkSize) {
483
483
  return chunks;
484
484
  }
485
485
 
486
+ var TimeoutError = /** @class */function (_super) {
487
+ __extends(TimeoutError, _super);
488
+ function TimeoutError(message) {
489
+ var _this = _super.call(this, message) || this;
490
+ _this.name = 'TimeoutError';
491
+ _this.code = 'ETIMEDOUT';
492
+ _this.timestamp = new Date();
493
+ // Maintain proper stack trace (only available in V8 engines, e.g., Chrome and Node.js)
494
+ if (Error.captureStackTrace) {
495
+ Error.captureStackTrace(_this, TimeoutError);
496
+ }
497
+ return _this;
498
+ }
499
+ return TimeoutError;
500
+ }(Error);
501
+ function withTimeout(fn, timeout) {
502
+ return function () {
503
+ var args = [];
504
+ for (var _i = 0; _i < arguments.length; _i++) {
505
+ args[_i] = arguments[_i];
506
+ }
507
+ return new Promise(function (resolve, reject) {
508
+ var timer = setTimeout(function () {
509
+ reject(new TimeoutError("Function timed out (max: ".concat(timeout, "ms)")));
510
+ }, timeout);
511
+ fn.apply(void 0, args).then(resolve)["catch"](reject)["finally"](function () {
512
+ return clearTimeout(timer);
513
+ });
514
+ });
515
+ };
516
+ }
517
+
486
518
  var SKIPPED = Symbol();
487
519
  function batchFn(itemFn, _a) {
488
520
  var _b = _a === void 0 ? {} : _a,
@@ -490,7 +522,12 @@ function batchFn(itemFn, _a) {
490
522
  batchSize = _c === void 0 ? 10 : _c,
491
523
  skip = _b.skip,
492
524
  _d = _b.retry,
493
- retry = _d === void 0 ? false : _d;
525
+ retry = _d === void 0 ? false : _d,
526
+ timeout = _b.timeout;
527
+ //
528
+ // Optionally wrap itemFn into timeout
529
+ //
530
+ itemFn = timeout ? withTimeout(itemFn, timeout) : itemFn;
494
531
  return function batchExec(items) {
495
532
  var _this = this;
496
533
  var batches = arrayChunk(items, batchSize);
@@ -1485,4 +1522,4 @@ validateAsync.or = or;
1485
1522
  validateAsync.not = not;
1486
1523
  validateAsync.assertValid = assertValidAsync;
1487
1524
 
1488
- export { PromiseLikeEventEmitter, SKIPPED, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait };
1525
+ export { PromiseLikeEventEmitter, SKIPPED, TimeoutError, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait, withTimeout };
@@ -4,6 +4,7 @@ type BatchFnOptions = {
4
4
  skip?: (input: any) => Promise<boolean> | boolean;
5
5
  batchSize?: number;
6
6
  retry?: BackoffOptions | boolean;
7
+ timeout?: number | false;
7
8
  };
8
9
  type Batch = {
9
10
  index: number;
@@ -39,7 +40,7 @@ type EventTypes = {
39
40
  }) => void;
40
41
  error: (err: Error) => void;
41
42
  };
42
- export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
43
+ export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry, timeout }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
43
44
  type ParsedBatchResults = {
44
45
  results: any[];
45
46
  errors: Error[];
@@ -1,3 +1,4 @@
1
1
  export * from './promiseReduce';
2
2
  export * from './resolveNestedPromises';
3
3
  export * from './batchFn';
4
+ export * from './withTimeout';
@@ -0,0 +1,6 @@
1
+ export declare class TimeoutError extends Error {
2
+ code: string;
3
+ timestamp: Date;
4
+ constructor(message: string);
5
+ }
6
+ export declare function withTimeout<T extends (...args: any[]) => Promise<any>>(fn: T, timeout: number): (...args: Parameters<T>) => Promise<ReturnType<T>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@orioro/util",
3
- "version": "0.5.0",
3
+ "version": "0.6.0",
4
4
  "packageManager": "yarn@4.0.2",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",