Events enable you to inject information into a running workflow instance. For example, if your e-commerce customer wants to change her delivery address it should be possible to do it even after the beginning of the delivery workflow.
An event is simply defined by a name and some properties.
You can easily send an event to a workflow:
client.select.workflow("WelcomeFlow").withTag(email).send("AddressUpdatedEvent", { address: "One Infinite Loop Cupertino, CA 95014" });
Then the workflow instance will handle the event through an
onEvent
method that will receive the event object as a parameter. For example:
*onEvent(name, ...data) {
if (name === 'AddressUpdatedEvent') {
this.address = data.address;
}
}
The onEvent
method is called as soon the event is sent and
an Agent is available to execute it.
Remember the workflow implementation MUST be idempotent. So the constraints on theonEvent
method are the same as thehandle
method (it must implement a logical flow and NOT the tasks themselves.)