Skip to content
This repository was archived by the owner on Oct 22, 2025. It is now read-only.

Commit 9d9a0ae

Browse files
committed
chore(core): clean up lifecycle hooks for easier type inference (#1161)
1 parent 731c3db commit 9d9a0ae

File tree

14 files changed

+551
-208
lines changed

14 files changed

+551
-208
lines changed

examples/cloudflare-workers/src/registry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ActorContext, actor, setup } from "@rivetkit/actor";
1+
import { actor, setup } from "@rivetkit/actor";
22

33
export const counter = actor({
44
onAuth: () => {

internal-docs/CONFIG_TYPES.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Config Types
2+
3+
## Inferred types
4+
5+
- All types must be included in `ActorTypes` so the user can hardcode types
6+
7+
- If using input parameters for inferring types, they must be raw parameters. e.g.:
8+
9+
```typescript
10+
// It's hard for users to infer TConnParams
11+
onAuth: (opts: OnAuthOpts<TConnParams>) => TAuthData,
12+
// Because you would have to import & use an extra type
13+
onAuth: (opts: OnAuthOpts<MyConnParam>) => TAuthData,
14+
```
15+
16+
- When inferring via return data, you must use a union. e.g.:
17+
18+
```typescript
19+
{ state: TState } | { createState: () => TState } | undefined
20+
```
21+

packages/core/fixtures/driver-test-suite/action-inputs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ export interface State {
88
// Test actor that can capture input during creation
99
export const inputActor = actor({
1010
onAuth: () => {},
11-
createState: (c, { input }): State => {
11+
createState: (c, input): State => {
1212
return {
1313
initialInput: input,
1414
onCreateInput: undefined,
1515
};
1616
},
1717

18-
onCreate: (c, { input }) => {
18+
onCreate: (c, input) => {
1919
c.state.onCreateInput = input;
2020
},
2121

packages/core/fixtures/driver-test-suite/auth.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import { actor, UserError } from "@rivetkit/core";
33
// Basic auth actor - requires API key
44
export const authActor = actor({
55
state: { requests: 0 },
6-
onAuth: (opts) => {
7-
const { request, intents, params } = opts;
6+
onAuth: (params) => {
87
const apiKey = (params as any)?.apiKey;
98
if (!apiKey) {
109
throw new UserError("API key required", { code: "missing_auth" });
@@ -28,8 +27,7 @@ export const authActor = actor({
2827
// Intent-specific auth actor - checks different permissions for different intents
2928
export const intentAuthActor = actor({
3029
state: { value: 0 },
31-
onAuth: (opts) => {
32-
const { request, intents, params } = opts;
30+
onAuth: (params, { request, intents }) => {
3331
console.log("intents", intents, params);
3432
const role = (params as any)?.role;
3533

@@ -82,9 +80,7 @@ export const noAuthActor = actor({
8280
// Async auth actor - tests promise-based authentication
8381
export const asyncAuthActor = actor({
8482
state: { count: 0 },
85-
onAuth: async (opts) => {
86-
const { params } = opts;
87-
83+
onAuth: async (params) => {
8884
const token = (params as any)?.token;
8985
if (!token) {
9086
throw new UserError("Token required", { code: "missing_token" });

packages/core/fixtures/driver-test-suite/lifecycle.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
11
import { actor } from "@rivetkit/core";
22

3+
type ConnParams = { trackLifecycle?: boolean } | undefined;
4+
35
export const counterWithLifecycle = actor({
46
onAuth: () => {},
57
state: {
68
count: 0,
79
events: [] as string[],
810
},
9-
createConnState: (
10-
c,
11-
opts: { params: { trackLifecycle?: boolean } | undefined },
12-
) => ({
11+
createConnState: (c, params: ConnParams) => ({
1312
joinTime: Date.now(),
1413
}),
1514
onStart: (c) => {
1615
c.state.events.push("onStart");
1716
},
18-
onBeforeConnect: (c, conn) => {
19-
if (conn.params?.trackLifecycle) c.state.events.push("onBeforeConnect");
17+
onBeforeConnect: (c, params) => {
18+
if (params?.trackLifecycle) c.state.events.push("onBeforeConnect");
2019
},
2120
onConnect: (c, conn) => {
2221
if (conn.params?.trackLifecycle) c.state.events.push("onConnect");

packages/core/fixtures/driver-test-suite/raw-http-auth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ export const rawHttpAuthActor = actor({
55
state: {
66
requestCount: 0,
77
},
8-
onAuth: (opts) => {
9-
const { params } = opts;
8+
onAuth: (params) => {
109
const apiKey = (params as any)?.apiKey;
1110
if (!apiKey) {
1211
throw new UserError("API key required", { code: "missing_auth" });

packages/core/fixtures/driver-test-suite/raw-websocket-auth.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ export const rawWebSocketAuthActor = actor({
1111
connectionCount: 0,
1212
messageCount: 0,
1313
},
14-
onAuth: (opts) => {
15-
const { params } = opts;
14+
onAuth: (params) => {
1615
const apiKey = (params as any)?.apiKey;
1716
if (!apiKey) {
1817
throw new UserError("API key required", { code: "missing_auth" });

packages/core/fixtures/driver-test-suite/raw-websocket.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ export const rawWebSocketActor = actor({
99
connectionCount: 0,
1010
messageCount: 0,
1111
},
12-
onAuth(opts) {
12+
onAuth(params) {
1313
// Allow all connections and pass through connection params
14-
return { connParams: opts.params };
14+
return { connParams: params };
1515
},
1616
onWebSocket(ctx, websocket, opts) {
1717
ctx.state.connectionCount = ctx.state.connectionCount + 1;

packages/core/src/actor/action.ts

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,23 @@ import type { Schedule } from "./schedule";
1414
* @typeParam A Actor this action belongs to
1515
*/
1616
export class ActionContext<
17-
S,
18-
CP,
19-
CS,
20-
V,
21-
I,
22-
AD,
23-
DB extends AnyDatabaseProvider,
17+
TState,
18+
TConnParams,
19+
TConnState,
20+
TVars,
21+
TInput,
22+
TAuthData,
23+
TDatabase extends AnyDatabaseProvider,
2424
> {
25-
#actorContext: ActorContext<S, CP, CS, V, I, AD, DB>;
25+
#actorContext: ActorContext<
26+
TState,
27+
TConnParams,
28+
TConnState,
29+
TVars,
30+
TInput,
31+
TAuthData,
32+
TDatabase
33+
>;
2634

2735
/**
2836
* Should not be called directly.
@@ -31,23 +39,39 @@ export class ActionContext<
3139
* @param conn - The connection associated with the action
3240
*/
3341
constructor(
34-
actorContext: ActorContext<S, CP, CS, V, I, AD, DB>,
35-
public readonly conn: Conn<S, CP, CS, V, I, AD, DB>,
42+
actorContext: ActorContext<
43+
TState,
44+
TConnParams,
45+
TConnState,
46+
TVars,
47+
TInput,
48+
TAuthData,
49+
TDatabase
50+
>,
51+
public readonly conn: Conn<
52+
TState,
53+
TConnParams,
54+
TConnState,
55+
TVars,
56+
TInput,
57+
TAuthData,
58+
TDatabase
59+
>,
3660
) {
3761
this.#actorContext = actorContext;
3862
}
3963

4064
/**
4165
* Get the actor state
4266
*/
43-
get state(): S {
67+
get state(): TState {
4468
return this.#actorContext.state;
4569
}
4670

4771
/**
4872
* Get the actor variables
4973
*/
50-
get vars(): V {
74+
get vars(): TVars {
5175
return this.#actorContext.vars;
5276
}
5377

@@ -103,7 +127,10 @@ export class ActionContext<
103127
/**
104128
* Gets the map of connections.
105129
*/
106-
get conns(): Map<ConnId, Conn<S, CP, CS, V, I, AD, DB>> {
130+
get conns(): Map<
131+
ConnId,
132+
Conn<TState, TConnParams, TConnState, TVars, TInput, TAuthData, TDatabase>
133+
> {
107134
return this.#actorContext.conns;
108135
}
109136

@@ -117,7 +144,7 @@ export class ActionContext<
117144
/**
118145
* @experimental
119146
*/
120-
get db(): InferDatabaseClient<DB> {
147+
get db(): InferDatabaseClient<TDatabase> {
121148
return this.#actorContext.db;
122149
}
123150

0 commit comments

Comments
 (0)