waterfall icon indicating copy to clipboard operation
waterfall copied to clipboard

Passing arguments to `call` vs using `outflow`

Open artsyca opened this issue 6 years ago • 1 comments

I'm a bit confused by the ambiguity of passing arguments to call on a Waterfall as opposed to attaching them to the outflow context

Generally the arguments being passed are coming from an outflow anyway, I don't know when to use one over the other

Additionally, it seems each object has its own @outflow property, so passing the outflow as a block is not always needed?

artsyca avatar Oct 01 '19 05:10 artsyca

Hello :)

I feel like it's clearer to separate inputs/outputs:

  • inputs are passed in the initializer
  • outflow is the output

Each waterfall is independent and has its own outflow. You can get data from another service by grabbing data from its outflow with your desired local name. Nothing is global or shared per default.

Using or not block params to retrieve outflow is really a matter of context. Lets see this with examples.

If you work with a class, you dont need outflow as block param:

class Services::Registration::AddUser
  include Waterfall

  def initialize(user_params, event_id)
    @user_params = user_params
    @event_id = event_id
  end

  def call
    with_transaction do 
      chain(user: :user) do 
        Services::CreateUser.new(@user_params)
      end
      chain do 
        # here you can access outflow directly as its builtin within the object
        Services::Registration::Subscribe.new(outflow.user, @event_id)
      end
    end
  end
end

But whenever you trigger the flow you need block params. In a controller for instance:

Wf.new
  .chain(user: :user) do 
    Services::Registration::AddUser(user_params, event_id)
  end
  .chain do |outflow| # here you need the outflow from the block
    render json: { user_id: outflow.user.id }
  end
  .on_dam do |error_pool|
    render json: { errors: error_pool }, status: 422
  end

Is it clearer?

apneadiving avatar Oct 01 '19 08:10 apneadiving