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

Conversation

dummdidumm
Copy link
Member

fixes #9330

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Comment on lines +32 to +36
next: async () => {
const next = await deferred[0].promise;
if (!next.done) deferred.shift();
return next;
}
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

},
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]

@Rich-Harris Rich-Harris merged commit 18d330b into master Mar 6, 2023
@Rich-Harris Rich-Harris deleted the iterator-fix branch March 6, 2023 15:57
@Rich-Harris Rich-Harris restored the iterator-fix branch March 6, 2023 15:59
Rich-Harris added a commit that referenced this pull request Mar 6, 2023
@Rich-Harris Rich-Harris deleted the iterator-fix branch March 6, 2023 17:02
@github-actions github-actions bot mentioned this pull request Mar 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Only one promise resolves when streamed from load (using Promise.resolve)
2 participants