Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions website/src/content/docs/actors/actions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions website/src/content/docs/actors/lifecycle.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading