Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: always initialize iterator state in RpcOutputStreamController #662

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/runtime-rpc/spec/server-streaming-call.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('ServerStreamingCall', () => {
);
});

it('should provide correct data', async function () {
it('should provide correct data when getting the values first', async function () {
expect(call.requestHeaders).toBe(requestHeaders);
expect(call.request).toBe(request);
expect(call.responses).toBe(stream);
Expand All @@ -48,7 +48,7 @@ describe('ServerStreamingCall', () => {
});


it('should run through with wrong order', async function () {
it('should provide correct data when getting the values last', async function () {
expect(call.requestHeaders).toBe(requestHeaders);
expect(call.request).toBe(request);
expect(call.responses).toBe(stream);
Expand All @@ -58,7 +58,7 @@ describe('ServerStreamingCall', () => {
for await (let x of call.responses) {
ids.push(x.id);
}
expect(ids.length).toBe(0);
expect(ids).toEqual(["one", "two", "three"]);
});

it('should provide correct data when finished', async function () {
Expand Down
14 changes: 3 additions & 11 deletions packages/runtime-rpc/src/rpc-output-stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type RemoveListenerFn = () => void;
/**
* A `RpcOutputStream` that you control.
*/
export class RpcOutputStreamController<T extends object = object> {
export class RpcOutputStreamController<T extends object = object> implements RpcOutputStream<T> {


constructor() {
Expand Down Expand Up @@ -181,14 +181,14 @@ export class RpcOutputStreamController<T extends object = object> {

// iterator state.
// is undefined when no iterator has been acquired yet.
private _itState: undefined | {
private _itState: {
// a pending result. we yielded that because we were
// waiting for messages at the time.
p?: Deferred<IteratorResult<T, null>>,

// a queue of results that we produced faster that the iterator consumed
q: Array<IteratorResult<T, null> | Error>,
};
} = {q: []};


/**
Expand All @@ -205,12 +205,6 @@ export class RpcOutputStreamController<T extends object = object> {
* messages are queued.
*/
[Symbol.asyncIterator](): AsyncIterator<T> {

// init the iterator state, enabling pushIt()
if (!this._itState) {
this._itState = {q: []};
}

// if we are closed, we are definitely not receiving any more messages.
// but we can't let the iterator get stuck. we want to either:
// a) finish the new iterator immediately, because we are completed
Expand Down Expand Up @@ -249,8 +243,6 @@ export class RpcOutputStreamController<T extends object = object> {
// this either resolves a pending promise, or enqueues the result.
private pushIt(result: IteratorResult<T, null> | Error): void {
let state = this._itState;
if (!state)
return;

// is the consumer waiting for us?
if (state.p) {
Expand Down