Skip to content

Commit

Permalink
Move 'isAsyncIterable' into 'jsutils' (graphql#2775)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Aug 31, 2020
1 parent ab329e9 commit f3e2954
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 18 deletions.
53 changes: 53 additions & 0 deletions src/jsutils/__tests__/isAsyncIterable-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import identityFunc from '../identityFunc';
import isAsyncIterable from '../isAsyncIterable';

describe('isAsyncIterable', () => {
it('should return `true` for AsyncIterable', () => {
// $FlowFixMe[prop-missing] Flow doesn't support Symbol.asyncIterator
const asyncIteratable = { [Symbol.asyncIterator]: identityFunc };
expect(isAsyncIterable(asyncIteratable)).to.equal(true);

// istanbul ignore next (Never called and use just as a placeholder)
async function* asyncGeneratorFunc() {
/* do nothing */
}

expect(isAsyncIterable(asyncGeneratorFunc())).to.equal(true);

// But async generator function itself is not iteratable
expect(isAsyncIterable(asyncGeneratorFunc)).to.equal(false);
});

it('should return `false` for all other values', () => {
expect(isAsyncIterable(null)).to.equal(false);
expect(isAsyncIterable(undefined)).to.equal(false);

expect(isAsyncIterable('ABC')).to.equal(false);
expect(isAsyncIterable('0')).to.equal(false);
expect(isAsyncIterable('')).to.equal(false);

expect(isAsyncIterable([])).to.equal(false);
expect(isAsyncIterable(new Int8Array(1))).to.equal(false);

expect(isAsyncIterable({})).to.equal(false);
expect(isAsyncIterable({ iterable: true })).to.equal(false);

const iterator = { [Symbol.iterator]: identityFunc };
expect(isAsyncIterable(iterator)).to.equal(false);

// istanbul ignore next (Never called and use just as a placeholder)
function* generatorFunc() {
/* do nothing */
}
expect(isAsyncIterable(generatorFunc())).to.equal(false);

const invalidAsyncIteratable = {
// $FlowFixMe[prop-missing] Flow doesn't support Symbol.asyncIterator
[Symbol.asyncIterator]: { next: identityFunc },
};
expect(isAsyncIterable(invalidAsyncIteratable)).to.equal(false);
});
});
17 changes: 17 additions & 0 deletions src/jsutils/isAsyncIterable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { SYMBOL_ASYNC_ITERATOR } from '../polyfills/symbols';

/**
* Returns true if the provided object implements the AsyncIterator protocol via
* either implementing a `Symbol.asyncIterator` or `"@@asyncIterator"` method.
*/
declare function isAsyncIterable(value: mixed): boolean %checks(value instanceof
AsyncIterable);

// eslint-disable-next-line no-redeclare
export default function isAsyncIterable(maybeAsyncIterable) {
if (maybeAsyncIterable == null || typeof maybeAsyncIterable !== 'object') {
return false;
}

return typeof maybeAsyncIterable[SYMBOL_ASYNC_ITERATOR] === 'function';
}
21 changes: 3 additions & 18 deletions src/subscription/subscribe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { SYMBOL_ASYNC_ITERATOR } from '../polyfills/symbols';

import inspect from '../jsutils/inspect';
import isAsyncIterable from '../jsutils/isAsyncIterable';
import { addPath, pathToArray } from '../jsutils/Path';

import { GraphQLError } from '../error/GraphQLError';
Expand Down Expand Up @@ -158,7 +157,7 @@ function subscribeImpl(
// Note: Flow can't refine isAsyncIterable, so explicit casts are used.
isAsyncIterable(resultOrStream)
? mapAsyncIterator(
((resultOrStream: any): AsyncIterable<mixed>),
resultOrStream,
mapSourceToResponse,
reportGraphQLError,
)
Expand Down Expand Up @@ -289,24 +288,10 @@ function executeSubscription(
`Received: ${inspect(eventStream)}.`,
);
}

// Note: isAsyncIterable above ensures this will be correct.
return ((eventStream: any): AsyncIterable<mixed>);
return eventStream;
},
(error) => {
throw locatedError(error, fieldNodes, pathToArray(path));
},
);
}

/**
* Returns true if the provided object implements the AsyncIterator protocol via
* either implementing a `Symbol.asyncIterator` or `"@@asyncIterator"` method.
*/
function isAsyncIterable(maybeAsyncIterable: mixed): boolean {
if (maybeAsyncIterable == null || typeof maybeAsyncIterable !== 'object') {
return false;
}

return typeof maybeAsyncIterable[SYMBOL_ASYNC_ITERATOR] === 'function';
}

0 comments on commit f3e2954

Please sign in to comment.