class AsyncDataProvider

Attributes

thread_abort_on_exception[RW]
thread_priority[RW]

Public Class Methods

new() click to toggle source
# 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

Public Instance Methods

get_data(**args) click to toggle source
# 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?() click to toggle source
# File lib/async_data_provider.rb, line 60
def has_data?
        return !@data.nil?
end

Private Instance Methods

update_data(**args) click to toggle source
# File lib/async_data_provider.rb, line 65
def update_data(**args)
        raise "must be implemented"
end