Class: Rooibos::Command::Mapped

Inherits:
Object
  • Object
show all
Includes:
Custom
Defined in:
lib/rooibos/command.rb

Overview

Wraps another command’s result with a transformation.

Fractal Architecture requires composition. Child fragments produce commands with their own tags. Parent fragments need those results routed back with a parent prefix. Without transformation, update functions become monolithic “God Reducers” that know about every child’s internals.

This command wraps an inner command and transforms its result message. The parent fragment delegates to the child, then intercepts the result and adds its routing prefix. Clean separation. No coupling.

Use it to compose child fragments that return their own commands.

Prefer the Command.map factory method for convenience.

Example

# Using the factory method (recommended)
Command.map(child_command) { |msg| [:sidebar, msg] }

# Using the class directly
Mapped.new(inner_command: child_command, mapper: ->(msg) { [:sidebar, msg] })

Instance Method Summary collapse

Methods included from Custom

#deconstruct_keys, #rooibos_command?

Instance Method Details

#call(out, token) ⇒ Object

Executes the inner command and transforms each message.



489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/rooibos/command.rb', line 489

def call(out, token)
  inner_channel = Concurrent::Promises::Channel.new
  inner_outlet = Outlet.new(inner_channel, lifecycle: out.live)

  Concurrent::Promises.future do
    if inner_command.respond_to?(:call)
      inner_command.call(inner_outlet, token)
    else
      raise ArgumentError, "Inner command must respond to #call"
    end
    inner_channel.push(DONE)
  end

  loop do
    msg = inner_channel.pop
    break if msg.equal?(DONE)
    transformed = mapper.call(msg)
    out.put(*transformed) if transformed
  end
end

#rooibos_cancellation_grace_periodObject

Grace period delegates to inner command.



483
484
485
486
# File 'lib/rooibos/command.rb', line 483

def rooibos_cancellation_grace_period
  inner_command.respond_to?(:rooibos_cancellation_grace_period) ?
    inner_command.rooibos_cancellation_grace_period : 0.1
end