@envelop/core 1.6.6-alpha-1e6c37b.0 → 1.6.6-alpha-ed3a9ee.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/enable-if.d.ts CHANGED
@@ -10,5 +10,5 @@ export declare function isPluginEnabled(t: PluginOrDisabledPlugin): t is Plugin;
10
10
  /**
11
11
  * Utility function to enable a plugin.
12
12
  */
13
- export declare function enableIf(condition: boolean, plugin: Plugin | (() => Plugin)): PluginOrDisabledPlugin;
13
+ export declare function enableIf<PluginContextType = {}>(condition: boolean, plugin: Plugin<PluginContextType> | (() => Plugin<PluginContextType>)): PluginOrDisabledPlugin;
14
14
  export {};
package/index.js CHANGED
@@ -136,27 +136,46 @@ function getSubscribeArgs(args) {
136
136
  */
137
137
  const makeSubscribe = (subscribeFn) => ((...polyArgs) => subscribeFn(getSubscribeArgs(polyArgs)));
138
138
  function mapAsyncIterator(source, mapper) {
139
- const iterable = source[Symbol.asyncIterator]();
139
+ const iterator = source[Symbol.asyncIterator]();
140
+ async function mapResult(result) {
141
+ var _a;
142
+ if (result.done) {
143
+ return result;
144
+ }
145
+ try {
146
+ return { value: await mapper(result.value), done: false };
147
+ }
148
+ catch (error) {
149
+ try {
150
+ await ((_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator));
151
+ }
152
+ catch (_error) {
153
+ /* ignore error */
154
+ }
155
+ throw error;
156
+ }
157
+ }
140
158
  const stream = {
141
159
  [Symbol.asyncIterator]() {
142
160
  return stream;
143
161
  },
144
162
  async next() {
145
- const value = await iterable.next();
146
- if (value.done) {
147
- return value;
148
- }
149
- return { done: false, value: await mapper(value.value) };
163
+ return await mapResult(await iterator.next());
150
164
  },
151
165
  async return() {
152
166
  var _a;
153
- (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
154
- return { done: true, value: undefined };
167
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
168
+ return promise ? await mapResult(await promise) : { value: undefined, done: true };
155
169
  },
156
170
  async throw(error) {
157
171
  var _a;
158
- (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
159
- return { done: true, value: undefined };
172
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
173
+ if (promise) {
174
+ return await mapResult(await promise);
175
+ }
176
+ // if the source has no throw method we just re-throw error
177
+ // usually throw is not called anyways
178
+ throw error;
160
179
  },
161
180
  };
162
181
  return stream;
@@ -211,14 +230,14 @@ function handleStreamOrSingleExecutionResult(payload, fn) {
211
230
  }
212
231
  }
213
232
  function finalAsyncIterator(source, onFinal) {
214
- const iterable = source[Symbol.asyncIterator]();
233
+ const iterator = source[Symbol.asyncIterator]();
215
234
  let isDone = false;
216
235
  const stream = {
217
236
  [Symbol.asyncIterator]() {
218
237
  return stream;
219
238
  },
220
239
  async next() {
221
- const result = await iterable.next();
240
+ const result = await iterator.next();
222
241
  if (result.done && isDone === false) {
223
242
  isDone = true;
224
243
  onFinal();
@@ -227,45 +246,55 @@ function finalAsyncIterator(source, onFinal) {
227
246
  },
228
247
  async return() {
229
248
  var _a;
230
- (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
249
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
231
250
  if (isDone === false) {
232
251
  isDone = true;
233
252
  onFinal();
234
253
  }
235
- return { done: true, value: undefined };
254
+ return promise ? await promise : { done: true, value: undefined };
236
255
  },
237
256
  async throw(error) {
238
257
  var _a;
239
- (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
240
- return { done: true, value: undefined };
258
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
259
+ if (promise) {
260
+ return await promise;
261
+ }
262
+ // if the source has no throw method we just re-throw error
263
+ // usually throw is not called anyways
264
+ throw error;
241
265
  },
242
266
  };
243
267
  return stream;
244
268
  }
245
269
  function errorAsyncIterator(source, onError) {
246
- const iterable = source[Symbol.asyncIterator]();
270
+ const iterator = source[Symbol.asyncIterator]();
247
271
  const stream = {
248
272
  [Symbol.asyncIterator]() {
249
273
  return stream;
250
274
  },
251
275
  async next() {
252
276
  try {
253
- return await iterable.next();
277
+ return await iterator.next();
254
278
  }
255
279
  catch (error) {
256
280
  onError(error);
257
- throw error;
281
+ return { done: true, value: undefined };
258
282
  }
259
283
  },
260
284
  async return() {
261
285
  var _a;
262
- (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
263
- return { done: true, value: undefined };
286
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
287
+ return promise ? await promise : { done: true, value: undefined };
264
288
  },
265
289
  async throw(error) {
266
290
  var _a;
267
- (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
268
- return { done: true, value: undefined };
291
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
292
+ if (promise) {
293
+ return await promise;
294
+ }
295
+ // if the source has no throw method we just re-throw error
296
+ // usually throw is not called anyways
297
+ throw error;
269
298
  },
270
299
  };
271
300
  return stream;
package/index.mjs CHANGED
@@ -132,27 +132,46 @@ function getSubscribeArgs(args) {
132
132
  */
133
133
  const makeSubscribe = (subscribeFn) => ((...polyArgs) => subscribeFn(getSubscribeArgs(polyArgs)));
134
134
  function mapAsyncIterator(source, mapper) {
135
- const iterable = source[Symbol.asyncIterator]();
135
+ const iterator = source[Symbol.asyncIterator]();
136
+ async function mapResult(result) {
137
+ var _a;
138
+ if (result.done) {
139
+ return result;
140
+ }
141
+ try {
142
+ return { value: await mapper(result.value), done: false };
143
+ }
144
+ catch (error) {
145
+ try {
146
+ await ((_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator));
147
+ }
148
+ catch (_error) {
149
+ /* ignore error */
150
+ }
151
+ throw error;
152
+ }
153
+ }
136
154
  const stream = {
137
155
  [Symbol.asyncIterator]() {
138
156
  return stream;
139
157
  },
140
158
  async next() {
141
- const value = await iterable.next();
142
- if (value.done) {
143
- return value;
144
- }
145
- return { done: false, value: await mapper(value.value) };
159
+ return await mapResult(await iterator.next());
146
160
  },
147
161
  async return() {
148
162
  var _a;
149
- (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
150
- return { done: true, value: undefined };
163
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
164
+ return promise ? await mapResult(await promise) : { value: undefined, done: true };
151
165
  },
152
166
  async throw(error) {
153
167
  var _a;
154
- (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
155
- return { done: true, value: undefined };
168
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
169
+ if (promise) {
170
+ return await mapResult(await promise);
171
+ }
172
+ // if the source has no throw method we just re-throw error
173
+ // usually throw is not called anyways
174
+ throw error;
156
175
  },
157
176
  };
158
177
  return stream;
@@ -207,14 +226,14 @@ function handleStreamOrSingleExecutionResult(payload, fn) {
207
226
  }
208
227
  }
209
228
  function finalAsyncIterator(source, onFinal) {
210
- const iterable = source[Symbol.asyncIterator]();
229
+ const iterator = source[Symbol.asyncIterator]();
211
230
  let isDone = false;
212
231
  const stream = {
213
232
  [Symbol.asyncIterator]() {
214
233
  return stream;
215
234
  },
216
235
  async next() {
217
- const result = await iterable.next();
236
+ const result = await iterator.next();
218
237
  if (result.done && isDone === false) {
219
238
  isDone = true;
220
239
  onFinal();
@@ -223,45 +242,55 @@ function finalAsyncIterator(source, onFinal) {
223
242
  },
224
243
  async return() {
225
244
  var _a;
226
- (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
245
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
227
246
  if (isDone === false) {
228
247
  isDone = true;
229
248
  onFinal();
230
249
  }
231
- return { done: true, value: undefined };
250
+ return promise ? await promise : { done: true, value: undefined };
232
251
  },
233
252
  async throw(error) {
234
253
  var _a;
235
- (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
236
- return { done: true, value: undefined };
254
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
255
+ if (promise) {
256
+ return await promise;
257
+ }
258
+ // if the source has no throw method we just re-throw error
259
+ // usually throw is not called anyways
260
+ throw error;
237
261
  },
238
262
  };
239
263
  return stream;
240
264
  }
241
265
  function errorAsyncIterator(source, onError) {
242
- const iterable = source[Symbol.asyncIterator]();
266
+ const iterator = source[Symbol.asyncIterator]();
243
267
  const stream = {
244
268
  [Symbol.asyncIterator]() {
245
269
  return stream;
246
270
  },
247
271
  async next() {
248
272
  try {
249
- return await iterable.next();
273
+ return await iterator.next();
250
274
  }
251
275
  catch (error) {
252
276
  onError(error);
253
- throw error;
277
+ return { done: true, value: undefined };
254
278
  }
255
279
  },
256
280
  async return() {
257
281
  var _a;
258
- (_a = iterable.return) === null || _a === void 0 ? void 0 : _a.call(iterable);
259
- return { done: true, value: undefined };
282
+ const promise = (_a = iterator.return) === null || _a === void 0 ? void 0 : _a.call(iterator);
283
+ return promise ? await promise : { done: true, value: undefined };
260
284
  },
261
285
  async throw(error) {
262
286
  var _a;
263
- (_a = iterable.throw) === null || _a === void 0 ? void 0 : _a.call(iterable, error);
264
- return { done: true, value: undefined };
287
+ const promise = (_a = iterator.throw) === null || _a === void 0 ? void 0 : _a.call(iterator);
288
+ if (promise) {
289
+ return await promise;
290
+ }
291
+ // if the source has no throw method we just re-throw error
292
+ // usually throw is not called anyways
293
+ throw error;
265
294
  },
266
295
  };
267
296
  return stream;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@envelop/core",
3
- "version": "1.6.6-alpha-1e6c37b.0",
3
+ "version": "1.6.6-alpha-ed3a9ee.0",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
6
  "graphql": "^14.0.0 || ^15.0.0 || ^16.0.0"