Module: Decider::Reactor

Defined in:
lib/decider/reactor.rb

Defined Under Namespace

Classes: Module

Class Method Summary collapse

Class Method Details

.combine_with_decider(reactor, decider) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/decider/reactor.rb', line 111

def self.combine_with_decider(reactor, decider)
  Decider.define do
    initial_state decider.initial_state

    decide proc { true } do
      fn = ->(commands, events, ds) {
        case commands
        in []
          events
        in [head, *tail]
          new_events = decider.decide(head, ds)
          new_commands = new_events.flat_map { |action_result| reactor.react(action_result) }
          new_state = new_events.reduce(ds, &decider.evolve)

          fn.call(tail + new_commands, events + new_events, new_state)
        end
      }

      fn.call([command], [], state).each { |event| emit event }
    end

    evolve proc { true } do
      decider.evolve(state, event)
    end

    terminal? do
      decider.terminal?(state)
    end
  end
end

.define(&block) ⇒ Object



82
83
84
85
# File 'lib/decider/reactor.rb', line 82

def self.define(&block)
  builder = Builder.new
  builder.build(&block)
end

.lmap_on_action_result(fn, reactor) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/decider/reactor.rb', line 87

def self.lmap_on_action_result(fn, reactor)
  define do
    react proc { true } do
      reactor.react(fn.call(action_result)).each do |action|
        issue action
      end
    end
  end
end

.map_on_action(fn, reactor) ⇒ Object



107
108
109
# File 'lib/decider/reactor.rb', line 107

def self.map_on_action(fn, reactor)
  rmap_on_action(fn, reactor)
end

.rmap_on_action(fn, reactor) ⇒ Object



97
98
99
100
101
102
103
104
105
# File 'lib/decider/reactor.rb', line 97

def self.rmap_on_action(fn, reactor)
  define do
    react proc { true } do
      reactor.react(action_result).each do |action|
        issue fn.call(action)
      end
    end
  end
end