class MyCommand
include Wisper::Publisher
def initialize(dependencies = {})
@token_generator = depdendencies.fetch(:token_generator) do
require 'generate_token'
GenerateToken.new
end
end
def call(params, &block)
yield if block_given?
if all_good?
broadcast(:successful, { id: id })
else
broadcast(:failure, { errors: errors })
end
end
private
def on(event, handler)
# ...
end
end
# app initalization process
System = {}
System[:my_command] = MyCommand.new
# handle request from UI (e.g. in a controller)
System[:my_command].call(params) do
on(:success) { }
on(failure) { }
end
The command must be stateless so one instance can be invoked many times over the lifetime of the application.
TODO: The on
subscribers need to be temporary since we are using one instance.