Skip to content

Commit 5857a05

Browse files
committed
chore(docs): document abort signal (#3673)
1 parent 28a5c08 commit 5857a05

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

website/src/content/docs/actors/actions.mdx

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,40 @@ const counter = actor({
245245

246246
Actions have a single return value. To stream realtime data in response to an action, use [events](/docs/actors/events).
247247

248+
## Canceling Long-Running Actions
249+
250+
For operations that should be cancelable on-demand, create your own `AbortController` and chain it with `c.abortSignal` for automatic cleanup on actor shutdown.
251+
252+
```typescript
253+
import { actor } from "rivetkit";
254+
255+
const chatActor = actor({
256+
createVars: () => ({ controller: null as AbortController | null }),
257+
258+
actions: {
259+
generate: async (c, prompt: string) => {
260+
const controller = new AbortController();
261+
c.vars.controller = controller;
262+
c.abortSignal.addEventListener("abort", () => controller.abort());
263+
264+
const response = await fetch("https://api.example.com/generate", {
265+
method: "POST",
266+
body: JSON.stringify({ prompt }),
267+
signal: controller.signal
268+
});
269+
270+
return await response.json();
271+
},
272+
273+
cancel: (c) => {
274+
c.vars.controller?.abort();
275+
}
276+
}
277+
});
278+
```
279+
280+
See [Actor Shutdown Abort Signal](/docs/actors/lifecycle#actor-shutdown-abort-signal) for automatically canceling operations when the actor stops.
281+
248282
## Using `ActionContext` Externally
249283

250284
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.

website/src/content/docs/actors/lifecycle.mdx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,29 @@ const gameRoom = actor({
616616
});
617617
```
618618

619+
### Actor Shutdown Abort Signal
620+
621+
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.
622+
623+
```typescript
624+
import { actor } from "rivetkit";
625+
626+
const chatActor = actor({
627+
actions: {
628+
generate: async (c, prompt: string) => {
629+
const response = await fetch("https://api.example.com/generate", {
630+
method: "POST",
631+
body: JSON.stringify({ prompt }),
632+
signal: c.abortSignal
633+
});
634+
635+
return await response.json();
636+
}
637+
}
638+
});
639+
```
640+
641+
See [Canceling Long-Running Actions](/docs/actors/actions#canceling-long-running-actions) for manually canceling operations on-demand.
619642

620643
### Using `ActorContext` Type Externally
621644

0 commit comments

Comments
 (0)