@graphql-tools/executor 0.0.2 → 0.0.3-alpha-20221031181646-cd48ed2f
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/cjs/execution/execute.js +654 -109
- package/cjs/execution/flattenAsyncIterable.js +91 -0
- package/cjs/execution/invariant.js +9 -0
- package/cjs/execution/promiseForObject.js +21 -0
- package/esm/execution/execute.js +651 -108
- package/esm/execution/flattenAsyncIterable.js +87 -0
- package/esm/execution/invariant.js +5 -0
- package/esm/execution/promiseForObject.js +17 -0
- package/package.json +2 -2
- package/typings/execution/execute.d.cts +168 -23
- package/typings/execution/execute.d.ts +168 -23
- package/typings/execution/flattenAsyncIterable.d.cts +7 -0
- package/typings/execution/flattenAsyncIterable.d.ts +7 -0
- package/typings/execution/invariant.d.cts +1 -0
- package/typings/execution/invariant.d.ts +1 -0
- package/typings/execution/promiseForObject.d.cts +12 -0
- package/typings/execution/promiseForObject.d.ts +12 -0
- package/cjs/execution/subscribe.js +0 -158
- package/esm/execution/subscribe.js +0 -153
- package/typings/execution/subscribe.d.cts +0 -59
- package/typings/execution/subscribe.d.ts +0 -59
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.flattenAsyncIterable = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Given an AsyncIterable of AsyncIterables, flatten all yielded results into a
|
|
6
|
+
* single AsyncIterable.
|
|
7
|
+
*/
|
|
8
|
+
function flattenAsyncIterable(iterable) {
|
|
9
|
+
// You might think this whole function could be replaced with
|
|
10
|
+
//
|
|
11
|
+
// async function* flattenAsyncIterable(iterable) {
|
|
12
|
+
// for await (const subIterator of iterable) {
|
|
13
|
+
// yield* subIterator;
|
|
14
|
+
// }
|
|
15
|
+
// }
|
|
16
|
+
//
|
|
17
|
+
// but calling `.return()` on the iterator it returns won't interrupt the `for await`.
|
|
18
|
+
const topIterator = iterable[Symbol.asyncIterator]();
|
|
19
|
+
let currentNestedIterator;
|
|
20
|
+
let waitForCurrentNestedIterator;
|
|
21
|
+
let done = false;
|
|
22
|
+
async function next() {
|
|
23
|
+
if (done) {
|
|
24
|
+
return { value: undefined, done: true };
|
|
25
|
+
}
|
|
26
|
+
try {
|
|
27
|
+
if (!currentNestedIterator) {
|
|
28
|
+
// Somebody else is getting it already.
|
|
29
|
+
if (waitForCurrentNestedIterator) {
|
|
30
|
+
await waitForCurrentNestedIterator;
|
|
31
|
+
return await next();
|
|
32
|
+
}
|
|
33
|
+
// Nobody else is getting it. We should!
|
|
34
|
+
let resolve;
|
|
35
|
+
waitForCurrentNestedIterator = new Promise(r => {
|
|
36
|
+
resolve = r;
|
|
37
|
+
});
|
|
38
|
+
const topIteratorResult = await topIterator.next();
|
|
39
|
+
if (topIteratorResult.done) {
|
|
40
|
+
// Given that done only ever transitions from false to true,
|
|
41
|
+
// require-atomic-updates is being unnecessarily cautious.
|
|
42
|
+
done = true;
|
|
43
|
+
return await next();
|
|
44
|
+
}
|
|
45
|
+
// eslint is making a reasonable point here, but we've explicitly protected
|
|
46
|
+
// ourself from the race condition by ensuring that only the single call
|
|
47
|
+
// that assigns to waitForCurrentNestedIterator is allowed to assign to
|
|
48
|
+
// currentNestedIterator or waitForCurrentNestedIterator.
|
|
49
|
+
currentNestedIterator = topIteratorResult.value[Symbol.asyncIterator]();
|
|
50
|
+
waitForCurrentNestedIterator = undefined;
|
|
51
|
+
resolve();
|
|
52
|
+
return await next();
|
|
53
|
+
}
|
|
54
|
+
const rememberCurrentNestedIterator = currentNestedIterator;
|
|
55
|
+
const nestedIteratorResult = await currentNestedIterator.next();
|
|
56
|
+
if (!nestedIteratorResult.done) {
|
|
57
|
+
return nestedIteratorResult;
|
|
58
|
+
}
|
|
59
|
+
// The nested iterator is done. If it's still the current one, make it not
|
|
60
|
+
// current. (If it's not the current one, somebody else has made us move on.)
|
|
61
|
+
if (currentNestedIterator === rememberCurrentNestedIterator) {
|
|
62
|
+
currentNestedIterator = undefined;
|
|
63
|
+
}
|
|
64
|
+
return await next();
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
done = true;
|
|
68
|
+
throw err;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
next,
|
|
73
|
+
async return() {
|
|
74
|
+
var _a, _b;
|
|
75
|
+
done = true;
|
|
76
|
+
await Promise.all([(_a = currentNestedIterator === null || currentNestedIterator === void 0 ? void 0 : currentNestedIterator.return) === null || _a === void 0 ? void 0 : _a.call(currentNestedIterator), (_b = topIterator.return) === null || _b === void 0 ? void 0 : _b.call(topIterator)]);
|
|
77
|
+
return { value: undefined, done: true };
|
|
78
|
+
},
|
|
79
|
+
async throw(error) {
|
|
80
|
+
var _a, _b;
|
|
81
|
+
done = true;
|
|
82
|
+
await Promise.all([(_a = currentNestedIterator === null || currentNestedIterator === void 0 ? void 0 : currentNestedIterator.throw) === null || _a === void 0 ? void 0 : _a.call(currentNestedIterator, error), (_b = topIterator.throw) === null || _b === void 0 ? void 0 : _b.call(topIterator, error)]);
|
|
83
|
+
/* c8 ignore next */
|
|
84
|
+
throw error;
|
|
85
|
+
},
|
|
86
|
+
[Symbol.asyncIterator]() {
|
|
87
|
+
return this;
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
exports.flattenAsyncIterable = flattenAsyncIterable;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.invariant = void 0;
|
|
4
|
+
function invariant(condition, message) {
|
|
5
|
+
if (!condition) {
|
|
6
|
+
throw new Error(message != null ? message : 'Unexpected invariant triggered.');
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
exports.invariant = invariant;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.promiseForObject = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* This function transforms a JS object `Record<string, Promise<T>>` into
|
|
6
|
+
* a `Promise<Record<string, T>>`
|
|
7
|
+
*
|
|
8
|
+
* This is akin to bluebird's `Promise.props`, but implemented only using
|
|
9
|
+
* `Promise.all` so it will work with any implementation of ES6 promises.
|
|
10
|
+
*/
|
|
11
|
+
async function promiseForObject(object) {
|
|
12
|
+
const keys = Object.keys(object);
|
|
13
|
+
const values = Object.values(object);
|
|
14
|
+
const resolvedValues = await Promise.all(values);
|
|
15
|
+
const resolvedObject = Object.create(null);
|
|
16
|
+
for (let i = 0; i < keys.length; ++i) {
|
|
17
|
+
resolvedObject[keys[i]] = resolvedValues[i];
|
|
18
|
+
}
|
|
19
|
+
return resolvedObject;
|
|
20
|
+
}
|
|
21
|
+
exports.promiseForObject = promiseForObject;
|