Skip to content

Commit 89519b9

Browse files
committed
docs: add runInBackground method and update actor lifecycle docs (#2842)
<!-- Please make sure there is an issue that this PR is correlated to. --> ## Changes <!-- If there are frontend changes, please include screenshots. -->
1 parent 0309f87 commit 89519b9

File tree

2 files changed

+32
-38
lines changed

2 files changed

+32
-38
lines changed

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

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -471,55 +471,48 @@ const loggingActor = actor({
471471

472472
## Destroying Actors
473473

474-
Actors can be shut down gracefully with `c.shutdown()`. Clients will be gracefully disconnected.
474+
_Destroying actors is not available yet._
475+
476+
## Advanced
477+
478+
### Running Background Tasks
479+
480+
The `c.runInBackground` method allows you to execute promises asynchronously without blocking the actor's main execution flow. The actor is prevented from sleeping while the promise passed to `runInBackground` is still active. This is useful for fire-and-forget operations where you don't need to wait for completion.
481+
482+
Common use cases:
483+
- **Analytics and logging**: Send events to external services without delaying responses
484+
- **State sync**: Populate external databases or APIs with updates to actor state in the background
475485

476486
```typescript
477487
import { actor } from "@rivetkit/actor";
478488

479-
const temporaryRoom = actor({
480-
state: {
481-
createdAt: 0,
482-
expiresAfterMs: 3600000 // 1 hour
483-
},
484-
485-
createState: () => ({
486-
createdAt: Date.now(),
487-
expiresAfterMs: 3600000 // 1 hour
488-
}),
489-
490-
onStart: (c) => {
491-
// Check if room is expired
492-
const now = Date.now();
493-
const expiresAt = c.state.createdAt + c.state.expiresAfterMs;
494-
495-
if (now > expiresAt) {
496-
console.log("Room expired, shutting down");
497-
c.shutdown();
498-
} else {
499-
// Set up expiration timer
500-
const timeUntilExpiry = expiresAt - now;
501-
setTimeout(() => {
502-
console.log("Room lifetime reached, shutting down");
503-
c.shutdown();
504-
}, timeUntilExpiry);
505-
}
506-
},
489+
const gameRoom = actor({
490+
state: { players: {}, scores: {} },
507491

508492
actions: {
509-
closeRoom: (c) => {
510-
// Notify all clients
511-
c.broadcast("roomClosed", { reason: "Admin closed the room" });
493+
playerJoined: (c, playerId: string) => {
494+
c.state.players[playerId] = { joinedAt: Date.now() };
512495

513-
// Shutdown the actor
514-
c.shutdown();
515-
}
496+
// Send analytics event without blocking
497+
c.runInBackground(
498+
fetch('https://analytics.example.com/events', {
499+
method: 'POST',
500+
body: JSON.stringify({
501+
event: 'player_joined',
502+
playerId,
503+
timestamp: Date.now()
504+
})
505+
}).then(() => console.log('Analytics sent'))
506+
);
507+
508+
return { success: true };
509+
},
516510
}
517511
});
518512
```
519513

520-
This action is permanent and cannot be reverted.
521514

522-
## Using `ActorContext` Type Externally
515+
### Using `ActorContext` Type Externally
523516

524517
When extracting logic from lifecycle hooks or actions into external functions, you'll often need to define the type of the context parameter. Rivet provides helper types that make it easy to extract and pass these context types to external functions.
525518

site/src/sitemap/mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ import {
5555
faPalette,
5656
faLayerGroup,
5757
faVercel,
58+
faSquareRootVariable,
5859
} from "@rivet-gg/icons";
5960

6061
// Goals:
@@ -195,7 +196,7 @@ export const sitemap = [
195196
},
196197
{
197198
title: "State Management",
198-
icon: faFloppyDisk,
199+
icon: faDatabase,
199200
collapsible: true,
200201
pages: [
201202
{

0 commit comments

Comments
 (0)