@naturalcycles/js-lib 14.69.0 → 14.69.4

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.d.ts CHANGED
@@ -54,8 +54,8 @@ import { Class, ConditionalExcept, ConditionalPick, Merge, Promisable, PromiseVa
54
54
  import { AsyncMapper, AsyncPredicate, BaseDBEntity, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, Saved, Unsaved, BatchResult, InstanceId, IsoDate, IsoDateTime, KeyValueTuple, Mapper, ObjectMapper, ObjectPredicate, Predicate, PromiseMap, AnyObject, AnyFunction, Reviver, SavedDBEntity, StringMap, UnixTimestamp, ValueOf, ValuesOf, _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues } from './types';
55
55
  import { _gb, _hb, _kb, _mb } from './unit/size.util';
56
56
  import { is } from './vendor/is';
57
- import { CommonLogLevel, CommonLogFunction, CommonLogger, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix } from './log/commonLogger';
57
+ import { CommonLogLevel, CommonLogFunction, CommonLogger, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, CommonLogWithLevelFunction, commonLoggerCreate } from './log/commonLogger';
58
58
  import { _safeJsonStringify } from './string/safeJsonStringify';
59
59
  import { PQueue, PQueueCfg } from './promise/pQueue';
60
- export type { PQueueCfg, MemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, PromiseValue, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestamp, BaseDBEntity, SavedDBEntity, Saved, Unsaved, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogFunction, CommonLogger, };
61
- export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, _defineLazyProperty, _defineLazyProps, _lazyValue, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, _safeJsonStringify, PQueue, };
60
+ export type { PQueueCfg, MemoCache, PromiseDecoratorCfg, PromiseDecoratorResp, ErrorData, ErrorObject, HttpErrorData, HttpErrorResponse, Admin401ErrorData, Admin403ErrorData, StringMap, PromiseMap, AnyObject, AnyFunction, ValuesOf, ValueOf, KeyValueTuple, ObjectMapper, ObjectPredicate, InstanceId, IsoDate, IsoDateTime, Reviver, PMapOptions, Mapper, AsyncMapper, Predicate, AsyncPredicate, BatchResult, DeferredPromise, PRetryOptions, PTimeoutOptions, TryCatchOptions, StringifyAnyOptions, JsonStringifyFunction, Merge, ReadonlyDeep, Promisable, PromiseValue, Simplify, ConditionalPick, ConditionalExcept, Class, UnixTimestamp, BaseDBEntity, SavedDBEntity, Saved, Unsaved, CreatedUpdated, CreatedUpdatedId, ObjectWithId, AnyObjectWithId, JsonSchema, JsonSchemaAny, JsonSchemaOneOf, JsonSchemaAllOf, JsonSchemaAnyOf, JsonSchemaNot, JsonSchemaRef, JsonSchemaConst, JsonSchemaEnum, JsonSchemaString, JsonSchemaNumber, JsonSchemaBoolean, JsonSchemaNull, JsonSchemaRootObject, JsonSchemaObject, JsonSchemaArray, JsonSchemaTuple, JsonSchemaBuilder, CommonLogLevel, CommonLogWithLevelFunction, CommonLogFunction, CommonLogger, };
61
+ export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, _defineLazyProperty, _defineLazyProps, _lazyValue, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, _safeJsonStringify, PQueue, };
package/dist/index.js CHANGED
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports._isObject = exports._undefinedIfEmpty = exports._filterObject = exports._filterEmptyValues = exports._filterEmptyArrays = exports._filterNullishValues = exports._filterUndefinedValues = exports._filterFalsyValues = exports._omit = exports._pick = exports._truncateMiddle = exports._truncate = exports._nl2br = exports._replaceAll = exports._substringBetweenLast = exports._substringAfterLast = exports._substringAfter = exports._substringBeforeLast = exports._substringBefore = exports._removeWhitespace = exports._split = exports._lowerFirst = exports._upperFirst = exports._capitalize = exports._objectKeys = exports._stringMapEntries = exports._stringMapValues = exports._inRange = exports._createDeterministicRandom = exports._randomArrayItem = exports._randomInt = exports._assertTypeOf = exports._assertIsNumber = exports._assertIsString = exports._assertIsError = exports._assertDeepEquals = exports._assertEquals = exports._assert = exports._isHttpErrorResponse = exports._isHttpErrorObject = exports._isErrorObject = exports.AssertionError = exports.HttpError = exports.AppError = exports._createPromiseDecorator = exports._getArgsSignature = exports._LogMethod = exports._memoFn = exports._Memo = exports.is = void 0;
4
4
  exports._average = exports.SimpleMovingAverage = exports._chunk = exports._flattenDeep = exports._flatten = exports._uniqBy = exports._uniq = exports._range = exports._errorObjectToAppError = exports._errorToErrorObject = exports._anyToErrorObject = exports._anyToError = exports._findKeyByValue = exports._mapToObject = exports._shuffle = exports._difference = exports._intersection = exports._countBy = exports._dropRightWhile = exports._dropWhile = exports._takeRightWhile = exports._takeWhile = exports._findLast = exports._round = exports._toPrecision = exports._toFixed = exports._sortNumbers = exports._sortBy = exports._groupBy = exports._by = exports._invertMap = exports._invert = exports._mask = exports._unset = exports._has = exports._set = exports._get = exports._sortObject = exports._sortObjectDeep = exports._deepTrim = exports._merge = exports._isEmpty = exports._isEmptyObject = exports._deepCopy = exports._deepEquals = exports._objectNullValuesToUndefined = exports._mapObject = exports._mapValues = exports._mapKeys = exports._isPrimitive = void 0;
