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: deal with fast consecutive promise resolutions when streaming #9332

Merged
merged 2 commits into from
Mar 6, 2023
Merged
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
5 changes: 5 additions & 0 deletions .changeset/strange-garlics-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

fix: deal with fast consecutive promise resolutions when streaming
17 changes: 12 additions & 5 deletions packages/kit/src/utils/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,29 @@ function defer() {
* }}
*/
export function create_async_iterator() {
let deferred = defer();
let deferred = [defer()];

return {
iterator: {
[Symbol.asyncIterator]() {
return {
next: () => deferred.promise
next: async () => {
const next = await deferred[0].promise;
if (!next.done) deferred.shift();
return next;
}
Comment on lines +32 to +36
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
next: async () => {
const next = await deferred[0].promise;
if (!next.done) deferred.shift();
return next;
}
next: deferred.shift().promise

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a type cast because TS doesn't know this is always defined, and strictly speaking it's not adhering to the spec that says you can call next as much as you want after it's done (it should always return done: true in that state).

};
}
},
push: (value) => {
deferred.fulfil({ value, done: false });
deferred = defer();
deferred[deferred.length - 1].fulfil({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
deferred[deferred.length - 1].fulfil({
deferred.at(-1).fulfil({

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comment below

value,
done: false
});
deferred.push(defer());
},
done: () => {
deferred.fulfil({ done: true });
deferred[deferred.length - 1].fulfil({ done: true });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
deferred[deferred.length - 1].fulfil({ done: true });
deferred.at(-1).fulfil({ done: true });

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had that previously but that requires you to do a stupid type cast because TS says "this might be undefined" and that code was harder to read/longer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait TS thinks array.at(x) might be undefined but array[x] is not? the fuck?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is mostly due to backwards compatibility to not break existing code bases. They have another flag outside the strict family that turns on the same behavior for array[x]

}
};
}
20 changes: 20 additions & 0 deletions packages/kit/src/utils/streaming.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { test } from 'uvu';
import * as assert from 'uvu/assert';
import { create_async_iterator } from './streaming.js';

test(`works with fast consecutive promise resolutions`, async () => {
const iterator = create_async_iterator();

Promise.resolve(1).then((n) => iterator.push(n));
Promise.resolve(2).then((n) => iterator.push(n));
Promise.resolve().then(() => iterator.done());

const actual = [];
for await (const value of iterator.iterator) {
actual.push(value);
}

assert.equal(actual, [1, 2]);
});

test.run();