From 9f0dc332d97d2b24fadb60bc9b20c16b0684b446 Mon Sep 17 00:00:00 2001 From: Nathan Flurry Date: Tue, 23 Dec 2025 15:34:02 -0700 Subject: [PATCH] chore(docs): document abort signal --- website/src/content/docs/actors/actions.mdx | 34 +++++++++++++++++++ website/src/content/docs/actors/lifecycle.mdx | 23 +++++++++++++ 2 files changed, 57 insertions(+) diff --git a/website/src/content/docs/actors/actions.mdx b/website/src/content/docs/actors/actions.mdx index e439307df3..9a80bd57f4 100644 --- a/website/src/content/docs/actors/actions.mdx +++ b/website/src/content/docs/actors/actions.mdx @@ -245,6 +245,40 @@ const counter = actor({ Actions have a single return value. To stream realtime data in response to an action, use [events](/docs/actors/events). +## Canceling Long-Running Actions + +For operations that should be cancelable on-demand, create your own `AbortController` and chain it with `c.abortSignal` for automatic cleanup on actor shutdown. + +```typescript +import { actor } from "rivetkit"; + +const chatActor = actor({ + createVars: () => ({ controller: null as AbortController | null }), + + actions: { + generate: async (c, prompt: string) => { + const controller = new AbortController(); + c.vars.controller = controller; + c.abortSignal.addEventListener("abort", () => controller.abort()); + + const response = await fetch("https://api.example.com/generate", { + method: "POST", + body: JSON.stringify({ prompt }), + signal: controller.signal + }); + + return await response.json(); + }, + + cancel: (c) => { + c.vars.controller?.abort(); + } + } +}); +``` + +See [Actor Shutdown Abort Signal](/docs/actors/lifecycle#actor-shutdown-abort-signal) for automatically canceling operations when the actor stops. + ## Using `ActionContext` Externally When writing complex logic for actions, you may want to extract parts of your implementation into separate helper functions. When doing this, you'll need a way to properly type the context parameter. diff --git a/website/src/content/docs/actors/lifecycle.mdx b/website/src/content/docs/actors/lifecycle.mdx index cc0fb3b08d..4bfd35059e 100644 --- a/website/src/content/docs/actors/lifecycle.mdx +++ b/website/src/content/docs/actors/lifecycle.mdx @@ -616,6 +616,29 @@ const gameRoom = actor({ }); ``` +### Actor Shutdown Abort Signal + +The `c.abortSignal` provides an `AbortSignal` that fires when the actor is stopping. Use this to cancel ongoing operations when the actor sleeps or is destroyed. + +```typescript +import { actor } from "rivetkit"; + +const chatActor = actor({ + actions: { + generate: async (c, prompt: string) => { + const response = await fetch("https://api.example.com/generate", { + method: "POST", + body: JSON.stringify({ prompt }), + signal: c.abortSignal + }); + + return await response.json(); + } + } +}); +``` + +See [Canceling Long-Running Actions](/docs/actors/actions#canceling-long-running-actions) for manually canceling operations on-demand. ### Using `ActorContext` Type Externally