You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/typescript/message-passing.mdx
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -129,6 +129,11 @@ export async function greetingWorkflow(): Promise<string> {
129
129
This allows you to use Activities, Child Workflows, durable [`workflow.sleep`](https://typescript.temporal.io/api/namespaces/workflow#sleep) Timers, [`workflow.condition`](https://typescript.temporal.io/api/namespaces/workflow#condition) conditions, and more.
130
130
See [Async handlers](#async-handlers) and [Workflow message passing](/encyclopedia/workflow-message-passing) for guidelines on safely using async Signal and Update handlers.
131
131
132
+
- Delay calling [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) until the Workflow initialization needed by Signal (or Update) handlers has finished.
133
+
This is safe because the SDK buffers messages when there are no registered handlers for them.
134
+
Note that [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) will immediately invoke the handler of buffered Signals (or Updates) with matching types.
135
+
This could lead to out-of-order processing of messages with different types.
136
+
132
137
-`setHandler` can take `SignalHandlerOptions` (such as `description` and `unfinishedPolicy`) as described in the API reference docs for [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler).
133
138
134
139
@@ -196,6 +201,10 @@ wf.setHandler(
196
201
This includes the Update ID, which can be useful for deduplication when using Continue-As-New: see [Ensuring your messages are processed exactly once](/encyclopedia/workflow-message-passing#exactly-once-message-processing).
197
202
- Update (and Signal) handlers can be `async`, letting them use Activities, Child Workflows, durable [`workflow.sleep`](https://typescript.temporal.io/api/namespaces/workflow#sleep) Timers, [`workflow.condition`](https://typescript.temporal.io/api/namespaces/workflow#condition) conditions, and more.
198
203
See [Async handlers](#async-handlers) and [Workflow message passing](/encyclopedia/workflow-message-passing) for safe usage guidelines.
204
+
- Delay calling [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) until the Workflow initialization needed by Update (or Signal) handlers has finished.
205
+
This is safe because the SDK buffers messages when there are no registered handlers for them.
206
+
Note that [`workflow.setHandler`](https://typescript.temporal.io/api/namespaces/workflow#sethandler) will immediately invoke the handler of buffered Updates (or Signals) with matching types.
207
+
This could lead to out-of-order processing of messages with different types.
Copy file name to clipboardExpand all lines: docs/encyclopedia/application-message-passing.mdx
+28-6Lines changed: 28 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -220,22 +220,44 @@ Every time the Workflow wakes up--generally, it wakes up when it needs to--it wi
220
220
221
221
This execution is on a single thread–while this means you don’t have to worry about parallelism, you do need to worry about concurrency if you have written Signal and Update handlers that can block. These can run interleaved with the main Workflow and with one another, resulting in potential race conditions. These methods should be made reentrant.
222
222
223
+
#### Initializing the Workflow first {#workflow-initializers}
224
+
225
+
Initialize your Workflow's state before handling messages.
226
+
This prevents your handler from reading uninitialized instance variables.
227
+
228
+
To see why, refer to the [diagram](#message-handler-concurrency).
229
+
It shows that your Workflow processes messages before the first run of your Workflow's main method.
230
+
231
+
The message handler runs first in several scenarios, such as:
232
+
233
+
- When using [Signal-with-Start](#signal-with-start).
234
+
- When your Worker experiences delays, such as when the Task Queue it polls gets backlogged.
235
+
- When messages arrive immediately after a Workflow continues as new but before it resumes.
236
+
237
+
For all languages except Go and TypeScript, use your constructor to set up state.
238
+
Annotate your constructor as a Workflow Initializer and take the same arguments as your Workflow's main method.
239
+
240
+
Note that you can't make blocking calls from your constructor.
241
+
If you need to block, make your Signal or Update handler [wait](#waiting) for an initialization flag.
242
+
243
+
In Go and TypeScript, register any message handlers only after completing initialization.
Here are several common patterns for write operations, Signal and Update handlers. They don't apply to pure read operations, i.e. Queries or [Update Validators](#update-validators):
226
248
227
-
-Synchronous, returning immediately.
228
-
- Waiting for the Workflow to be ready to process them.
249
+
-Returning immediately from a handler
250
+
- Waiting for the Workflow to be ready to process them
229
251
- Kicking off activities and other asynchronous tasks
230
-
- Injecting work into the main Workflow.
231
-
- Finishing handlers before the Workflow completes.
232
-
- Ensuring your messages are processed exactly once.
252
+
- Injecting work into the main Workflow
253
+
- Finishing handlers before the Workflow completes
254
+
- Ensuring your messages are processed exactly once
233
255
234
256
#### Synchronous handlers
235
257
236
258
Synchronous handlers don’t kick off any long-running operations or otherwise block. They're guaranteed to run atomically.
237
259
238
-
#### Waiting
260
+
#### Waiting{#waiting}
239
261
240
262
A Signal or Update handler can block waiting for the Workflow to reach a certain state using a Wait Condition. See the links below to find out how to use this with your SDK.
0 commit comments