Skip to content

Commit 676c775

Browse files
Fix Flow issues with Symbol.asyncIterator (#3043)
1 parent 917befd commit 676c775

File tree

6 files changed

+22
-26
lines changed

6 files changed

+22
-26
lines changed

flow-typed/core.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ declare opaque type $SymbolToStringTag: symbol;
260260
declare opaque type $SymbolUnscopables: symbol;
261261

262262
declare class Symbol {
263-
static asyncIterator: string; // graphql-js HACK
263+
static asyncIterator: '@@asyncIterator'; // graphql-js HACK
264264

265265
/**
266266
* Returns a new unique Symbol value.
@@ -287,7 +287,7 @@ declare class Symbol {
287287
* by Array.prototype.concat.
288288
*/
289289
static isConcatSpreadable: $SymboIsConcatSpreadable;
290-
static iterator: string; // polyfill '@@iterator'
290+
static iterator: '@@iterator';
291291
/**
292292
* Returns a key from the global symbol registry matching the given Symbol if found.
293293
* Otherwise, returns a undefined.

src/subscription/__tests__/mapAsyncIterator-test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('mapAsyncIterator', () => {
2525
it('maps over async iterable', async () => {
2626
const items = [1, 2, 3];
2727

28-
const iterable: $FlowFixMe = {
28+
const iterable = {
2929
[Symbol.asyncIterator]() {
3030
return this;
3131
},
@@ -122,7 +122,7 @@ describe('mapAsyncIterator', () => {
122122
it('allows returning early from mapped async iterable', async () => {
123123
const items = [1, 2, 3];
124124

125-
const iterable: any = {
125+
const iterable = {
126126
[Symbol.asyncIterator]() {
127127
return this;
128128
},
@@ -185,7 +185,7 @@ describe('mapAsyncIterator', () => {
185185
it('allows throwing errors through async iterable', async () => {
186186
const items = [1, 2, 3];
187187

188-
const iterable: any = {
188+
const iterable = {
189189
[Symbol.asyncIterator]() {
190190
return this;
191191
},

src/subscription/__tests__/simplePubSub-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { SimplePubSub } from './simplePubSub';
66
describe('SimplePubSub', () => {
77
it('subscribe async-iterator mock', async () => {
88
const pubsub = new SimplePubSub();
9-
const iterator = pubsub.getSubscriber();
9+
const iterator = pubsub.getSubscriber((x) => x);
1010

1111
// Queue up publishes
1212
expect(pubsub.emit('Apple')).to.equal(true);

src/subscription/__tests__/simplePubSub.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export class SimplePubSub<T> {
1616
return this._subscribers.size > 0;
1717
}
1818

19-
getSubscriber<R>(transform?: (T) => R): AsyncGenerator<R, void, void> {
20-
const pullQueue = [];
19+
getSubscriber<R>(transform: (T) => R): AsyncGenerator<R, void, void> {
20+
const pullQueue: Array<(result: IteratorResult<R, void>) => void> = [];
2121
const pushQueue = [];
2222
let listening = true;
2323
this._subscribers.add(pushValue);
@@ -32,9 +32,7 @@ export class SimplePubSub<T> {
3232
pushQueue.length = 0;
3333
};
3434

35-
/* TODO: Flow doesn't support symbols as keys:
36-
https://github.com/facebook/flow/issues/3258 */
37-
return ({
35+
return {
3836
next() {
3937
if (!listening) {
4038
return Promise.resolve({ value: undefined, done: true });
@@ -45,7 +43,7 @@ export class SimplePubSub<T> {
4543
}
4644
return new Promise((resolve) => pullQueue.push(resolve));
4745
},
48-
return() {
46+
return(): Promise<IteratorResult<R, void>> {
4947
emptyQueue();
5048
return Promise.resolve({ value: undefined, done: true });
5149
},
@@ -56,10 +54,10 @@ export class SimplePubSub<T> {
5654
[Symbol.asyncIterator]() {
5755
return this;
5856
},
59-
}: any);
57+
};
6058

6159
function pushValue(event: T): void {
62-
const value = transform != null ? transform(event) : event;
60+
const value: R = transform(event);
6361
if (pullQueue.length > 0) {
6462
pullQueue.shift()({ value, done: false });
6563
} else {

src/subscription/__tests__/subscribe-test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe('Subscription Initialization Phase', () => {
187187
fields: {
188188
importantEmail: {
189189
type: GraphQLString,
190-
subscribe: () => pubsub.getSubscriber(),
190+
subscribe: () => pubsub.getSubscriber((x) => x),
191191
},
192192
},
193193
}),
@@ -219,7 +219,7 @@ describe('Subscription Initialization Phase', () => {
219219
type: GraphQLString,
220220
subscribe: async () => {
221221
await resolveOnNextTick();
222-
return pubsub.getSubscriber();
222+
return pubsub.getSubscriber((x) => x);
223223
},
224224
},
225225
},
@@ -253,15 +253,16 @@ describe('Subscription Initialization Phase', () => {
253253
type: EmailEventType,
254254
subscribe() {
255255
didResolveImportantEmail = true;
256-
return new SimplePubSub().getSubscriber();
256+
// istanbul ignore next (FIXME)
257+
return new SimplePubSub().getSubscriber((x) => x);
257258
},
258259
},
259260
nonImportantEmail: {
260261
type: EmailEventType,
261262
// istanbul ignore next (Shouldn't be called)
262263
subscribe() {
263264
didResolveNonImportantEmail = true;
264-
return new SimplePubSub().getSubscriber();
265+
return new SimplePubSub().getSubscriber((x) => x);
265266
},
266267
},
267268
},

src/subscription/mapAsyncIterator.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ export function mapAsyncIterator<T, U>(
88
iterable: AsyncIterable<T> | AsyncGenerator<T, void, void>,
99
callback: (T) => PromiseOrValue<U>,
1010
): AsyncGenerator<U, void, void> {
11-
// $FlowFixMe[prop-missing]
12-
const iteratorMethod = iterable[Symbol.asyncIterator];
13-
const iterator: any = iteratorMethod.call(iterable);
11+
// $FlowIssue[incompatible-use]
12+
const iterator = iterable[Symbol.asyncIterator]();
1413

1514
async function abruptClose(error: mixed) {
1615
if (typeof iterator.return === 'function') {
@@ -37,13 +36,11 @@ export function mapAsyncIterator<T, U>(
3736
}
3837
}
3938

40-
/* TODO: Flow doesn't support symbols as keys:
41-
https://github.com/facebook/flow/issues/3258 */
42-
return ({
39+
return {
4340
next(): Promise<IteratorResult<U, void>> {
4441
return mapResult(iterator.next());
4542
},
46-
return() {
43+
return(): Promise<IteratorResult<U, void>> {
4744
return typeof iterator.return === 'function'
4845
? mapResult(iterator.return())
4946
: Promise.resolve({ value: undefined, done: true });
@@ -57,5 +54,5 @@ export function mapAsyncIterator<T, U>(
5754
[Symbol.asyncIterator]() {
5855
return this;
5956
},
60-
}: $FlowFixMe);
57+
};
6158
}

0 commit comments

Comments
 (0)