@naturalcycles/js-lib 14.200.0 → 14.201.1

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,24 @@
1
+ /**
2
+ * Like AbortSignal, but it can "abort itself" via the `.abort()` method.
3
+ *
4
+ * Similar to how DeferredPromise is both a Promise and has `.resolve()` and `.reject()` methods.
5
+ *
6
+ * This is to simplify the AbortController/AbortSignal usage.
7
+ *
8
+ * Before this - you need to keep track of 2 things: AbortController and AbortSignal.
9
+ *
10
+ * After - you are good with only AbortableSignal, which can do both.
11
+ * And it's compatible with AbortSignal (because it extends it).
12
+ *
13
+ * @experimental
14
+ */
15
+ export interface AbortableSignal extends AbortSignal {
16
+ abort: AbortController['abort'];
17
+ }
18
+ /**
19
+ * Creates AbortableSignal,
20
+ * which is like AbortSignal, but can "abort itself" with `.abort()` method.
21
+ *
22
+ * @experimental
23
+ */
24
+ export declare function createAbortableSignal(): AbortableSignal;
package/dist/abort.js ADDED
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.createAbortableSignal = void 0;
4
+ /**
5
+ * Creates AbortableSignal,
6
+ * which is like AbortSignal, but can "abort itself" with `.abort()` method.
7
+ *
8
+ * @experimental
9
+ */
10
+ function createAbortableSignal() {
11
+ const ac = new AbortController();
12
+ return Object.assign(ac.signal, {
13
+ abort: ac.abort.bind(ac),
14
+ });
15
+ }
16
+ exports.createAbortableSignal = createAbortableSignal;
package/dist/index.d.ts CHANGED
@@ -81,6 +81,7 @@ export * from './env/buildInfo';
81
81
  export * from './form.util';
82
82
  export * from './semver';
83
83
  export * from './web';
84
+ export * from './abort';
84
85
  export * from './polyfill';
85
86
  export * from './zod/zod.util';
86
87
  export * from './zod/zod.shared.schemas';
package/dist/index.js CHANGED
@@ -85,6 +85,7 @@ tslib_1.__exportStar(require("./env/buildInfo"), exports);
85
85
  tslib_1.__exportStar(require("./form.util"), exports);
86
86
  tslib_1.__exportStar(require("./semver"), exports);
87
87
  tslib_1.__exportStar(require("./web"), exports);
88
+ tslib_1.__exportStar(require("./abort"), exports);
88
89
  tslib_1.__exportStar(require("./polyfill"), exports);
89
90
  tslib_1.__exportStar(require("./zod/zod.util"), exports);
90
91
  tslib_1.__exportStar(require("./zod/zod.shared.schemas"), exports);
@@ -1,4 +1,4 @@
1
- import type { AbortableAsyncMapper } from '..';
1
+ import type { AbortableAsyncMapper, CommonLogger } from '..';
2
2
  import { ErrorMode } from '..';
3
3
  export interface PMapOptions {
4
4
  /**
@@ -13,8 +13,14 @@ export interface PMapOptions {
13
13
  * When set to THROW_AGGREGATED, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an Aggregated error.
14
14
  *
15
15
  * When set to SUPPRESS - will ignore errors and return results from successful operations.
16
+ * When in SUPPRESS - errors are still logged via the `logger` (which defaults to console).
16
17
  */
17
18
  errorMode?: ErrorMode;
19
+ /**
20
+ * Default to `console`.
21
+ * Pass `null` to skip logging completely.
22
+ */
23
+ logger?: CommonLogger | null;
18
24
  }
19
25
  /**
20
26
  * Returns a `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled,
@@ -37,6 +37,7 @@ const __1 = require("..");
37
37
  * })();
38
38
  */
39
39
  async function pMap(iterable, mapper, opt = {}) {
40
+ const { logger = console } = opt;
40
41
  const ret = [];
41
42
  // const iterator = iterable[Symbol.iterator]()
42
43
  const items = [...iterable];
@@ -65,7 +66,10 @@ async function pMap(iterable, mapper, opt = {}) {
65
66
  if (errorMode === __1.ErrorMode.THROW_AGGREGATED) {
66
67
  errors.push(err);
67
68
  }
68
- // otherwise, suppress completely
69
+ else {
70
+ // otherwise, suppress (but still log via logger)
71
+ logger?.error(err);
72
+ }
69
73
  }
70
74
  }
