Skip to content

feat: ability to cancel streamed responses + add TRPCRequestInfo.signal - #5865

Merged
KATT merged 55 commits into
nextfrom
issues/5851-take-two
Jul 10, 2024
Merged

KATT merged 55 commits into
nextfrom
issues/5851-take-two

Conversation

@KATT

@KATT KATT commented Jul 5, 2024 •

Copy link
Copy Markdown
Member

Closes #5851

🎯 Changes

  • Ability to cancel streamed responses
    • When signal.abort() is called on .query(x, { signal })
    • When return; is done when consuming a streamed response
  • Add signal to TRPCRequestInfo to be able to cancel downstream fetch() / db calls as a response to the browser cancelling the request

Cancellation when stopping an async iterable

This is done by handling return() - when you do e.g.

for await (const item of iterable) {
  if (item === 1) break; // <-- triggers `return()`
  console.log(item)
}

We handle this in the jsonl.ts - if there are no remaining things to iterate, we cancel the request through an abort controller

Refactor

Ignore whitespace when reviewing

  • Removed cancellation from dataloader and replaced with using abort signals in most places
  • Changed a lot of abort logic to use abort controllers
  • Removed AbortControllerEsque etc - I don't see why we need it when fetch is both in browsers and in node
  • A bunch of signatures changed from { cancel: () => void; promise: Promise<T> -> Promise<T> which made me to simplify a bunch of code

βœ… Checklist

  • I have followed the steps listed in the Contributing guide.
  • If necessary, I have added documentation related to the changes made.
  • I have added or updated the tests related to the changes made.

@railway-app
railway-app Bot temporarily deployed to trpc-pr-5865 July 5, 2024 14:07 Destroyed
@railway-app
railway-app Bot temporarily deployed to trpc-pr-5865 July 5, 2024 14:07 Destroyed
@railway-app

railway-app Bot commented Jul 5, 2024 •

Copy link
Copy Markdown

πŸš… Previously deployed to Railway in the trpc-sse-and-websockets project. Environment has been deleted.

@vercel

vercel Bot commented Jul 5, 2024 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for Git β†—οΈŽ

Name Status Preview Comments Updated (UTC)
next-prisma-starter βœ… Ready (Inspect) Visit Preview Jul 10, 2024 3:38pm
og-image βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Jul 10, 2024 3:38pm
www βœ… Ready (Inspect) Visit Preview πŸ’¬ Add feedback Jul 10, 2024 3:38pm

Comment on lines +561 to +562
wrapper.returned = true;
maybeAbort();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is sort of the key in it all

@github-actions

Copy link
Copy Markdown
Contributor

This pull request has been locked because we are very unlikely to see comments on closed issues. If you think, this PR is still necessary, create a new one with the same branch. Thank you.

This branch was successfully deployed

5 active deployments
Preview – www β€” 95b05567 Deployed Jul 10, 2024 by vercel[bot]
Preview – examples-todomvc β€” 95b05567 Deployed Jul 10, 2024 by vercel[bot]
Preview – next-prisma-starter β€” 95b05567 Deployed Jul 10, 2024 by vercel[bot]
Preview – trpc-next-app-dir β€” 95b05567 Deployed Jul 10, 2024 by vercel[bot]
Preview – og-image β€” 95b05567 Deployed Jul 10, 2024 by vercel[bot]
trpc-pr-5865 β€” 95b05567 Deployed Jul 10, 2024 by railway-app[bot]
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: Stream Link does not abort

1 participant