@orioro/util 0.5.0 → 0.7.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,13 @@ 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
+ rollback = _b.rollback;
528
+ //
529
+ // Optionally wrap itemFn into timeout
530
+ //
531
+ itemFn = timeout ? withTimeout(itemFn, timeout) : itemFn;
494
532
  return function batchExec(items) {
495
533
  var _this = this;
496
534
  var batches = arrayChunk(items, batchSize);
@@ -513,11 +551,11 @@ function batchFn(itemFn, _a) {
513
551
  });
514
552
  return [4 /*yield*/, Promise.all(batchItems.map(function (item) {
515
553
  return __awaiter(_this, void 0, void 0, function () {
516
- var itemSkip, itemResult, _a, err_1;
554
+ var itemSkip, itemResult, _a, err_1, rollbackErr_1;
517
555
  return __generator(this, function (_b) {
518
556
  switch (_b.label) {
519
557
  case 0:
520
- _b.trys.push([0, 5,, 6]);
558
+ _b.trys.push([0, 5,, 10]);
521
559
  promise.emit('itemStart', {
522
560
  batch: batch,
523
561
  item: item
@@ -564,8 +602,21 @@ function batchFn(itemFn, _a) {
564
602
  item: item,
565
603
  result: err_1
566
604
  });
567
- return [2 /*return*/, err_1];
605
+ if (!(typeof rollback === 'function')) return [3 /*break*/, 9];
606
+ _b.label = 6;
568
607
  case 6:
608
+ _b.trys.push([6, 8,, 9]);
609
+ return [4 /*yield*/, rollback(item, err_1)];
610
+ case 7:
611
+ _b.sent();
612
+ return [3 /*break*/, 9];
613
+ case 8:
614
+ rollbackErr_1 = _b.sent();
615
+ console.error('Failure rolling back', rollbackErr_1);
616
+ return [3 /*break*/, 9];
617
+ case 9:
618
+ return [2 /*return*/, err_1];
619
+ case 10:
569
620
  return [2 /*return*/];
570
621
  }
571
622
  });
@@ -1485,4 +1536,4 @@ validateAsync.or = or;
1485
1536
  validateAsync.not = not;
1486
1537
  validateAsync.assertValid = assertValidAsync;
1487
1538
 
1488
- export { PromiseLikeEventEmitter, SKIPPED, ValidationError, batchFn, debugFn, deepFreeze, interpolate, makeTypeOf, maybeFn, parseBatchedResults, pickPaths, promiseReduce, resolveNestedPromises, switchExec, switchValue, typeMap, typeOf, validate, validateAsync, wait };
1539
+ 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,8 @@ type BatchFnOptions = {
4
4
  skip?: (input: any) => Promise<boolean> | boolean;
5
5
  batchSize?: number;
6
6
  retry?: BackoffOptions | boolean;
7
+ timeout?: number | false;
8
+ rollback?: (input: any, err: any) => Promise<any> | any;
7
9
  };
8
10
  type Batch = {
9
11
  index: number;
@@ -39,7 +41,7 @@ type EventTypes = {
39
41
  }) => void;
40
42
  error: (err: Error) => void;
41
43
  };
42
- export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
44
+ export declare function batchFn(itemFn: (input: any) => Promise<any> | any, { batchSize, skip, retry, timeout, rollback, }?: BatchFnOptions): (items: any[]) => PromiseLikeEventEmitter<any[], EventTypes>;
43
45
  type ParsedBatchResults = {
44
46
  results: any[];
45
47
  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.7.0",
4
4
  "packageManager": "yarn@4.0.2",
5
5
  "type": "module",
6
6
  "main": "dist/index.mjs",