Skip to content

Commit 14c2829

Browse files
committed
docs: document createVars & driver-specific values (#787)
1 parent bde6b04 commit 14c2829

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

docs/concepts/lifecycle.mdx

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,51 @@ Actor lifecycle hooks are defined as functions in the actor configuration.
1313

1414
The `createState` function or `state` constant defines the initial state of the actor (see [state documentation](/concepts/state)). The `createState` function is called only once when the actor is first created.
1515

16+
### `createVars` and `vars`
17+
18+
The `createVars` function or `vars` constant defines ephemeral variables for the actor (see [state documentation](/concepts/state)). These variables are not persisted and are useful for storing runtime-only objects or temporary data.
19+
20+
The `createVars` function can also receive driver-specific context as its second parameter, allowing access to driver capabilities like Rivet KV or Cloudflare Durable Object storage.
21+
22+
```typescript
23+
import { actor } from "actor-core";
24+
25+
// Using vars constant
26+
const counter1 = actor({
27+
state: { count: 0 },
28+
vars: { lastAccessTime: 0 },
29+
actions: { /* ... */ }
30+
});
31+
32+
// Using createVars function
33+
const counter2 = actor({
34+
state: { count: 0 },
35+
createVars: () => {
36+
// Initialize with non-serializable objects
37+
return {
38+
lastAccessTime: Date.now(),
39+
emitter: createNanoEvents()
40+
};
41+
},
42+
actions: { /* ... */ }
43+
});
44+
45+
// Access driver-specific context
46+
const exampleActor = actor({
47+
state: { count: 0 },
48+
// Access driver context in createVars
49+
createVars: (c, rivet) => ({
50+
ctx: rivet.ctx,
51+
}),
52+
actions: {
53+
doSomething: (c) => {
54+
// Use driver-specific context
55+
console.log(`Region: ${c.vars.rivet.metadata.region.name}`);
56+
}
57+
}
58+
});
59+
```
60+
1661
### `onCreate`
1762

1863
The `onCreate` hook is called at the same time as `createState`, but unlike `createState`, it doesn't return any value. Use this hook for initialization logic that doesn't affect the initial state.

docs/drivers/overview.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Drivers in ActorCore the infrastructure layer between your actor code and the un
99

1010
<DriverNote />
1111

12+
## Accessing Driver Context
13+
14+
Drivers expose a custom driver context for functionality specific to the given driver. This can be accessed as a parameter to `createVars` then later accessed with `c.vars`. Read more about the [`createVars` lifecycle hook](/concepts/lifecycle).
15+
1216
## Available Drivers
1317

1418
Choose a driver based on your deployment needs - [Memory](/drivers/memory) for development, [Rivet](/drivers/rivet) or [Cloudflare Workers](/drivers/cloudflare-workers) for production environments, and [Redis](/drivers/redis) for specialized self-hosted scenarios.

docs/platforms/cloudflare-workers.mdx

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,31 @@ If you already have a Cloudflare Workers project and want to add ActorCore, you
189189
</Step>
190190
</Steps>
191191

192+
## Accessing Cloudflare Context And Bindings
193+
194+
[Cloudflare-specific `DurableObjectState`](https://developers.cloudflare.com/durable-objects/api/state/) and environment bindings can be accessed from `createVars`.
195+
196+
```typescript
197+
import { actor } from "actor-core";
198+
199+
const myActor = actor({
200+
// Load Cloudflare-specific variables
201+
createVars: (c, cloudflare) => ({
202+
ctx: cloudflare.ctx,
203+
env: cloudflare.env,
204+
}),
205+
actions: {
206+
foo: async (c) => {
207+
// Access DurableObjectState
208+
await c.vars.ctx.storage.get("foo");
209+
210+
// Access bindings
211+
await c.vars.env.MY_BUCKET.put(key, "foo");
212+
},
213+
}
214+
});
215+
```
216+
192217
## Available Regions
193218

194219
See available regions [here](https://developers.cloudflare.com/durable-objects/reference/data-location/#supported-locations-1).

docs/platforms/rivet.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,28 @@ import CreateActorCli from '/snippets/create-actor-cli.mdx';
9292
</Step>
9393
</Steps>
9494

95+
## Accessing Rivet Context
96+
97+
[Rivet's `ActorContext`](https://rivet.gg/docs/javascript-runtime#the-actor-context-object) can be accessed from `createVars`.
98+
99+
```typescript
100+
import { actor } from "actor-core";
101+
102+
const myActor = actor({
103+
// Load Rivet-specific variables
104+
createVars: (c, rivet) => ({
105+
rivet: rivet.ctx,
106+
}),
107+
actions: {
108+
foo: async (c) => {
109+
// Access ActorContext
110+
c.log.info(`Region: ${c.vars.rivet.metadata.region.name}`);
111+
await c.vars.rivet.kv.get("foo");
112+
},
113+
}
114+
});
115+
```
116+
95117
## Available Regions
96118

97119
Rivet supports deploying your actors to multiple regions automatically. You can specify region preferences in your Rivet project settings in the Rivet Hub.

0 commit comments

Comments
 (0)