Skip to content

Commit cb03928

Browse files
committed
don't await at all if there are ready payloads
1 parent fb36bff commit cb03928

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
@@ -603,6 +603,9 @@ describe('Execute: stream directive', () => {
603603
path: ['friendList', 2],
604604
},
605605
],
606+
hasNext: true,
607+
},
608+
{
606609
hasNext: false,
607610
},
608611
]);
@@ -642,7 +645,7 @@ describe('Execute: stream directive', () => {
642645
}
643646
}
644647
`);
645-
const result = await completeAsync(document, 2, {
648+
const result = await completeAsync(document, 3, {
646649
async *friendList() {
647650
yield await Promise.resolve(friends[0]);
648651
yield await Promise.resolve(friends[1]);
@@ -671,6 +674,12 @@ describe('Execute: stream directive', () => {
671674
path: ['friendList', 2],
672675
},
673676
],
677+
hasNext: true,
678+
},
679+
},
680+
{
681+
done: false,
682+
value: {
674683
hasNext: false,
675684
},
676685
},

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)