Class: TT::DeferredEvent

Inherits:
Object
  • Object
show all
Defined in:
TT_Lib2/deferred_event.rb

Overview

Special Proc like object that limits the frequency it's executed. Designed to be used with change events for TT::GUI::Textbox.

Since:

  • 2.7.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delay = 0.2, &block) ⇒ DeferredEvent

Returns a new instance of DeferredEvent

Parameters:

  • delay (Float) (defaults to: 0.2)

    Maximum frequency the event can be executed.

  • block (Proc)

Since:

  • 2.7.0



22
23
24
25
26
27
28
# File 'TT_Lib2/deferred_event.rb', line 22

def initialize( delay = 0.2, &block )
  @proc = block
  @delay = delay
  @last_value = nil
  @timer = nil
  @suppress_event_if_value_not_changed = true
end

Instance Attribute Details

#suppress_event_if_value_not_changedObject

Since:

  • 2.7.0



16
17
18
# File 'TT_Lib2/deferred_event.rb', line 16

def suppress_event_if_value_not_changed
  @suppress_event_if_value_not_changed
end

Instance Method Details

#call(value) ⇒ Boolean

Returns True is the event was executed.

Parameters:

  • value (Mixed)

    Must be different from last call in order to trigger.

Returns:

  • (Boolean)

    True is the event was executed.

Since:

  • 2.7.0



34
35
36
37
38
39
40
41
42
# File 'TT_Lib2/deferred_event.rb', line 34

def call( value )
  return false if @suppress_event_if_value_not_changed && value == @last_value
  UI.stop_timer( @timer ) if @timer
  @timer = UI.start_timer( @delay, false ) {
    UI.stop_timer( @timer ) # Ensure it only runs once.
    @proc.call( value )
  }
  true
end