@naturalcycles/js-lib 14.69.1 → 14.69.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.
@@ -21,6 +21,15 @@ export interface PQueueCfg {
21
21
  * If true - will LOG EVERYTHING:)
22
22
  */
23
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';
24
33
  }
25
34
  export declare type PromiseReturningFunction<R> = () => Promise<R>;
26
35
  /**
@@ -21,6 +21,7 @@ class PQueue {
21
21
  errorMode: errorMode_1.ErrorMode.THROW_IMMEDIATELY,
22
22
  logger: console,
23
23
  debug: false,
24
+ resolveOn: 'finish',
24
25
  ...cfg,
25
26
  };
26
27
  if (!cfg.debug) {
@@ -51,16 +52,24 @@ class PQueue {
51
52
  */
52
53
  push(fn_) {
53
54
  const { concurrency } = this.cfg;
55
+ const resolveOnStart = this.cfg.resolveOn === 'start';
54
56
  const fn = fn_;
55
57
  fn.defer || (fn.defer = (0, pDefer_1.pDefer)());
56
58
  if (this.inFlight < concurrency) {
57
59
  // There is room for more jobs. Can start immediately
58
60
  this.inFlight++;
59
61
  this.debug(`inFlight++ ${this.inFlight}/${concurrency}, queue ${this.queue.length}`);
62
+ if (resolveOnStart)
63
+ fn.defer.resolve();
60
64
  fn()
61
- .then(result => fn.defer.resolve(result))
65
+ .then(result => {
66
+ if (!resolveOnStart)
67
+ fn.defer.resolve(result);
68
+ })
62
69
  .catch(err => {
63
70
  this.cfg.logger.error(err);
71
+ if (resolveOnStart)
72
+ return;
64
73
  if (this.cfg.errorMode === errorMode_1.ErrorMode.SUPPRESS) {
65
74
  fn.defer.resolve(); // resolve with `void`
66
75
  }
@@ -15,7 +15,7 @@ export class PQueue {
15
15
  this.onIdleListeners = [];
16
16
  this.cfg = Object.assign({
17
17
  // concurrency: Number.MAX_SAFE_INTEGER,
18
- errorMode: ErrorMode.THROW_IMMEDIATELY, logger: console, debug: false }, cfg);
18
+ errorMode: ErrorMode.THROW_IMMEDIATELY, logger: console, debug: false, resolveOn: 'finish' }, cfg);
19
19
  if (!cfg.debug) {
20
20
  this.debug = () => { };
21
21
  }
@@ -44,16 +44,24 @@ export class PQueue {
44
44
  */
45
45
  push(fn_) {
46
46
  const { concurrency } = this.cfg;
47
+ const resolveOnStart = this.cfg.resolveOn === 'start';
47
48
  const fn = fn_;
48
49
  fn.defer || (fn.defer = pDefer());
49
50
  if (this.inFlight < concurrency) {
50
51
  // There is room for more jobs. Can start immediately
51
52
  this.inFlight++;
52
53
  this.debug(`inFlight++ ${this.inFlight}/${concurrency}, queue ${this.queue.length}`);
54
+ if (resolveOnStart)
55
+ fn.defer.resolve();
53
56
  fn()
54
- .then(result => fn.defer.resolve(result))
57
+ .then(result => {
58
+ if (!resolveOnStart)
59
+ fn.defer.resolve(result);
60
+ })
55
61
  .catch(err => {
56
62
  this.cfg.logger.error(err);
63
+ if (resolveOnStart)
64
+ return;
57
65
  if (this.cfg.errorMode === ErrorMode.SUPPRESS) {
58
66
  fn.defer.resolve(); // resolve with `void`
59
67
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.69.1",
3
+ "version": "14.69.2",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
@@ -33,6 +33,16 @@ export interface PQueueCfg {
33
33
  // logSizeChange?: boolean
34
34
 
35
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'
36
46
  }
37
47
 
38
48
  export type PromiseReturningFunction<R> = () => Promise<R>
@@ -56,6 +66,7 @@ export class PQueue {
56
66
  errorMode: ErrorMode.THROW_IMMEDIATELY,
57
67
  logger: console,
58
68
  debug: false,
69
+ resolveOn: 'finish',
59
70
  ...cfg,
60
71
  }
61
72
 
@@ -97,6 +108,7 @@ export class PQueue {
97
108
  */
98
109
  push<R>(fn_: PromiseReturningFunction<R>): Promise<R> {
99
110
  const { concurrency } = this.cfg
111
+ const resolveOnStart = this.cfg.resolveOn === 'start'
100
112
 
101
113
  const fn = fn_ as PromiseReturningFunctionWithDefer<R>
102
114
  fn.defer ||= pDefer<R>()
@@ -105,11 +117,16 @@ export class PQueue {
105
117
  // There is room for more jobs. Can start immediately
106
118
  this.inFlight++
107
119
  this.debug(`inFlight++ ${this.inFlight}/${concurrency}, queue ${this.queue.length}`)
120
+ if (resolveOnStart) fn.defer.resolve()
108
121
 
109
122
  fn()
110
- .then(result => fn.defer.resolve(result))
123
+ .then(result => {
124
+ if (!resolveOnStart) fn.defer.resolve(result)
125
+ })
111
126
  .catch(err => {
112
127
  this.cfg.logger.error(err)
128
+ if (resolveOnStart) return
129
+
113
130
  if (this.cfg.errorMode === ErrorMode.SUPPRESS) {
114
131
  fn.defer.resolve() // resolve with `void`
115
132
  } else {