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
2 changes: 1 addition & 1 deletion examples/crdt/src/backend/registry.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/game/src/backend/registry.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions examples/kitchen-sink/SPEC.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions examples/kitchen-sink/src/backend/actors/demo.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ export const counterWithLifecycle = actor({
createConnState: (c, opts, params: ConnParams) => ({
joinTime: Date.now(),
}),
onStart: (c) => {
c.state.events.push("onStart");
onWake: (c) => {
c.state.events.push("onWake");
},
onBeforeConnect: (c, opts, params: ConnParams) => {
if (params?.trackLifecycle) c.state.events.push("onBeforeConnect");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const metadataActor = actor({
storedTags: {} as Record<string, string>,
storedRegion: null as string | null,
},
onStart: (c) => {
onWake: (c) => {
// Store the actor name during initialization
c.state.actorName = c.name;
},
Expand Down Expand Up @@ -62,7 +62,7 @@ export const metadataActor = actor({
return c.state.storedRegion;
},

// Get the stored actor name (from onStart)
// Get the stored actor name (from onWake)
getStoredActorName: (c) => {
return c.state.actorName;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export const SLEEP_TIMEOUT = 1000;

export const sleep = actor({
state: { startCount: 0, sleepCount: 0 },
onStart: (c) => {
onWake: (c) => {
c.state.startCount += 1;
},
onStop: (c) => {
onSleep: (c) => {
c.state.sleepCount += 1;
},
actions: {
Expand Down Expand Up @@ -37,10 +37,10 @@ export const sleepWithLongRpc = actor({
state: { startCount: 0, sleepCount: 0 },
createVars: () =>
({}) as { longRunningResolve: PromiseWithResolvers<void> },
onStart: (c) => {
onWake: (c) => {
c.state.startCount += 1;
},
onStop: (c) => {
onSleep: (c) => {
c.state.sleepCount += 1;
},
actions: {
Expand All @@ -66,10 +66,10 @@ export const sleepWithLongRpc = actor({

export const sleepWithRawHttp = actor({
state: { startCount: 0, sleepCount: 0, requestCount: 0 },
onStart: (c) => {
onWake: (c) => {
c.state.startCount += 1;
},
onStop: (c) => {
onSleep: (c) => {
c.state.sleepCount += 1;
},
onFetch: async (c, request) => {
Expand Down Expand Up @@ -106,10 +106,10 @@ export const sleepWithRawHttp = actor({

export const sleepWithRawWebSocket = actor({
state: { startCount: 0, sleepCount: 0, connectionCount: 0 },
onStart: (c) => {
onWake: (c) => {
c.state.startCount += 1;
},
onStop: (c) => {
onSleep: (c) => {
c.state.sleepCount += 1;
},
onWebSocket: (c, websocket: UniversalWebSocket, opts) => {
Expand Down Expand Up @@ -175,10 +175,10 @@ export const sleepWithRawWebSocket = actor({

export const sleepWithNoSleepOption = actor({
state: { startCount: 0, sleepCount: 0 },
onStart: (c) => {
onWake: (c) => {
c.state.startCount += 1;
},
onStop: (c) => {
onSleep: (c) => {
c.state.sleepCount += 1;
},
actions: {
Expand Down
14 changes: 7 additions & 7 deletions rivetkit-typescript/packages/rivetkit/src/actor/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ export interface ActorTypes<
export const ActorConfigSchema = z
.object({
onCreate: z.function().optional(),
onStart: z.function().optional(),
onStop: z.function().optional(),
onWake: z.function().optional(),
onSleep: z.function().optional(),
onStateChange: z.function().optional(),
onBeforeConnect: z.function().optional(),
onConnect: z.function().optional(),
Expand Down Expand Up @@ -255,7 +255,7 @@ interface BaseActorConfig<
*
* @returns Void or a Promise that resolves when startup is complete
*/
onStart?: (
onWake?: (
c: ActorContext<
TState,
TConnParams,
Expand All @@ -276,7 +276,7 @@ interface BaseActorConfig<
*
* @returns Void or a Promise that resolves when shutdown is complete
*/
onStop?: (
onSleep?: (
c: ActorContext<
TState,
TConnParams,
Expand Down Expand Up @@ -471,7 +471,7 @@ export type ActorConfig<
z.infer<typeof ActorConfigSchema>,
| "actions"
| "onCreate"
| "onStart"
| "onWake"
| "onStateChange"
| "onBeforeConnect"
| "onConnect"
Expand Down Expand Up @@ -530,8 +530,8 @@ export type ActorConfigInput<
z.input<typeof ActorConfigSchema>,
| "actions"
| "onCreate"
| "onStart"
| "onStop"
| "onWake"
| "onSleep"
| "onStateChange"
| "onBeforeConnect"
| "onConnect"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,19 +864,19 @@ export class ActorInstance<S, CP, CS, V, I, DB extends AnyDatabaseProvider> {

async #callOnStart() {
this.#rLog.info({ msg: "actor starting" });
if (this.#config.onStart) {
const result = this.#config.onStart(this.actorContext);
if (this.#config.onWake) {
const result = this.#config.onWake(this.actorContext);
if (result instanceof Promise) {
await result;
}
}
}

async #callOnStop() {
if (this.#config.onStop) {
if (this.#config.onSleep) {
try {
this.#rLog.debug({ msg: "calling onStop" });
const result = this.#config.onStop(this.actorContext);
const result = this.#config.onSleep(this.actorContext);
if (result instanceof Promise) {
await deadline(result, this.#config.options.onStopTimeout);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export function runActorConnTests(driverTestConfig: DriverTestConfig) {
// Verify lifecycle events were triggered
const events = await connection.getEvents();
expect(events).toEqual([
"onStart",
"onWake",
"onBeforeConnect",
"onConnect",
]);
Expand All @@ -279,18 +279,18 @@ export function runActorConnTests(driverTestConfig: DriverTestConfig) {
expect(finalEvents).toBeOneOf([
// Still active
[
"onStart",
"onWake",
"onBeforeConnect",
"onConnect",
"onDisconnect",
],
// Went to sleep and woke back up
[
"onStart",
"onWake",
"onBeforeConnect",
"onConnect",
"onDisconnect",
"onStart",
"onWake",
],
]);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,25 @@ export function runActorHandleTests(driverTestConfig: DriverTestConfig) {
test("should trigger lifecycle hooks on actor creation", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);

// Get or create a new actor - this should trigger onStart
// Get or create a new actor - this should trigger onWake
const handle = client.counterWithLifecycle.getOrCreate([
"test-lifecycle-handle",
]);

// Verify onStart was triggered
// Verify onWake was triggered
const initialEvents = await handle.getEvents();
expect(initialEvents).toContain("onStart");
expect(initialEvents).toContain("onWake");

// Create a separate handle to the same actor
const sameHandle = client.counterWithLifecycle.getOrCreate([
"test-lifecycle-handle",
]);

// Verify events still include onStart but don't duplicate it
// (onStart should only be called once when the actor is first created)
// Verify events still include onWake but don't duplicate it
// (onWake should only be called once when the actor is first created)
const events = await sameHandle.getEvents();
expect(events).toContain("onStart");
expect(events.filter((e) => e === "onStart").length).toBe(1);
expect(events).toContain("onWake");
expect(events.filter((e) => e === "onWake").length).toBe(1);
});

test("should trigger lifecycle hooks for each Action call", async (c) => {
Expand All @@ -212,9 +212,9 @@ export function runActorHandleTests(driverTestConfig: DriverTestConfig) {
"test-lifecycle-action",
]);

// Initial state should only have onStart
// Initial state should only have onWake
const initialEvents = await viewHandle.getEvents();
expect(initialEvents).toContain("onStart");
expect(initialEvents).toContain("onWake");
expect(initialEvents).not.toContain("onBeforeConnect");
expect(initialEvents).not.toContain("onConnect");
expect(initialEvents).not.toContain("onDisconnect");
Expand Down Expand Up @@ -297,8 +297,8 @@ export function runActorHandleTests(driverTestConfig: DriverTestConfig) {
// Check lifecycle hooks
const events = await viewHandle.getEvents();

// Should have 1 onStart, 2 each of onBeforeConnect, onConnect, and onDisconnect
expect(events.filter((e) => e === "onStart").length).toBe(1);
// Should have 1 onWake, 2 each of onBeforeConnect, onConnect, and onDisconnect
expect(events.filter((e) => e === "onWake").length).toBe(1);
expect(
events.filter((e) => e === "onBeforeConnect").length,
).toBe(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export function runActorMetadataTests(driverTestConfig: DriverTestConfig) {
expect(actorName).toBe("metadataActor");
});

test("should preserve actor name in state during onStart", async (c) => {
test("should preserve actor name in state during onWake", async (c) => {
const { client } = await setupDriverTest(c, driverTestConfig);

// Get the stored actor name
Expand Down
Loading