As well as matching on the final transaction result, you can subscribe to individual steps and trigger specific behaviors based on their success or failure.
You can subscribe to events from specific steps using #subscribe(step_name: listener)
, or subscribe to all steps via #subscribe(listener)
.
The transaction will broadcast the following events for each step:
step
(withstep_name:
andargs:
, representing the name of the step and an array of arguments passed to the step)step_succeeded
(withstep_name:
,args:
, andvalue:
, which is the return value of the step)step_failed
(withstep_name:
,args:
, andvalue:
)
For example:
= []
include Dry::Transaction
step :validate
step :create
private
# ...
end
# ...
end
end
extend self
user = event[:value]
NOTIFICATIONS << "Started creation of "
end
user = event[:value]
NOTIFICATIONS << " created"
end
user = event[:value]
NOTIFICATIONS << " creation failed"
end
end
create_user = CreateUser.new
create_user.subscribe(create: UserCreationListener)
create_user.call(name: "Jane", email: "jane@doe.com")
NOTIFICATIONS
# => ["Started creation of jane@doe.com", "jane@doe.com created"]
This pub/sub mechanism is provided by dry-events.