71
75
  if (errors.length) {
@@ -87,6 +91,10 @@ async function pMap(iterable, mapper, opt = {}) {
87
91
  else if (errorMode === __1.ErrorMode.THROW_AGGREGATED) {
88
92
  errors.push(r.reason);
89
93
  }
94
+ else {
95
+ // otherwise, suppress (but still log via logger)
96
+ logger?.error(r.reason);
97
+ }
90
98
  });
91
99
  if (errors.length) {
92
100
  throw new AggregateError(errors, `pMap resulted in ${errors.length} error(s)`);
@@ -133,6 +141,10 @@ async function pMap(iterable, mapper, opt = {}) {
133
141
  if (errorMode === __1.ErrorMode.THROW_AGGREGATED) {
134
142
  errors.push(err);
135
143
  }
144
+ else {
145
+ // otherwise, suppress (but still log via logger)
146
+ logger?.error(err);
147
+ }
136
148
  resolvingCount--;
137
149
  next();
138
150
  }
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Creates AbortableSignal,
3
+ * which is like AbortSignal, but can "abort itself" with `.abort()` method.
4
+ *
5
+ * @experimental
6
+ */
7
+ export function createAbortableSignal() {
8
+ const ac = new AbortController();
9
+ return Object.assign(ac.signal, {
10
+ abort: ac.abort.bind(ac),
11
+ });
12
+ }
package/dist-esm/index.js CHANGED
@@ -81,6 +81,7 @@ export * from './env/buildInfo';
81
81
  export * from './form.util';
82
82
  export * from './semver';
83
83
  export * from './web';
84
+ export * from './abort';
84
85
  export * from './polyfill';
85
86
  export * from './zod/zod.util';
86
87
  export * from './zod/zod.shared.schemas';
@@ -34,6 +34,7 @@ import { END, ErrorMode, SKIP } from '..';
34
34
  * })();
35
35
  */
36
36
  export async function pMap(iterable, mapper, opt = {}) {
37
+ const { logger = console } = opt;
37
38
  const ret = [];
38
39
  // const iterator = iterable[Symbol.iterator]()
39
40
  const items = [...iterable];
@@ -62,7 +63,10 @@ export async function pMap(iterable, mapper, opt = {}) {
62
63
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
63
64
  errors.push(err);
64
65
  }
65
- // otherwise, suppress completely
66
+ else {
67
+ // otherwise, suppress (but still log via logger)
68
+ logger === null || logger === void 0 ? void 0 : logger.error(err);
69
+ }
66
70
  }
67
71
  }
68
72
  if (errors.length) {
@@ -84,6 +88,10 @@ export async function pMap(iterable, mapper, opt = {}) {
84
88
  else if (errorMode === ErrorMode.THROW_AGGREGATED) {
85
89
  errors.push(r.reason);
86
90
  }
91
+ else {
92
+ // otherwise, suppress (but still log via logger)
93
+ logger === null || logger === void 0 ? void 0 : logger.error(r.reason);
94
+ }
87
95
  });
