-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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; | ||||||
} | ||||||
}; | ||||||
} | ||||||
}, | ||||||
push: (value) => { | ||||||
deferred.fulfil({ value, done: false }); | ||||||
deferred = defer(); | ||||||
deferred[deferred.length - 1].fulfil({ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 }); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wait TS thinks There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||||||
} | ||||||
}; | ||||||
} |
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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 returndone: true
in that state).