Skip to content

Commit 61bc9ad

Browse files
committed
fix: fix combining CreateState and CreateVars causing V to be unknown (#794)
1 parent 169260a commit 61bc9ad

File tree

4 files changed

+17
-12
lines changed

4 files changed

+17
-12
lines changed

packages/actor-core/src/actor/config.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export interface OnConnectOptions<CP> {
8181
// This must have only one or the other or else S will not be able to be inferred
8282
type CreateState<S, CP, CS, V> =
8383
| { state: S }
84-
| { createState: (c: ActorContext<undefined, CP, CS, V>) => S | Promise<S> }
84+
| { createState: (c: ActorContext<undefined, undefined, undefined, undefined>) => S | Promise<S> }
8585
| Record<never, never>;
8686

8787
// Creates connection state config
@@ -91,7 +91,7 @@ type CreateConnState<S, CP, CS, V> =
9191
| { connState: CS }
9292
| {
9393
createConnState: (
94-
c: ActorContext<S, CP, CS, V>,
94+
c: ActorContext<undefined, undefined, undefined, undefined>,
9595
opts: OnConnectOptions<CP>,
9696
) => CS | Promise<CS>;
9797
}
@@ -114,7 +114,7 @@ type CreateVars<S, CP, CS, V> =
114114
/**
115115
* @experimental
116116
*/
117-
createVars: (c: ActorContext<S, CP, CS, undefined>, driverCtx: unknown) => V | Promise<V>;
117+
createVars: (c: ActorContext<undefined, undefined, undefined, undefined>, driverCtx: unknown) => V | Promise<V>;
118118
}
119119
| Record<never, never>;
120120

packages/actor-core/src/actor/instance.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ export class ActorInstance<S, CP, CS, V> {
171171
let vars: V | undefined = undefined;
172172
if ("createVars" in this.#config) {
173173
const dataOrPromise = this.#config.createVars(
174-
this.actorContext as unknown as ActorContext<S, CP, CS, undefined>,
174+
this.actorContext as unknown as ActorContext<undefined, undefined, undefined, undefined>,
175175
this.#actorDriver.context,
176176
);
177177
if (dataOrPromise instanceof Promise) {
@@ -406,7 +406,7 @@ export class ActorInstance<S, CP, CS, V> {
406406

407407
// Convert state to undefined since state is not defined yet here
408408
stateData = await this.#config.createState(
409-
this.actorContext as unknown as ActorContext<undefined, CP, CS, V>,
409+
this.actorContext as unknown as ActorContext<undefined, undefined, undefined, undefined>,
410410
);
411411
} else if ("state" in this.#config) {
412412
stateData = structuredClone(this.#config.state);
@@ -509,7 +509,7 @@ export class ActorInstance<S, CP, CS, V> {
509509
if (this.#connStateEnabled) {
510510
if ("createConnState" in this.#config) {
511511
const dataOrPromise = this.#config.createConnState(
512-
this.actorContext,
512+
this.actorContext as unknown as ActorContext<undefined, undefined, undefined, undefined>,
513513
onBeforeConnectOpts,
514514
);
515515
if (dataOrPromise instanceof Promise) {

packages/actor-core/tests/vars.test.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ describe("Actor Vars", () => {
77
test("should provide access to static vars", async () => {
88
// Define actor with static vars
99
const varActor = actor({
10-
//state: { value: 0 },
10+
state: { value: 0 },
11+
connState: { hello: "world" },
1112
vars: { counter: 42, name: "test-actor" },
1213
actions: {
1314
getVars: (c) => {
@@ -40,7 +41,8 @@ describe("Actor Vars", () => {
4041
test("should deep clone static vars between actor instances", async () => {
4142
// Define actor with nested object in vars
4243
const nestedVarActor = actor({
43-
//state: { value: 0 },
44+
state: { value: 0 },
45+
connState: { hello: "world" },
4446
vars: {
4547
counter: 42,
4648
nested: {
@@ -95,7 +97,8 @@ describe("Actor Vars", () => {
9597
test("should support dynamic vars creation", async () => {
9698
// Define actor with createVars function
9799
const dynamicVarActor = actor({
98-
//state: { count: 0 },
100+
state: { value: 0 },
101+
connState: { hello: "world" },
99102
createVars: () => {
100103
return {
101104
random: Math.random(),
@@ -130,7 +133,8 @@ describe("Actor Vars", () => {
130133
test("should create different vars for different instances", async () => {
131134
// Define actor with createVars function that generates unique values
132135
const uniqueVarActor = actor({
133-
//state: { value: 0 },
136+
state: { value: 0 },
137+
connState: { hello: "world" },
134138
createVars: () => {
135139
return {
136140
id: Math.floor(Math.random() * 1000000),
@@ -178,7 +182,8 @@ describe("Actor Vars", () => {
178182
}
179183

180184
const driverCtxActor = actor({
181-
//state: { value: 0 },
185+
state: { value: 0 },
186+
connState: { hello: "world" },
182187
createVars: (c, driverCtx: any): DriverVars => {
183188
// In test environment, we get a context with a state property
184189
return {

turbo.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"outputs": ["dist/**"]
1111
},
1212
"check-types": {
13-
"inputs": ["src/**", "tsconfig.json", "tsup.config.ts", "package.json"],
13+
"inputs": ["src/**", "tests/**", "tsconfig.json", "tsup.config.ts", "package.json"],
1414
"dependsOn": ["^build"]
1515
},
1616
"dev": {

0 commit comments

Comments
 (0)