88
96
  if (errors.length) {
89
97
  throw new AggregateError(errors, `pMap resulted in ${errors.length} error(s)`);
@@ -130,6 +138,10 @@ export async function pMap(iterable, mapper, opt = {}) {
130
138
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
131
139
  errors.push(err);
132
140
  }
141
+ else {
142
+ // otherwise, suppress (but still log via logger)
143
+ logger === null || logger === void 0 ? void 0 : logger.error(err);
144
+ }
133
145
  resolvingCount--;
134
146
  next();
135
147
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
- "version": "14.200.0",
3
+ "version": "14.201.1",
4
4
  "scripts": {
5
5
  "prepare": "husky install",
6
6
  "build-prod": "build-prod-esm-cjs",
package/src/abort.ts ADDED
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Like AbortSignal, but it can "abort itself" via the `.abort()` method.
3
+ *
4
+ * Similar to how DeferredPromise is both a Promise and has `.resolve()` and `.reject()` methods.
5
+ *
6
+ * This is to simplify the AbortController/AbortSignal usage.
7
+ *
8
+ * Before this - you need to keep track of 2 things: AbortController and AbortSignal.
9
+ *
10
+ * After - you are good with only AbortableSignal, which can do both.
11
+ * And it's compatible with AbortSignal (because it extends it).
12
+ *
13
+ * @experimental
14
+ */
15
+ export interface AbortableSignal extends AbortSignal {
16
+ abort: AbortController['abort']
17
+ }
18
+
19
+ /**
20
+ * Creates AbortableSignal,
21
+ * which is like AbortSignal, but can "abort itself" with `.abort()` method.
22
+ *
23
+ * @experimental
24
+ */
25
+ export function createAbortableSignal(): AbortableSignal {
26
+ const ac = new AbortController()
27
+ return Object.assign(ac.signal, {
28
+ abort: ac.abort.bind(ac),
29
+ })
30
+ }
package/src/index.ts CHANGED
@@ -81,6 +81,7 @@ export * from './env/buildInfo'
81
81
  export * from './form.util'
82
82
  export * from './semver'
83
83
  export * from './web'
84
+ export * from './abort'
84
85
  export * from './polyfill'
85
86
  export * from './zod/zod.util'
86
87
  export * from './zod/zod.shared.schemas'
@@ -7,7 +7,7 @@ Improvements:
7
7
  - Compatible with pProps (that had typings issues)
8
8
  */
9
9
 
10
- import type { AbortableAsyncMapper } from '..'
10
+ import type { AbortableAsyncMapper, CommonLogger } from '..'
11
11
  import { END, ErrorMode, SKIP } from '..'
12
12
 
13
13
  export interface PMapOptions {
@@ -24,8 +24,15 @@ export interface PMapOptions {
24
24
  * When set to THROW_AGGREGATED, instead of stopping when a promise rejects, it will wait for all the promises to settle and then reject with an Aggregated error.
25
25
  *
26
26
  * When set to SUPPRESS - will ignore errors and return results from successful operations.
27
+ * When in SUPPRESS - errors are still logged via the `logger` (which defaults to console).
27
28
  */
28
29
  errorMode?: ErrorMode
30
+
31
+ /**
32
+ * Default to `console`.
33
+ * Pass `null` to skip logging completely.
34
+ */
35
+ logger?: CommonLogger | null
29
36
  }
30
37
 
31
38
  /**
@@ -59,6 +66,7 @@ export async function pMap<IN, OUT>(
59
66
  mapper: AbortableAsyncMapper<IN, OUT>,
60
67
  opt: PMapOptions = {},
61
68
  ): Promise<OUT[]> {
69
+ const { logger = console } = opt
62
70
  const ret: (OUT | typeof SKIP)[] = []
63
71
  // const iterator = iterable[Symbol.iterator]()
64
72
  const items = [...iterable]
@@ -86,8 +94,10 @@ export async function pMap<IN, OUT>(
86
94
  if (errorMode === ErrorMode.THROW_IMMEDIATELY) throw err
87
95
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
88
96
  errors.push(err as Error)
97
+ } else {
98
+ // otherwise, suppress (but still log via logger)
99
+ logger?.error(err)
89
100
  }
90
- // otherwise, suppress completely
91
101
  }
92
102
  }
93
103
 
@@ -110,6 +120,9 @@ export async function pMap<IN, OUT>(
110
120
  if (r.value !== SKIP && r.value !== END) ret.push(r.value)
111
121
  } else if (errorMode === ErrorMode.THROW_AGGREGATED) {
112
122
  errors.push(r.reason)
123
+ } else {
124
+ // otherwise, suppress (but still log via logger)
125
+ logger?.error(r.reason)
113
126
  }
114
127
  })
115
128
 
@@ -165,6 +178,9 @@ export async function pMap<IN, OUT>(
165
178
  } else {
166
179
  if (errorMode === ErrorMode.THROW_AGGREGATED) {
167
180
  errors.push(err)
181
+ } else {
182
+ // otherwise, suppress (but still log via logger)
183
+ logger?.error(err)
168
184
  }
169
185
  resolvingCount--
170
186
  next()