Class: Musa::Sequencer::BaseSequencer::EveryControl

Inherits:
EventHandler show all
Defined in:
lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb

Overview

Control object for every loops.

Manages lifecycle of every loop including stopping conditions, callbacks, and execution tracking. Extends EventHandler to support event-based control (e.g., launching custom events).

Stopping Conditions

  • duration: Maximum loop duration in bars
  • till: Absolute position to stop at
  • condition: Proc returning true to continue, false to stop
  • manual stop: Call control.stop to halt loop

Callbacks

  • on_stop: Called when loop stops (any reason, including manual stop)
  • after: Called only on natural termination (duration/till/condition/nil-interval), NOT on manual stop

Execution Tracking

  • _start_position: Position when loop started
  • _execution_counter: Number of iterations executed

Examples:

Dynamic control

control = sequencer.every(1r) { |control| puts control._execution_counter }
control.duration(4r)  # Stop after 4 bars
control.on_stop { puts "Finished!" }
control.after(2r) { puts "2 bars after finish" }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, duration: nil, till: nil, condition: nil, on_stop: nil, after_bars: nil, after: nil) ⇒ EveryControl

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates every loop control.

Parameters:

  • parent (EventHandler)

    parent event handler

  • duration (Rational, nil) (defaults to: nil)

    maximum duration in bars

  • till (Rational, nil) (defaults to: nil)

    absolute stop position

  • condition (Proc, nil) (defaults to: nil)

    continuation condition

  • on_stop (Proc, nil) (defaults to: nil)

    stop callback

  • after_bars (Rational, nil) (defaults to: nil)

    delay for after callback

  • after (Proc, nil) (defaults to: nil)

    after callback block



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 125

def initialize(parent, duration: nil, till: nil, condition: nil, on_stop: nil, after_bars: nil, after: nil)
  super parent

  @duration_value = duration
  @till_value = till
  @condition_block = condition

  @do_on_stop = []
  @do_after = []

  @do_on_stop << on_stop if on_stop

  self.after after_bars, &after if after
end

Instance Attribute Details

#_execution_counterInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns number of iterations executed.

Returns:

  • (Integer)

    number of iterations executed



112
113
114
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 112

def _execution_counter
  @_execution_counter
end

#_start_positionRational

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns position when loop started.

Returns:

  • (Rational)

    position when loop started



109
110
111
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 109

def _start_position
  @_start_position
end

#condition_blockProc? (readonly)

Returns condition block (returns true to continue).

Returns:

  • (Proc, nil)

    condition block (returns true to continue)



101
102
103
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 101

def condition_block
  @condition_block
end

#do_afterArray<Hash> (readonly)

Returns after callbacks with delays.

Returns:

  • (Array<Hash>)

    after callbacks with delays



105
106
107
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 105

def do_after
  @do_after
end

#do_on_stopArray<Proc> (readonly)

Returns callbacks when loop stops.

Returns:

  • (Array<Proc>)

    callbacks when loop stops



103
104
105
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 103

def do_on_stop
  @do_on_stop
end

#duration_valueRational? (readonly)

Returns maximum duration in bars.

Returns:

  • (Rational, nil)

    maximum duration in bars



97
98
99
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 97

def duration_value
  @duration_value
end

#till_valueRational? (readonly)

Returns absolute position to stop at.

Returns:

  • (Rational, nil)

    absolute position to stop at



99
100
101
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 99

def till_value
  @till_value
end

Instance Method Details

#after(bars = nil) { ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Registers callback to execute after loop terminates naturally. NOT called on manual stop (.stop).

Examples:

Immediate after callback

control.after { puts "Done" }

Delayed after callback

control.after(2r) { puts "2 bars after stop" }

Parameters:

  • bars (Numeric, nil) (defaults to: nil)

    delay in bars after natural termination (default: 0)

Yields:

  • after callback block



199
200
201
202
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 199

def after(bars = nil, &block)
  bars ||= 0
  @do_after << { bars: bars.rationalize, block: block }
end

#condition { ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sets continuation condition.

Yields:

  • condition block (returns true to continue, false to stop)



169
170
171
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 169

def condition(&block)
  @condition_block = block
end

#duration(value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sets maximum loop duration.

Parameters:

  • value (Numeric)

    duration in bars



147
148
149
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 147

def duration(value)
  @duration_value = value.rationalize
end

#on_stop { ... } ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Registers callback for when loop stops (any reason, including manual stop).

Yields:

  • stop callback block



180
181
182
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 180

def on_stop(&block)
  @do_on_stop << block
end

#till(value) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Sets absolute stop position.

Parameters:

  • value (Numeric)

    position to stop at



158
159
160
# File 'lib/musa-dsl/sequencer/base-sequencer-implementation-every.rb', line 158

def till(value)
  @till_value = value.rationalize
end