Skip to content

Commit 275a763

Browse files
committed
feat(rivetkit): add actor run handler
1 parent bde0b0f commit 275a763

File tree

3 files changed

+99
-118
lines changed

3 files changed

+99
-118
lines changed

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
}

0 commit comments

Comments
 (0)