Skip to content

Commit 9533bfd

Browse files
committed
rules of workflows
1 parent 2535e0a commit 9533bfd

File tree

1 file changed

+28
-8
lines changed

1 file changed

+28
-8
lines changed

src/content/docs/workflows/build/rules-of-workflows.mdx

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,34 @@ export class MyWorkflow extends WorkflowEntrypoint {
193193
}
194194
```
195195

196-
### Set sensible retry parameters
197-
198-
TODO
199-
200-
### Name your steps clearly
196+
### Don't mutate your incoming events
201197

202-
TODO
198+
The `event` passed to your Workflow's `run` method is immutable: changes you make to the event are not persisted across steps and/or Workflow restarts.
203199

204-
### Don't mutate your incoming events
200+
```ts
201+
interface MyEvent {
202+
user: string;
203+
data: string;
204+
}
205205

206-
TODO
206+
export class MyWorkflow extends WorkflowEntrypoint {
207+
async run(event: WorkflowEvent<MyEvent>, step: WorkflowStep) {
208+
// 🔴 Bad: Mutating the event
209+
// This will not be persisted across steps and `event.data` will
210+
// take on its original value.
211+
await step.do("bad step that mutates the incoming event", async () => {
212+
let userData = await env.KV.get(event.user)
213+
event.data = userData
214+
})
215+
216+
// ✅ Good: persist data by returning it as state from your step
217+
// Use that state in subsequent steps
218+
let userData = await step.do("good step that returns state", async () => {
219+
return await env.KV.get(event.user)
220+
})
221+
222+
let someOtherData await step.do("following step that uses that state", async () => {
223+
// Access to userData here
224+
// Will always be the same if this step is retried
225+
})
226+
```

0 commit comments

Comments
 (0)