Skip to content

Commit 0e93c82

Browse files
committed
fix: asyncIterator typescript return type
1 parent 167ed72 commit 0e93c82

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

src/pubsub-async-iterator.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ import { PubSubEngine } from 'graphql-subscriptions';
3030
* @property pubsub @type {PubSubEngine}
3131
* The PubSubEngine whose events will be observed.
3232
*/
33-
export class PubSubAsyncIterator<T> implements AsyncIterator<T> {
33+
34+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
35+
// @ts-ignore - https://github.com/leebyron/iterall/issues/49
36+
export class PubSubAsyncIterator<T> implements AsyncIterator<T>, AsyncIterable<T> {
3437

3538
constructor(pubsub: PubSubEngine, eventNames: string | string[], options?: unknown) {
3639
this.pubsub = pubsub;
@@ -41,7 +44,7 @@ export class PubSubAsyncIterator<T> implements AsyncIterator<T> {
4144
this.eventsArray = typeof eventNames === 'string' ? [eventNames] : eventNames;
4245
}
4346

44-
public async next() {
47+
public async next(): Promise<IteratorResult<T>> {
4548
await this.subscribeAll();
4649
return this.listening ? this.pullValue() : this.return();
4750
}
@@ -51,17 +54,17 @@ export class PubSubAsyncIterator<T> implements AsyncIterator<T> {
5154
return { value: undefined, done: true };
5255
}
5356

54-
public async throw(error): Promise<never> {
57+
public async throw(error: Error): Promise<never> {
5558
await this.emptyQueue();
5659
return Promise.reject(error);
5760
}
5861

59-
public [$$asyncIterator]() {
62+
public [$$asyncIterator](): AsyncIterator<T> {
6063
return this;
6164
}
6265

63-
private pullQueue: Array<(data: { value: unknown, done: boolean }) => void>;
64-
private pushQueue: any[];
66+
private pullQueue: Array<(data: { value: T, done: boolean }) => void>;
67+
private pushQueue: T[];
6568
private eventsArray: string[];
6669
private subscriptionIds: Promise<number[]> | undefined;
6770
private listening: boolean;
@@ -77,7 +80,7 @@ export class PubSubAsyncIterator<T> implements AsyncIterator<T> {
7780
}
7881
}
7982

80-
private pullValue(): Promise<IteratorResult<any>> {
83+
private pullValue(): Promise<IteratorResult<T>> {
8184
return new Promise(resolve => {
8285
if (this.pushQueue.length !== 0) {
8386
resolve({ value: this.pushQueue.shift(), done: false });

src/redis-pubsub.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Cluster, Ok, Redis, RedisOptions} from 'ioredis';
2-
import {PubSubEngine} from 'graphql-subscriptions';
2+
33
import {PubSubAsyncIterator} from './pubsub-async-iterator';
4+
import {PubSubEngine} from 'graphql-subscriptions';
45

56
type RedisClient = Redis | Cluster;
67
type OnMessage<T> = (message: T) => void;
@@ -136,8 +137,9 @@ export class RedisPubSub implements PubSubEngine {
136137
delete this.subscriptionMap[subId];
137138
}
138139

139-
public asyncIterator<T>(triggers: string | string[], options?: unknown): AsyncIterator<T> {
140-
return new PubSubAsyncIterator<T>(this, triggers, options);
140+
// return type can be simplified after https://github.com/leebyron/iterall/issues/49 is resolved
141+
public asyncIterator<T>(triggers: string | string[], options?: unknown): PubSubAsyncIterator<T> & AsyncIterator<T> & AsyncIterable<T> {
142+
return new PubSubAsyncIterator<T>(this, triggers, options) as unknown as PubSubAsyncIterator<T> & AsyncIterator<T> & AsyncIterable<T>;
141143
}
142144

143145
public getSubscriber(): RedisClient {

src/test/integration-tests.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import * as chai from 'chai';
22
import * as chaiAsPromised from 'chai-as-promised';
3-
import { mock } from 'simple-mock';
4-
import { parse, GraphQLSchema, GraphQLObjectType, GraphQLString, GraphQLFieldResolver } from 'graphql';
5-
import { isAsyncIterable } from 'iterall';
6-
import { subscribe } from 'graphql/subscription';
73

4+
import { GraphQLFieldResolver, GraphQLObjectType, GraphQLSchema, GraphQLString, parse } from 'graphql';
5+
6+
import { Cluster } from 'ioredis';
87
import { RedisPubSub } from '../redis-pubsub';
8+
import { isAsyncIterable } from 'iterall';
9+
import { mock } from 'simple-mock';
10+
import { subscribe } from 'graphql/subscription';
911
import { withFilter } from '../with-filter';
10-
import { Cluster } from 'ioredis';
1112

1213
chai.use(chaiAsPromised);
1314
const expect = chai.expect;

src/test/tests.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import * as IORedis from 'ioredis';
12
import * as chai from 'chai';
23
import * as chaiAsPromised from 'chai-as-promised';
3-
import { spy, restore, stub } from 'simple-mock';
4-
import { isAsyncIterable } from 'iterall';
4+
5+
import { restore, spy, stub } from 'simple-mock';
6+
57
import { RedisPubSub } from '../redis-pubsub';
6-
import * as IORedis from 'ioredis';
8+
import { isAsyncIterable } from 'iterall';
79

810
chai.use(chaiAsPromised);
911
const expect = chai.expect;
@@ -424,14 +426,22 @@ describe('RedisPubSub', () => {
424426

425427
describe('PubSubAsyncIterator', () => {
426428

427-
it('should expose valid asyncItrator for a specific event', () => {
429+
it('should expose valid asyncItrator for a specific event', async () => {
428430
const pubSub = new RedisPubSub(mockOptions);
429431
const eventName = 'test';
430432
const iterator = pubSub.asyncIterator(eventName);
431433
// tslint:disable-next-line:no-unused-expression
432434
expect(iterator).to.exist;
433435
// tslint:disable-next-line:no-unused-expression
434436
expect(isAsyncIterable(iterator)).to.be.true;
437+
pubSub.publish(eventName, { test: true })
438+
439+
const payloads = []
440+
for await (let payload of iterator) {
441+
payloads.push(payload)
442+
return
443+
}
444+
expect(payloads).eql([{ test: true }])
435445
});
436446

437447
it('should trigger event on asyncIterator when published', done => {

0 commit comments

Comments
 (0)