Skip to content

Commit e3ee67d

Browse files
committed
don't await at all if there are ready payloads
1 parent 61260af commit e3ee67d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/execution/__tests__/stream-test.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,9 @@ describe('Execute: stream directive', () => {
555555
path: ['friendList', 2],
556556
},
557557
],
558+
hasNext: true,
559+
},
560+
{
558561
hasNext: false,
559562
},
560563
]);
@@ -594,7 +597,7 @@ describe('Execute: stream directive', () => {
594597
}
595598
}
596599
`);
597-
const result = await completeAsync(document, 2, {
600+
const result = await completeAsync(document, 3, {
598601
async *friendList() {
599602
yield await Promise.resolve(friends[0]);
600603
yield await Promise.resolve(friends[1]);
@@ -623,6 +626,12 @@ describe('Execute: stream directive', () => {
623626
path: ['friendList', 2],
624627
},
625628
],
629+
hasNext: true,
630+
},
631+
},
632+
{
633+
done: false,
634+
value: {
626635
hasNext: false,
627636
},
628637
},

src/execution/publisher.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,21 +168,39 @@ export class Publisher<TSource extends Source, TIncremental, TPayload> {
168168
return { value: undefined, done: true };
169169
}
170170

171+
const incremental = this._getCompletedIncrementalResults();
172+
if (!incremental.length) {
173+
return onSignal();
174+
}
175+
176+
const hasNext = this.hasNext();
177+
178+
if (!hasNext) {
179+
isDone = true;
180+
}
181+
182+
return {
183+
value: this.toPayload(incremental, hasNext),
184+
done: false,
185+
};
186+
};
187+
188+
const onSignal = async (): Promise<IteratorResult<TPayload, void>> => {
171189
await this.signal;
172190

173191
if (isDone) {
174192
return { value: undefined, done: true };
175193
}
176194

177195
const incremental = this._getCompletedIncrementalResults();
178-
const hasNext = this.hasNext();
179196

180197
this.signal = new Promise((resolve) => {
181198
this.trigger = resolve;
182199
});
183200

201+
const hasNext = this.hasNext();
184202
if (!incremental.length && hasNext) {
185-
return next();
203+
return onSignal();
186204
}
187205

188206
if (!hasNext) {

0 commit comments

Comments
 (0)