5
5
  exports.JSON_SCHEMA_ORDER = exports.JsonSchemaAnyBuilder = exports.jsonSchema = exports.mergeJsonSchemaObjects = exports._last = exports._clamp = exports._sumBy = exports._sum = exports._kebabCase = exports._camelCase = exports._snakeCase = exports._kb = exports._mb = exports._gb = exports._hb = exports._since = exports._ms = exports._stringifyAny = exports._jsonParseIfPossible = exports.pTry = exports._try = exports._TryCatch = exports._tryCatch = exports._Timeout = exports._Retry = exports.pTuple = exports.pTimeout = exports.pRetry = exports.AggregatedError = exports.pState = exports.pHang = exports.pDefer = exports.pDelay = exports.pProps = exports.pFilter = exports.ErrorMode = exports.pBatch = exports._noop = exports._passNothingPredicate = exports._passthroughPredicate = exports._passUndefinedMapper = exports._passthroughMapper = exports.pMap = exports._Throttle = exports._Debounce = exports._throttle = exports._debounce = exports._median = exports._percentile = exports._averageWeighted = void 0;
6
- exports.PQueue = exports._safeJsonStringify = exports.commonLoggerPrefix = exports.commonLoggerPipe = exports.commonLogLevelNumber = exports.commonLoggerNoop = exports.commonLoggerMinLevel = exports._lazyValue = exports._defineLazyProps = exports._defineLazyProperty = exports._parseQueryString = exports.generateJsonSchemaFromData = void 0;
6
+ exports.PQueue = exports._safeJsonStringify = exports.commonLoggerCreate = exports.commonLoggerPrefix = exports.commonLoggerPipe = exports.commonLogLevelNumber = exports.commonLoggerNoop = exports.commonLoggerMinLevel = exports._lazyValue = exports._defineLazyProps = exports._defineLazyProperty = exports._parseQueryString = exports.generateJsonSchemaFromData = void 0;
7
7
  const array_util_1 = require("./array/array.util");
8
8
  Object.defineProperty(exports, "_by", { enumerable: true, get: function () { return array_util_1._by; } });
9
9
  Object.defineProperty(exports, "_chunk", { enumerable: true, get: function () { return array_util_1._chunk; } });
