Skip to content

Commit 51c6167

Browse files
committed
state
1 parent afc175d commit 51c6167

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/content/docs/agents/examples/manage-and-sync-state.mdx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import { Agent } from "@cloudflare/agents";
6262

6363
interface Env {}
6464

65-
//
65+
// Define a type for your Agent's state
6666
interface FlightRecord {
6767
id: string;
6868
departureIata: string;
@@ -73,7 +73,18 @@ interface FlightRecord {
7373

7474
// Pass in the type of your Agent's state
7575
export class MyAgent extends Agent<Env, FlightRecord> {
76-
// This allows this.setState and
76+
// This allows this.setState and the onStateUpdate method to
77+
// be typed:
78+
async onStateUpdate(state: FlightRecord) {
79+
console.log("state updated", state);
80+
}
81+
82+
async someOtherMethod() {
83+
this.setState({
84+
...this.state,
85+
price: this.state.price + 10,
86+
});
87+
}
7788
}
7889
```
7990

src/content/docs/agents/examples/schedule-tasks.mdx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ export class SchedulingAgent extends Agent {
3737
```
3838
</TypeScriptExample>
3939

40+
:::caution
41+
42+
Tasks that set a callback for a method that does not exist wil throw an exception.
43+
44+
:::
45+
4046
You can schedule tasks in multiple ways:
4147

4248
<TypeScriptExample>
@@ -60,12 +66,6 @@ this.cancelSchedule(task.id);
6066

6167
</TypeScriptExample>
6268

63-
:::caution
64-
65-
Tasks that set a callback for a method that does not exist will fail silently.
66-
67-
:::
68-
6969
Calling `await this.schedule` returns a `Schedule`, which includes the task's randomly generated `id`. You can use this `id` to retrieve or cancel the task in the future. It also provides a `type` property that indicates the type of schedule, for example, one of `"scheduled" | "delayed" | "cron"`.
7070

7171
:::note[Maximum scheduled tasks]

0 commit comments

Comments
 (0)