Support custom state for event handlers #400
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
An event handler can define and update custom state which is held in the
GenServer
process' memory. It is passed to thehandle/2
function as part of the metadata using the:state
key. The state is transient and will be lost whenever the process restarts.Initial state can be set in the
init/1
callback function by adding a:state
key to the config. It can also be provided when starting the handler process:Or when supervised:
State can be updated by returning
{:ok, new_state}
from anyhandle/2
function. Returning an:ok
reply will keep the state unchanged.Handler state is also included in the
Commanded.Event.FailureContext
struct passed to theerror/3
callback function.Example
Closes #366.