Skip to content

Commit 79b136e

Browse files
committed
feat(rivetkit): add actor run handler
1 parent 266ebbd commit 79b136e

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

rivetkit-typescript/packages/rivetkit/src/actor/config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const ActorConfigSchema = z
5050
.object({
5151
onCreate: zFunction().optional(),
5252
onDestroy: zFunction().optional(),
53+
run: zFunction().optional(),
5354
onWake: zFunction().optional(),
5455
onSleep: zFunction().optional(),
5556
onStateChange: zFunction().optional(),
@@ -130,6 +131,13 @@ export const ActorConfigSchema = z
130131
message: "Cannot define both 'vars' and 'createVars'",
131132
path: ["vars"],
132133
},
134+
)
135+
.refine(
136+
(data) => !(data.run !== undefined && data.onWake !== undefined),
137+
{
138+
message: "Cannot define both 'run' and 'onWake'. 'run' is an alias for 'onWake'.",
139+
path: ["run"],
140+
},
133141
);
134142

135143
// Creates state config
@@ -279,6 +287,24 @@ interface BaseActorConfig<
279287
>,
280288
) => void | Promise<void>;
281289

290+
/**
291+
* Called when the actor is started and ready to receive connections and actions.
292+
*
293+
* This is an alias for `onWake`. Use either `run` or `onWake`, but not both.
294+
*
295+
* @returns Void or a Promise that resolves when startup is complete
296+
*/
297+
run?: (
298+
c: WakeContext<
299+
TState,
300+
TConnParams,
301+
TConnState,
302+
TVars,
303+
TInput,
304+
TDatabase
305+
>,
306+
) => void | Promise<void>;
307+
282308
/**
283309
* Called when the actor is started and ready to receive connections and action.
284310
*
@@ -497,6 +523,7 @@ export type ActorConfig<
497523
| "actions"
498524
| "onCreate"
499525
| "onDestroy"
526+
| "run"
500527
| "onWake"
501528
| "onStateChange"
502529
| "onBeforeConnect"
@@ -557,6 +584,7 @@ export type ActorConfigInput<
557584
| "actions"
558585
| "onCreate"
559586
| "onDestroy"
587+
| "run"
560588
| "onWake"
561589
| "onSleep"
562590
| "onStateChange"

rivetkit-typescript/packages/rivetkit/src/actor/instance/mod.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,10 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {
852852

853853
async #callOnStart() {
854854
this.#rLog.info({ msg: "actor starting" });
855-
if (this.#config.onWake) {
856-
const result = this.#config.onWake(this.actorContext);
855+
// `run` is an alias for `onWake`
856+
const onWakeHandler = this.#config.run ?? this.#config.onWake;
857+
if (onWakeHandler) {
858+
const result = onWakeHandler(this.actorContext);
857859
if (result instanceof Promise) {
858860
await result;
859861
}

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

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Actors transition through several states during their lifetime. Each transition
1111
1. `createState`
1212
2. `onCreate`
1313
3. `createVars`
14-
4. `onWake`
14+
4. `run`
1515

1616
**On Destroy**
1717

@@ -20,7 +20,7 @@ Actors transition through several states during their lifetime. Each transition
2020
**On Wake** (after sleep, restart, or crash)
2121

2222
1. `createVars`
23-
2. `onWake`
23+
2. `run`
2424

2525
**On Sleep** (after idle period)
2626

@@ -145,7 +145,7 @@ const gameSession = actor({
145145
});
146146
```
147147

148-
### `onWake`
148+
### `run`
149149

150150
[API Reference](/typedoc/interfaces/rivetkit.mod.ActorDefinition.html)
151151

@@ -162,7 +162,7 @@ const counter = actor({
162162
state: { count: 0 },
163163
vars: { intervalId: null as NodeJS.Timeout | null },
164164

165-
onWake: (c) => {
165+
run: (c) => {
166166
console.log('Actor started with count:', c.state.count);
167167

168168
// Set up interval for automatic counting
@@ -187,6 +187,10 @@ const counter = actor({
187187
});
188188
```
189189

190+
### `onWake`
191+
192+
`onWake` is an alias for [`run`](#run). Use either `run` or `onWake`, but not both.
193+
190194
### `onSleep`
191195

192196
[API Reference](/typedoc/interfaces/rivetkit.mod.ActorDefinition.html)
@@ -204,7 +208,7 @@ const counter = actor({
204208
state: { count: 0 },
205209
vars: { intervalId: null as NodeJS.Timeout | null },
206210

207-
onWake: (c) => {
211+
run: (c) => {
208212
// Set up interval when actor wakes
209213
c.vars.intervalId = setInterval(() => {
210214
c.state.count++;
@@ -649,9 +653,9 @@ import { actor, ActorContextOf } from "rivetkit";
649653

650654
const myActor = actor({
651655
state: { count: 0 },
652-
656+
653657
// Use external function in lifecycle hook
654-
onWake: (c) => logActorStarted(c)
658+
run: (c) => logActorStarted(c)
655659
});
656660

657661
// Simple external function with typed context
@@ -716,7 +720,7 @@ const counter = actor({
716720
},
717721

718722
// Lifecycle hooks
719-
onWake: (c) => {
723+
run: (c) => {
720724
console.log(`Counter "${c.state.name}" started with count:`, c.state.count);
721725
},
722726

0 commit comments

Comments
 (0)