Skip to content
Closed
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
28 changes: 28 additions & 0 deletions rivetkit-typescript/packages/rivetkit/src/actor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const ActorConfigSchema = z
.object({
onCreate: zFunction().optional(),
onDestroy: zFunction().optional(),
run: zFunction().optional(),
onWake: zFunction().optional(),
onSleep: zFunction().optional(),
onStateChange: zFunction().optional(),
Expand Down Expand Up @@ -130,6 +131,13 @@ export const ActorConfigSchema = z
message: "Cannot define both 'vars' and 'createVars'",
path: ["vars"],
},
)
.refine(
(data) => !(data.run !== undefined && data.onWake !== undefined),
{
message: "Cannot define both 'run' and 'onWake'. 'run' is an alias for 'onWake'.",
path: ["run"],
},
);

// Creates state config
Expand Down Expand Up @@ -279,6 +287,24 @@ interface BaseActorConfig<
>,
) => void | Promise<void>;

/**
* Called when the actor is started and ready to receive connections and actions.
*
* This is an alias for `onWake`. Use either `run` or `onWake`, but not both.
*
* @returns Void or a Promise that resolves when startup is complete
*/
run?: (
c: WakeContext<
TState,
TConnParams,
TConnState,
TVars,
TInput,
TDatabase
>,
) => void | Promise<void>;

/**
* Called when the actor is started and ready to receive connections and action.
*
Expand Down Expand Up @@ -497,6 +523,7 @@ export type ActorConfig<
| "actions"
| "onCreate"
| "onDestroy"
| "run"
| "onWake"
| "onStateChange"
| "onBeforeConnect"
Expand Down Expand Up @@ -557,6 +584,7 @@ export type ActorConfigInput<
| "actions"
| "onCreate"
| "onDestroy"
| "run"
| "onWake"
| "onSleep"
| "onStateChange"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -852,8 +852,10 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {

async #callOnStart() {
this.#rLog.info({ msg: "actor starting" });
if (this.#config.onWake) {
const result = this.#config.onWake(this.actorContext);
// `run` is an alias for `onWake`
const onWakeHandler = this.#config.run ?? this.#config.onWake;
if (onWakeHandler) {
const result = onWakeHandler(this.actorContext);
if (result instanceof Promise) {
await result;
}
Expand Down
Loading
Loading