Class: Brainstorm::Debouncer

Inherits:
Neuron
  • Object
show all
Defined in:
lib/brainstorm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_periodObject

Returns the value of attribute quiet_period.



112
113
114
# File 'lib/brainstorm.rb', line 112

def quiet_period
  @quiet_period
end

#stateObject (readonly)

Returns the value of attribute state.



113
114
115
# File 'lib/brainstorm.rb', line 113

def state
  @state
end

#timerObject (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

Returns:

  • (Boolean)


122
123
124
# File 'lib/brainstorm.rb', line 122

def is_asleep?
  @state == :asleep
end

#is_buffering?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/brainstorm.rb', line 126

def is_buffering?
  @state == :buffering
end