@@ -217,6 +217,7 @@ Object.defineProperty(exports, "commonLoggerNoop", { enumerable: true, get: func
217
217
  Object.defineProperty(exports, "commonLogLevelNumber", { enumerable: true, get: function () { return commonLogger_1.commonLogLevelNumber; } });
218
218
  Object.defineProperty(exports, "commonLoggerPipe", { enumerable: true, get: function () { return commonLogger_1.commonLoggerPipe; } });
219
219
  Object.defineProperty(exports, "commonLoggerPrefix", { enumerable: true, get: function () { return commonLogger_1.commonLoggerPrefix; } });
220
+ Object.defineProperty(exports, "commonLoggerCreate", { enumerable: true, get: function () { return commonLogger_1.commonLoggerCreate; } });
220
221
  const safeJsonStringify_1 = require("./string/safeJsonStringify");
221
222
  Object.defineProperty(exports, "_safeJsonStringify", { enumerable: true, get: function () { return safeJsonStringify_1._safeJsonStringify; } });
222
223
  const pQueue_1 = require("./promise/pQueue");
@@ -17,6 +17,7 @@ export declare const commonLogLevelNumber: Record<CommonLogLevel, number>;
17
17
  * @experimental
18
18
  */
19
19
  export declare type CommonLogFunction = (...args: any[]) => void;
20
+ export declare type CommonLogWithLevelFunction = (level: CommonLogLevel, args: any[]) => void;
20
21
  /**
21
22
  * Interface is inspired/compatible with `console.*`
22
23
  * So, `console` is a valid CommonLogger implementation as-is.
@@ -46,3 +47,7 @@ export declare function commonLoggerPipe(loggers: CommonLogger[]): CommonLogger;
46
47
  * Creates a "child" CommonLogger with prefix (one or multiple).
47
48
  */
48
49
  export declare function commonLoggerPrefix(logger: CommonLogger, ...prefixes: any[]): CommonLogger;
50
+ /**
51
+ * Creates a CommonLogger from a single function that takes `level` and `args`.
52
+ */
53
+ export declare function commonLoggerCreate(fn: CommonLogWithLevelFunction): CommonLogger;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.commonLoggerPrefix = exports.commonLoggerPipe = exports.commonLoggerMinLevel = exports.commonLoggerNoop = exports.commonLogLevelNumber = void 0;
3
+ exports.commonLoggerCreate = exports.commonLoggerPrefix = exports.commonLoggerPipe = exports.commonLoggerMinLevel = exports.commonLoggerNoop = exports.commonLogLevelNumber = void 0;
4
4
  const index_1 = require("../index");
5
5
  exports.commonLogLevelNumber = {
6
6
  log: 10,
@@ -71,3 +71,14 @@ function commonLoggerPrefix(logger, ...prefixes) {
71
71
  };
72
72
  }
73
73
  exports.commonLoggerPrefix = commonLoggerPrefix;
74
+ /**
75
+ * Creates a CommonLogger from a single function that takes `level` and `args`.
76
+ */
77
+ function commonLoggerCreate(fn) {
78
+ return {
79
+ log: (...args) => fn('log', args),
80
+ warn: (...args) => fn('warn', args),
81
+ error: (...args) => fn('error', args),
82
+ };
83
+ }
84
+ exports.commonLoggerCreate = commonLoggerCreate;
@@ -1,9 +1,15 @@
1
+ import { ErrorMode } from '../error/errorMode';
1
2
  import { CommonLogger } from '../log/commonLogger';
2
3
  export interface PQueueCfg {
3
4
  concurrency: number;
4
5
  /**
5
6
  * Default: THROW_IMMEDIATELY
7
+ *
8
+ * THROW_AGGREGATED is not supported.
9
+ *
10
+ * SUPPRESS_ERRORS will still log errors via logger. It will resolve the `.push` promise with void.
6
11
  */
12
+ errorMode?: ErrorMode;
7
13
  /**
8
14
  * @default true
9
15
  */
@@ -15,8 +21,17 @@ export interface PQueueCfg {
15
21
  * If true - will LOG EVERYTHING:)
16
22
  */
17
23
  debug?: boolean;
24
+ /**
25
+ * By default .push method resolves when the Promise is done (finished).
26
+ *
27
+ * If you set resolveOn = 'start' - .push method will resolve the Promise (with void) upon
28
+ * the START of the processing.
29
+ *
30
+ * @default finish
31
+ */
32
+ resolveOn?: 'finish' | 'start';
18
33
  }
19
- export declare type PromiseReturningFunction = () => Promise<any>;
34
+ export declare type PromiseReturningFunction<R> = () => Promise<R>;
20
35
  /**
21
36
  * Inspired by: https://github.com/sindresorhus/p-queue
22
37
  *
@@ -39,5 +54,9 @@ export declare class PQueue {
39
54
  * Idle means 0 queue and 0 inFlight.
40
55
  */
41
56
  onIdle(): Promise<void>;
42
- push(fn: PromiseReturningFunction): void;
57
+ /**
58
+ * Push PromiseReturningFunction to the Queue.
59
+ * Returns a Promise that resolves (or rejects) with the return value from the Promise.
60
+ */
61
+ push<R>(fn_: PromiseReturningFunction<R>): Promise<R>;
43
62
  }
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.PQueue = void 0;
4
+ const errorMode_1 = require("../error/errorMode");
4
5
  const pDefer_1 = require("./pDefer");
5
6
  /**
6
7
  * Inspired by: https://github.com/sindresorhus/p-queue
@@ -17,8 +18,10 @@ class PQueue {
17
18
  this.onIdleListeners = [];
18
19
  this.cfg = {
19
20
  // concurrency: Number.MAX_SAFE_INTEGER,
21
+ errorMode: errorMode_1.ErrorMode.THROW_IMMEDIATELY,
20
22
  logger: console,
21
23
  debug: false,
24
+ resolveOn: 'finish',
22
25
  ...cfg,
23
26
  };
24
27
  if (!cfg.debug) {
@@ -37,21 +40,43 @@ class PQueue {
37
40
  * Idle means 0 queue and 0 inFlight.
38
41
  */
39
42
  onIdle() {
40
- if (this.queue.length === 0)
43
+ if (this.queue.length === 0 && this.inFlight === 0)
41
44
  return Promise.resolve();
42
45
  const listener = (0, pDefer_1.pDefer)();
43
46
  this.onIdleListeners.push(listener);
44
47
  return listener;
45
48
  }
46
- push(fn) {
49
+ /**
50
+ * Push PromiseReturningFunction to the Queue.
51
+ * Returns a Promise that resolves (or rejects) with the return value from the Promise.
52
+ */
53
+ push(fn_) {
47
54
  const { concurrency } = this.cfg;
55
+ const resolveOnStart = this.cfg.resolveOn === 'start';
56
+ const fn = fn_;
57
+ fn.defer || (fn.defer = (0, pDefer_1.pDefer)());
48
58
  if (this.inFlight < concurrency) {
49
59
  // There is room for more jobs. Can start immediately
50
60
  this.inFlight++;
51
61
  this.debug(`inFlight++ ${this.inFlight}/${concurrency}, queue ${this.queue.length}`);
62
+ if (resolveOnStart)
63
+ fn.defer.resolve();
52
64
  fn()
65
+ .then(result => {
66
+ if (!resolveOnStart)
67
+ fn.defer.resolve(result);
68
+ })
53
69
  .catch(err => {
54
70
  this.cfg.logger.error(err);
71
+ if (resolveOnStart)
72
+ return;
73
+ if (this.cfg.errorMode === errorMode_1.ErrorMode.SUPPRESS) {
74
+ fn.defer.resolve(); // resolve with `void`
75
+ }
76
+ else {
77
+ // Should be handled on the outside, otherwise it'll cause UnhandledRejection
78
+ fn.defer.reject(err);
79
+ }
55
80
  })
56
81
  .finally(() => {
57
82
  this.inFlight--;
@@ -59,7 +84,7 @@ class PQueue {
59
84
  // check if there's room to start next job
60
85
  if (this.queue.length && this.inFlight <= concurrency) {
61
86
  const nextFn = this.queue.shift();
62
- this.push(nextFn);
87
+ void this.push(nextFn);
63
88
  }
64
89
  else {
65
90
  if (this.inFlight === 0) {
@@ -74,6 +99,7 @@ class PQueue {
74
99
  this.queue.push(fn);
75
100
  this.debug(`inFlight ${this.inFlight}/${concurrency}, queue++ ${this.queue.length}`);
76
101
  }
102
+ return fn.defer;
77
103
  }
78
104
  }
79
105
  exports.PQueue = PQueue;
package/dist-esm/index.js CHANGED
@@ -50,7 +50,7 @@ import { _ms, _since } from './time/time.util';
50
50
  import { _noop, _objectKeys, _passNothingPredicate, _passthroughMapper, _passthroughPredicate, _passUndefinedMapper, _stringMapEntries, _stringMapValues, } from './types';
51
51
  import { _gb, _hb, _kb, _mb } from './unit/size.util';
52
52
  import { is } from './vendor/is';
53
- import { commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, } from './log/commonLogger';
53
+ import { commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, } from './log/commonLogger';
54
54
  import { _safeJsonStringify } from './string/safeJsonStringify';
55
55
  import { PQueue } from './promise/pQueue';
56
- export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, _defineLazyProperty, _defineLazyProps, _lazyValue, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, _safeJsonStringify, PQueue, };
56
+ export { is, _Memo, _memoFn, _LogMethod, _getArgsSignature, _createPromiseDecorator, AppError, HttpError, AssertionError, _isErrorObject, _isHttpErrorObject, _isHttpErrorResponse, _assert, _assertEquals, _assertDeepEquals, _assertIsError, _assertIsString, _assertIsNumber, _assertTypeOf, _randomInt, _randomArrayItem, _createDeterministicRandom, _inRange, _stringMapValues, _stringMapEntries, _objectKeys, _capitalize, _upperFirst, _lowerFirst, _split, _removeWhitespace, _substringBefore, _substringBeforeLast, _substringAfter, _substringAfterLast, _substringBetweenLast, _replaceAll, _nl2br, _truncate, _truncateMiddle, _pick, _omit, _filterFalsyValues, _filterUndefinedValues, _filterNullishValues, _filterEmptyArrays, _filterEmptyValues, _filterObject, _undefinedIfEmpty, _isObject, _isPrimitive, _mapKeys, _mapValues, _mapObject, _objectNullValuesToUndefined, _deepEquals, _deepCopy, _isEmptyObject, _isEmpty, _merge, _deepTrim, _sortObjectDeep, _sortObject, _get, _set, _has, _unset, _mask, _invert, _invertMap, _by, _groupBy, _sortBy, _sortNumbers, _toFixed, _toPrecision, _round, _findLast, _takeWhile, _takeRightWhile, _dropWhile, _dropRightWhile, _countBy, _intersection, _difference, _shuffle, _mapToObject, _findKeyByValue, _anyToError, _anyToErrorObject, _errorToErrorObject, _errorObjectToAppError, _range, _uniq, _uniqBy, _flatten, _flattenDeep, _chunk, SimpleMovingAverage, _average, _averageWeighted, _percentile, _median, _debounce, _throttle, _Debounce, _Throttle, pMap, _passthroughMapper, _passUndefinedMapper, _passthroughPredicate, _passNothingPredicate, _noop, pBatch, ErrorMode, pFilter, pProps, pDelay, pDefer, pHang, pState, AggregatedError, pRetry, pTimeout, pTuple, _Retry, _Timeout, _tryCatch, _TryCatch, _try, pTry, _jsonParseIfPossible, _stringifyAny, _ms, _since, _hb, _gb, _mb, _kb, _snakeCase, _camelCase, _kebabCase, _sum, _sumBy, _clamp, _last, mergeJsonSchemaObjects, jsonSchema, JsonSchemaAnyBuilder, JSON_SCHEMA_ORDER, generateJsonSchemaFromData, _parseQueryString, _defineLazyProperty, _defineLazyProps, _lazyValue, commonLoggerMinLevel, commonLoggerNoop, commonLogLevelNumber, commonLoggerPipe, commonLoggerPrefix, commonLoggerCreate, _safeJsonStringify, PQueue, };
@@ -65,3 +65,13 @@ export function commonLoggerPrefix(logger, ...prefixes) {
65
65
  error: (...args) => logger.error(...prefixes, ...args),
66
66
  };
67
67
  }
68
+ /**
69
+ * Creates a CommonLogger from a single function that takes `level` and `args`.
70
+ */
71
+ export function commonLoggerCreate(fn) {
72
+ return {
73
+ log: (...args) => fn('log', args),
74
+ warn: (...args) => fn('warn', args),
75
+ error: (...args) => fn('error', args),
76
+ };
77
+ }
@@ -1,3 +1,4 @@
1
+ import { ErrorMode } from '../error/errorMode';
1
2
  import { pDefer } from './pDefer';
2
3
  /**
3
4
  * Inspired by: https://github.com/sindresorhus/p-queue
@@ -14,7 +15,7 @@ export class PQueue {
14
15
  this.onIdleListeners = [];
15
16
  this.cfg = Object.assign({
16
17
  // concurrency: Number.MAX_SAFE_INTEGER,
17
- logger: console, debug: false }, cfg);
18
+ errorMode: ErrorMode.THROW_IMMEDIATELY, logger: console, debug: false, resolveOn: 'finish' }, cfg);
18
19
  if (!cfg.debug) {
19
20
  this.debug = () => { };
20
21
  }
@@ -31,21 +32,43 @@ export class PQueue {
31
32
  * Idle means 0 queue and 0 inFlight.
32
33
  */
33
34
  onIdle() {
34
- if (this.queue.length === 0)
35
+ if (this.queue.length === 0 && this.inFlight === 0)
35
36
  return Promise.resolve();
36
37
  const listener = pDefer();
37
38
  this.onIdleListeners.push(listener);
38
39
  return listener;
39
40
  }
40
- push(fn) {
41
+ /**
42
+ * Push PromiseReturningFunction to the Queue.
43
+ * Returns a Promise that resolves (or rejects) with the return value from the Promise.
44
+ */
45
+ push(fn_) {
41
46
  const { concurrency } = this.cfg;
47
+ const resolveOnStart = this.cfg.resolveOn === 'start';
48
+ const fn = fn_;
49
+ fn.defer || (fn.defer = pDefer());
42
50
  if (this.inFlight < concurrency) {
43
51
  // There is room for more jobs. Can start immediately
44
52
  this.inFlight++;
45
53
  this.debug(`inFlight++ ${this.inFlight}/${concurrency}, queue ${this.queue.length}`);
54
+ if (resolveOnStart)
55
+ fn.defer.resolve();
46
56
  fn()
57
+ .then(result => {
58
+ if (!resolveOnStart)
59
+ fn.defer.resolve(result);
60
+ })
47
61
  .catch(err => {
48
62
  this.cfg.logger.error(err);
63
+ if (resolveOnStart)
64
+ return;
65
+ if (this.cfg.errorMode === ErrorMode.SUPPRESS) {
66
+ fn.defer.resolve(); // resolve with `void`
67
+ }
68
+ else {
69
+ // Should be handled on the outside, otherwise it'll cause UnhandledRejection
70
+ fn.defer.reject(err);
71
+ }
49
72
  })
50
73
  .finally(() => {
51
74
  this.inFlight--;
@@ -53,7 +76,7 @@ export class PQueue {
53
76
  // check if there's room to start next job
54
77
  if (this.queue.length && this.inFlight <= concurrency) {
55
78
  const nextFn = this.queue.shift();
56
- this.push(nextFn);
79
+ void this.push(nextFn);
57
80
  }
58
81
  else {
59
82
  if (this.inFlight === 0) {
@@ -68,5 +91,6 @@ export class PQueue {
68
91
  this.queue.push(fn);
69
92
  this.debug(`inFlight ${this.inFlight}/${concurrency}, queue++ ${this.queue.length}`);
70
93
  }
94
+ return fn.defer;
71
95
  }
72
96
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.69.0",
3
+ "version": "14.69.4",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
package/src/index.ts CHANGED
@@ -233,6 +233,8 @@ import {
233
233
  commonLogLevelNumber,
234
234
  commonLoggerPipe,
235
235
  commonLoggerPrefix,
236
+ CommonLogWithLevelFunction,
237
+ commonLoggerCreate,
236
238
  } from './log/commonLogger'
237
239
  import { _safeJsonStringify } from './string/safeJsonStringify'
238
240
  import { PQueue, PQueueCfg } from './promise/pQueue'
@@ -309,6 +311,7 @@ export type {
309
311
  JsonSchemaTuple,
310
312
  JsonSchemaBuilder,
311
313
  CommonLogLevel,
314
+ CommonLogWithLevelFunction,
312
315
  CommonLogFunction,
313
316
  CommonLogger,
314
317
  }
@@ -474,6 +477,7 @@ export {
474
477
  commonLogLevelNumber,
475
478
  commonLoggerPipe,
476
479
  commonLoggerPrefix,
480
+ commonLoggerCreate,
477
481
  _safeJsonStringify,
478
482
  PQueue,
479
483
  }
@@ -25,6 +25,7 @@ export const commonLogLevelNumber: Record<CommonLogLevel, number> = {
25
25
  * @experimental
26
26
  */
27
27
  export type CommonLogFunction = (...args: any[]) => void
28
+ export type CommonLogWithLevelFunction = (level: CommonLogLevel, args: any[]) => void
28
29
 
29
30
  /**
30
31
  * Interface is inspired/compatible with `console.*`
@@ -109,3 +110,14 @@ export function commonLoggerPrefix(logger: CommonLogger, ...prefixes: any[]): Co
109
110
  error: (...args) => logger.error(...prefixes, ...args),
110
111
  }
111
112
  }
113
+
114
+ /**
115
+ * Creates a CommonLogger from a single function that takes `level` and `args`.
116
+ */
117
+ export function commonLoggerCreate(fn: CommonLogWithLevelFunction): CommonLogger {
118
+ return {
119
+ log: (...args) => fn('log', args),
120
+ warn: (...args) => fn('warn', args),
121
+ error: (...args) => fn('error', args),
122
+ }
123
+ }
@@ -1,3 +1,4 @@
1
+ import { ErrorMode } from '../error/errorMode'
1
2
  import { CommonLogger } from '../log/commonLogger'
2
3
  import { DeferredPromise, pDefer } from './pDefer'
3
4
 
@@ -6,8 +7,12 @@ export interface PQueueCfg {
6
7
 
7
8
  /**
8
9
  * Default: THROW_IMMEDIATELY
10
+ *
11
+ * THROW_AGGREGATED is not supported.
12
+ *
13
+ * SUPPRESS_ERRORS will still log errors via logger. It will resolve the `.push` promise with void.
9
14
  */
10
- // errorMode?: ErrorMode
15
+ errorMode?: ErrorMode
11
16
 
12
17
  /**
13
18
  * @default true
@@ -28,9 +33,23 @@ export interface PQueueCfg {
28
33
  // logSizeChange?: boolean
29
34
 
30
35
  // timeout
36
+
37
+ /**
38
+ * By default .push method resolves when the Promise is done (finished).
39
+ *
40
+ * If you set resolveOn = 'start' - .push method will resolve the Promise (with void) upon
41
+ * the START of the processing.
42
+ *
43
+ * @default finish
44
+ */
45
+ resolveOn?: 'finish' | 'start'
31
46
  }
32
47
 
33
- export type PromiseReturningFunction = () => Promise<any>
48
+ export type PromiseReturningFunction<R> = () => Promise<R>
49
+
50
+ interface PromiseReturningFunctionWithDefer<R> extends PromiseReturningFunction<R> {
51
+ defer: DeferredPromise<R>
52
+ }
34
53
 
35
54
  /**
36
55
  * Inspired by: https://github.com/sindresorhus/p-queue
@@ -44,8 +63,10 @@ export class PQueue {
44
63
  constructor(cfg: PQueueCfg) {
45
64
  this.cfg = {
46
65
  // concurrency: Number.MAX_SAFE_INTEGER,
66
+ errorMode: ErrorMode.THROW_IMMEDIATELY,
47
67
  logger: console,
48
68
  debug: false,
69
+ resolveOn: 'finish',
49
70
  ...cfg,
50
71
  }
51
72
 
@@ -61,7 +82,7 @@ export class PQueue {
61
82
  }
62
83
 
63
84
  inFlight = 0
64
- private queue: PromiseReturningFunction[] = []
85
+ private queue: PromiseReturningFunction<any>[] = []
65
86
  private onIdleListeners: DeferredPromise[] = []
66
87
 
67
88
  get queueSize(): number {
@@ -74,24 +95,44 @@ export class PQueue {
74
95
  * Idle means 0 queue and 0 inFlight.
75
96
  */
76
97
  onIdle(): Promise<void> {
77
- if (this.queue.length === 0) return Promise.resolve()
98
+ if (this.queue.length === 0 && this.inFlight === 0) return Promise.resolve()
78
99
 
79
100
  const listener = pDefer()
80
101
  this.onIdleListeners.push(listener)
81
102
  return listener
82
103
  }
83
104
 
84
- push(fn: PromiseReturningFunction): void {
105
+ /**
106
+ * Push PromiseReturningFunction to the Queue.
107
+ * Returns a Promise that resolves (or rejects) with the return value from the Promise.
108
+ */
109
+ push<R>(fn_: PromiseReturningFunction<R>): Promise<R> {
85
110
  const { concurrency } = this.cfg
111
+ const resolveOnStart = this.cfg.resolveOn === 'start'
112
+
113
+ const fn = fn_ as PromiseReturningFunctionWithDefer<R>
114
+ fn.defer ||= pDefer<R>()
86
115
 
87
116
  if (this.inFlight < concurrency) {
88
117
  // There is room for more jobs. Can start immediately
89
118
  this.inFlight++
90
119
  this.debug(`inFlight++ ${this.inFlight}/${concurrency}, queue ${this.queue.length}`)
120
+ if (resolveOnStart) fn.defer.resolve()
91
121
 
92
122
  fn()
123
+ .then(result => {
124
+ if (!resolveOnStart) fn.defer.resolve(result)
125
+ })
93
126
  .catch(err => {
94
127
  this.cfg.logger.error(err)
128
+ if (resolveOnStart) return
129
+
130
+ if (this.cfg.errorMode === ErrorMode.SUPPRESS) {
131
+ fn.defer.resolve() // resolve with `void`
132
+ } else {
133
+ // Should be handled on the outside, otherwise it'll cause UnhandledRejection
134
+ fn.defer.reject(err)
135
+ }
95
136
  })
96
137
  .finally(() => {
97
138
  this.inFlight--
@@ -100,7 +141,7 @@ export class PQueue {
100
141
  // check if there's room to start next job
101
142
  if (this.queue.length && this.inFlight <= concurrency) {
102
143
  const nextFn = this.queue.shift()!
103
- this.push(nextFn)
144
+ void this.push(nextFn)
104
145
  } else {
105
146
  if (this.inFlight === 0) {
106
147
  this.debug('onIdle')
@@ -113,5 +154,7 @@ export class PQueue {
113
154
  this.queue.push(fn)
114
155
  this.debug(`inFlight ${this.inFlight}/${concurrency}, queue++ ${this.queue.length}`)
115
156
  }
157
+
158
+ return fn.defer
116
159
  }
117
160
  }