Class: AsyncDataProvider

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/async_data_provider.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAsyncDataProvider

Returns a new instance of AsyncDataProvider.



30
31
32
33
34
35
36
37
38
39
# File 'lib/async_data_provider.rb', line 30

def initialize
  @last_time_retrieved       = nil
  @time_retrieve_interval    = "must be implemented"
  @mutex                     = Mutex.new
  @data                      = nil

  # Thread properties
  @thread_abort_on_exception = true
  @thread_priority           = nil
end

Instance Attribute Details

#thread_abort_on_exceptionObject

Returns the value of attribute thread_abort_on_exception.



40
41
42
# File 'lib/async_data_provider.rb', line 40

def thread_abort_on_exception
  @thread_abort_on_exception
end

#thread_priorityObject

Returns the value of attribute thread_priority.



40
41
42
# File 'lib/async_data_provider.rb', line 40

def thread_priority
  @thread_priority
end

Instance Method Details

#get_data(**args) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/async_data_provider.rb', line 42

def get_data(**args)
  if(@last_time_retrieved.nil? || (Time.now - @last_time_retrieved >= @time_retrieve_interval))
    @last_time_retrieved = Time.now

    t = Thread.new do
      if(@mutex.try_lock)
        @data = update_data(args)
        @last_time_retrieved = Time.now
        @mutex.unlock
      end
    end
    t.abort_on_exception = @thread_abort_on_exception
    t.priority           = @thread_priority if @thread_priority
  end

  return @data
end

#has_data?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/async_data_provider.rb', line 60

def has_data?
  return !@data.nil?
end