fyn 2.0.2 → 2.0.3

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.
@@ -11,143 +11,93 @@ exports.modules = {
11
11
  /* harmony export */ "default": () => (/* binding */ pMap)
12
12
  /* harmony export */ });
13
13
  /* unused harmony exports pMapIterable, pMapSkip */
14
- function _awaitAsyncGenerator(value) { return new _AwaitValue(value); }
15
-
16
- function _wrapAsyncGenerator(fn) { return function () { return new _AsyncGenerator(fn.apply(this, arguments)); }; }
17
-
18
- function _AsyncGenerator(gen) { var front, back; function send(key, arg) { return new Promise(function (resolve, reject) { var request = { key: key, arg: arg, resolve: resolve, reject: reject, next: null }; if (back) { back = back.next = request; } else { front = back = request; resume(key, arg); } }); } function resume(key, arg) { try { var result = gen[key](arg); var value = result.value; var wrappedAwait = value instanceof _AwaitValue; Promise.resolve(wrappedAwait ? value.wrapped : value).then(function (arg) { if (wrappedAwait) { resume(key === "return" ? "return" : "next", arg); return; } settle(result.done ? "return" : "normal", arg); }, function (err) { resume("throw", err); }); } catch (err) { settle("throw", err); } } function settle(type, value) { switch (type) { case "return": front.resolve({ value: value, done: true }); break; case "throw": front.reject(value); break; default: front.resolve({ value: value, done: false }); break; } front = front.next; if (front) { resume(front.key, front.arg); } else { back = null; } } this._invoke = send; if (typeof gen.return !== "function") { this.return = undefined; } }
19
-
20
- _AsyncGenerator.prototype[typeof Symbol === "function" && Symbol.asyncIterator || "@@asyncIterator"] = function () { return this; };
21
-
22
- _AsyncGenerator.prototype.next = function (arg) { return this._invoke("next", arg); };
23
-
24
- _AsyncGenerator.prototype.throw = function (arg) { return this._invoke("throw", arg); };
25
-
26
- _AsyncGenerator.prototype.return = function (arg) { return this._invoke("return", arg); };
27
-
28
- function _AwaitValue(value) { this.wrapped = value; }
29
-
30
14
  async function pMap(iterable, mapper, {
31
15
  concurrency = Number.POSITIVE_INFINITY,
32
16
  stopOnError = true,
33
17
  signal
34
18
  } = {}) {
35
19
  return new Promise((resolve_, reject_) => {
36
- if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) {
20
+ if (iterable[Symbol.iterator] === void 0 && iterable[Symbol.asyncIterator] === void 0) {
37
21
  throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`);
38
22
  }
39
-
40
- if (typeof mapper !== 'function') {
41
- throw new TypeError('Mapper function is required');
23
+ if (typeof mapper !== "function") {
24
+ throw new TypeError("Mapper function is required");
42
25
  }
43
-
44
26
  if (!(Number.isSafeInteger(concurrency) && concurrency >= 1 || concurrency === Number.POSITIVE_INFINITY)) {
45
27
  throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
46
28
  }
47
-
48
29
  const result = [];
49
30
  const errors = [];
50
- const skippedIndexesMap = new Map();
31
+ const skippedIndexesMap = /* @__PURE__ */ new Map();
51
32
  let isRejected = false;
52
33
  let isResolved = false;
53
34
  let isIterableDone = false;
54
35
  let resolvingCount = 0;
55
36
  let currentIndex = 0;
56
- const iterator = iterable[Symbol.iterator] === undefined ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
57
-
37
+ const iterator = iterable[Symbol.iterator] === void 0 ? iterable[Symbol.asyncIterator]() : iterable[Symbol.iterator]();
58
38
  const signalListener = () => {
59
39
  reject(signal.reason);
60
40
  };
61
-
62
41
  const cleanup = () => {
63
- signal === null || signal === void 0 ? void 0 : signal.removeEventListener('abort', signalListener);
42
+ signal?.removeEventListener("abort", signalListener);
64
43
  };
65
-
66
- const resolve = value => {
44
+ const resolve = (value) => {
67
45
  resolve_(value);
68
46
  cleanup();
69
47
  };
70
-
71
- const reject = reason => {
48
+ const reject = (reason) => {
72
49
  isRejected = true;
73
50
  isResolved = true;
74
51
  reject_(reason);
75
52
  cleanup();
76
53
  };
77
-
78
54
  if (signal) {
79
55
  if (signal.aborted) {
80
56
  reject(signal.reason);
81
57
  }
82
-
83
- signal.addEventListener('abort', signalListener, {
84
- once: true
85
- });
58
+ signal.addEventListener("abort", signalListener, { once: true });
86
59
  }
87
-
88
60
  const next = async () => {
89
61
  if (isResolved) {
90
62
  return;
91
63
  }
92
-
93
64
  const nextItem = await iterator.next();
94
65
  const index = currentIndex;
95
- currentIndex++; // Note: `iterator.next()` can be called many times in parallel.
96
- // This can cause multiple calls to this `next()` function to
97
- // receive a `nextItem` with `done === true`.
98
- // The shutdown logic that rejects/resolves must be protected
99
- // so it runs only one time as the `skippedIndex` logic is
100
- // non-idempotent.
101
-
66
+ currentIndex++;
102
67
  if (nextItem.done) {
103
68
  isIterableDone = true;
104
-
105
69
  if (resolvingCount === 0 && !isResolved) {
106
70
  if (!stopOnError && errors.length > 0) {
107
- reject(new AggregateError(errors)); // eslint-disable-line unicorn/error-message
108
-
71
+ reject(new AggregateError(errors));
109
72
  return;
110
73
  }
111
-
112
74
  isResolved = true;
113
-
114
75
  if (skippedIndexesMap.size === 0) {
115
76
  resolve(result);
116
77
  return;
117
78
  }
118
-
119
- const pureResult = []; // Support multiple `pMapSkip`'s.
120
-
121
- for (const [index, value] of result.entries()) {
122
- if (skippedIndexesMap.get(index) === pMapSkip) {
79
+ const pureResult = [];
80
+ for (const [index2, value] of result.entries()) {
81
+ if (skippedIndexesMap.get(index2) === pMapSkip) {
123
82
  continue;
124
83
  }
125
-
126
84
  pureResult.push(value);
127
85
  }
128
-
129
86
  resolve(pureResult);
130
87
  }
131
-
132
88
  return;
133
89
  }
134
-
135
- resolvingCount++; // Intentionally detached
136
-
90
+ resolvingCount++;
137
91
  (async () => {
138
92
  try {
139
93
  const element = await nextItem.value;
140
-
141
94
  if (isResolved) {
142
95
  return;
143
96
  }
144
-
145
- const value = await mapper(element, index); // Use Map to stage the index of the element.
146
-
97
+ const value = await mapper(element, index);
147
98
  if (value === pMapSkip) {
148
99
  skippedIndexesMap.set(index, value);
149
100
  }
150
-
151
101
  result[index] = value;
152
102
  resolvingCount--;
153
103
  await next();
@@ -156,37 +106,24 @@ async function pMap(iterable, mapper, {
156
106
  reject(error);
157
107
  } else {
158
108
  errors.push(error);
159
- resolvingCount--; // In that case we can't really continue regardless of `stopOnError` state
160
- // since an iterable is likely to continue throwing after it throws once.
161
- // If we continue calling `next()` indefinitely we will likely end up
162
- // in an infinite loop of failed iteration.
163
-
109
+ resolvingCount--;
164
110
  try {
165
111
  await next();
166
- } catch (error) {
167
- reject(error);
112
+ } catch (error2) {
113
+ reject(error2);
168
114
  }
169
115
  }
170
116
  }
171
117
  })();
172
- }; // Create the concurrent runners in a detached (non-awaited)
173
- // promise. We need this so we can await the `next()` calls
174
- // to stop creating runners before hitting the concurrency limit
175
- // if the iterable has already been marked as done.
176
- // NOTE: We *must* do this for async iterators otherwise we'll spin up
177
- // infinite `next()` calls by default and never start the event loop.
178
-
179
-
118
+ };
180
119
  (async () => {
181
120
  for (let index = 0; index < concurrency; index++) {
182
121
  try {
183
- // eslint-disable-next-line no-await-in-loop
184
122
  await next();
185
123
  } catch (error) {
186
124
  reject(error);
187
125
  break;
188
126
  }
189
-
190
127
  if (isIterableDone || isRejected) {
191
128
  break;
192
129
  }
@@ -198,115 +135,75 @@ function pMapIterable(iterable, mapper, {
198
135
  concurrency = Number.POSITIVE_INFINITY,
199
136
  backpressure = concurrency
200
137
  } = {}) {
201
- if (iterable[Symbol.iterator] === undefined && iterable[Symbol.asyncIterator] === undefined) {
138
+ if (iterable[Symbol.iterator] === void 0 && iterable[Symbol.asyncIterator] === void 0) {
202
139
  throw new TypeError(`Expected \`input\` to be either an \`Iterable\` or \`AsyncIterable\`, got (${typeof iterable})`);
203
140
  }
204
-
205
- if (typeof mapper !== 'function') {
206
- throw new TypeError('Mapper function is required');
141
+ if (typeof mapper !== "function") {
142
+ throw new TypeError("Mapper function is required");
207
143
  }
208
-
209
144
  if (!(Number.isSafeInteger(concurrency) && concurrency >= 1 || concurrency === Number.POSITIVE_INFINITY)) {
210
145
  throw new TypeError(`Expected \`concurrency\` to be an integer from 1 and up or \`Infinity\`, got \`${concurrency}\` (${typeof concurrency})`);
211
146
  }
212
-
213
147
  if (!(Number.isSafeInteger(backpressure) && backpressure >= concurrency || backpressure === Number.POSITIVE_INFINITY)) {
214
148
  throw new TypeError(`Expected \`backpressure\` to be an integer from \`concurrency\` (${concurrency}) and up or \`Infinity\`, got \`${backpressure}\` (${typeof backpressure})`);
215
149
  }
216
-
217
150
  return {
218
- [Symbol.asyncIterator]() {
219
- return _wrapAsyncGenerator(function* () {
220
- const iterator = iterable[Symbol.asyncIterator] === undefined ? iterable[Symbol.iterator]() : iterable[Symbol.asyncIterator]();
221
- const promises = [];
222
- let runningMappersCount = 0;
223
- let isDone = false;
224
- let index = 0;
225
-
226
- function trySpawn() {
227
- if (isDone || !(runningMappersCount < concurrency && promises.length < backpressure)) {
228
- return;
151
+ async *[Symbol.asyncIterator]() {
152
+ const iterator = iterable[Symbol.asyncIterator] === void 0 ? iterable[Symbol.iterator]() : iterable[Symbol.asyncIterator]();
153
+ const promises = [];
154
+ let runningMappersCount = 0;
155
+ let isDone = false;
156
+ let index = 0;
157
+ function trySpawn() {
158
+ if (isDone || !(runningMappersCount < concurrency && promises.length < backpressure)) {
159
+ return;
160
+ }
161
+ const promise = (async () => {
162
+ const { done, value } = await iterator.next();
163
+ if (done) {
164
+ return { done: true };
229
165
  }
230
-
231
- const promise = (async () => {
232
- const {
233
- done,
234
- value
235
- } = await iterator.next();
236
-
237
- if (done) {
238
- return {
239
- done: true
240
- };
166
+ runningMappersCount++;
167
+ trySpawn();
168
+ try {
169
+ const returnValue = await mapper(await value, index++);
170
+ runningMappersCount--;
171
+ if (returnValue === pMapSkip) {
172
+ const index2 = promises.indexOf(promise);
173
+ if (index2 > 0) {
174
+ promises.splice(index2, 1);
175
+ }
241
176
  }
242
-
243
- runningMappersCount++; // Spawn if still below concurrency and backpressure limit
244
-
245
177
  trySpawn();
246
-
247
- try {
248
- const returnValue = await mapper(await value, index++);
249
- runningMappersCount--;
250
-
251
- if (returnValue === pMapSkip) {
252
- const index = promises.indexOf(promise);
253
-
254
- if (index > 0) {
255
- promises.splice(index, 1);
256
- }
257
- } // Spawn if still below backpressure limit and just dropped below concurrency limit
258
-
259
-
260
- trySpawn();
261
- return {
262
- done: false,
263
- value: returnValue
264
- };
265
- } catch (error) {
266
- isDone = true;
267
- return {
268
- error
269
- };
270
- }
271
- })();
272
-
273
- promises.push(promise);
178
+ return { done: false, value: returnValue };
179
+ } catch (error) {
180
+ isDone = true;
181
+ return { error };
182
+ }
183
+ })();
184
+ promises.push(promise);
185
+ }
186
+ trySpawn();
187
+ while (promises.length > 0) {
188
+ const { error, done, value } = await promises[0];
189
+ promises.shift();
190
+ if (error) {
191
+ throw error;
192
+ }
193
+ if (done) {
194
+ return;
274
195
  }
275
-
276
196
  trySpawn();
277
-
278
- while (promises.length > 0) {
279
- const {
280
- error,
281
- done,
282
- value
283
- } = yield _awaitAsyncGenerator(promises[0]); // eslint-disable-line no-await-in-loop
284
-
285
- promises.shift();
286
-
287
- if (error) {
288
- throw error;
289
- }
290
-
291
- if (done) {
292
- return;
293
- } // Spawn if just dropped below backpressure limit and below the concurrency limit
294
-
295
-
296
- trySpawn();
297
-
298
- if (value === pMapSkip) {
299
- continue;
300
- }
301
-
302
- yield value;
197
+ if (value === pMapSkip) {
198
+ continue;
303
199
  }
304
- })();
200
+ yield value;
201
+ }
305
202
  }
306
-
307
203
  };
308
204
  }
309
- const pMapSkip = Symbol('skip');
205
+ const pMapSkip = Symbol("skip");
206
+
310
207
 
311
208
  /***/ })
312
209
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "fyn",
3
- "version": "2.0.2",
3
+ "version": "2.0.3",
4
4
  "description": "fyn is the NPM for fynpo -- a zero setup monorepo tool",
5
5
  "main": "./bin/fyn.js",
6
6
  "homepage": "https://jchip.github.io/fynpo/",
@@ -10,7 +10,7 @@
10
10
  "debug-test": "node --inspect-brk node_modules/.bin/mocha --extension ts,js,tsx,jsx,cjs,mjs -c test/spec",
11
11
  "lint": "xrun xarc/lint",
12
12
  "compile-yarn": "babel yarn/src --out-dir yarn/lib",
13
- "build": "xrun --serial fyn/create-tgz compile-yarn bundle",
13
+ "build": "tsx node_modules/.bin/xrun --serial fyn/create-tgz compile-yarn bundle",
14
14
  "analyze": "ANALYZE_BUNDLE=1 xrun compile-yarn bundle",
15
15
  "coverage": "xrun xarc/check",
16
16
  "ci:check": "xrun xarc/check",