Class: AsyncDataProvider
- Inherits:
-
Object
- Object
- AsyncDataProvider
- Includes:
- Singleton
- Defined in:
- lib/async_data_provider.rb
Instance Attribute Summary collapse
-
#thread_abort_on_exception ⇒ Object
Returns the value of attribute thread_abort_on_exception.
-
#thread_priority ⇒ Object
Returns the value of attribute thread_priority.
Instance Method Summary collapse
- #get_data(**args) ⇒ Object
- #has_data? ⇒ Boolean
-
#initialize ⇒ AsyncDataProvider
constructor
A new instance of AsyncDataProvider.
Constructor Details
#initialize ⇒ AsyncDataProvider
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_exception ⇒ Object
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_priority ⇒ Object
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
60 61 62 |
# File 'lib/async_data_provider.rb', line 60 def has_data? return !@data.nil? end |