Class: Brainstorm::Debouncer
Instance Attribute Summary collapse
-
#quiet_period ⇒ Object
Returns the value of attribute quiet_period.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#timer ⇒ Object
readonly
Returns the value of attribute timer.
Instance Method Summary collapse
- #call(item) ⇒ Object
-
#initialize(quiet_period) ⇒ Debouncer
constructor
A new instance of Debouncer.
- #is_asleep? ⇒ Boolean
- #is_buffering? ⇒ Boolean
Methods inherited from Neuron
Constructor Details
#initialize(quiet_period) ⇒ Debouncer
Returns a new instance of Debouncer.
115 116 117 118 119 120 |
# File 'lib/brainstorm.rb', line 115 def initialize(quiet_period) @quiet_period = quiet_period @timer = 0 @state = :asleep end |
Instance Attribute Details
#quiet_period ⇒ Object
Returns the value of attribute quiet_period.
112 113 114 |
# File 'lib/brainstorm.rb', line 112 def quiet_period @quiet_period end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
113 114 115 |
# File 'lib/brainstorm.rb', line 113 def state @state end |
#timer ⇒ Object (readonly)
Returns the value of attribute timer.
113 114 115 |
# File 'lib/brainstorm.rb', line 113 def timer @timer end |
Instance Method Details
#call(item) ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/brainstorm.rb', line 131 def call(item) if item.is_a? Start if is_asleep? fire start elsif is_buffering? @buffer.each {|i| fire i} end @state = :in_block elsif item.is_a? Finish @buffer = [] @timer = 0 @state = :buffering else if is_buffering? @buffer.push item @timer += 1 if @timer > @quiet_period fire finish @buffer.each {|i| fire i} @state = :asleep end else fire item end end end |
#is_asleep? ⇒ Boolean
122 123 124 |
# File 'lib/brainstorm.rb', line 122 def is_asleep? @state == :asleep end |
#is_buffering? ⇒ Boolean
126 127 128 |
# File 'lib/brainstorm.rb', line 126 def is_buffering? @state == :buffering end |