Class: SlowLoadableComponent

Inherits:
LoadableComponent show all
Defined in:
lib/loadable_component.rb

Constant Summary

Constants inherited from LoadableComponent

LoadableComponent::VERSION

Instance Method Summary collapse

Methods inherited from LoadableComponent

#load, #loaded?

Constructor Details

#initialize(timeout) ⇒ SlowLoadableComponent

Returns a new instance of SlowLoadableComponent.



41
42
43
# File 'lib/loadable_component.rb', line 41

def initialize(timeout)
  @timeout = timeout
end

Instance Method Details

#check_errorObject

Override this method to check for well-known error cases, which means loading has finished, but an error condition was seen.



69
70
71
# File 'lib/loadable_component.rb', line 69

def check_error
  # no-op by default
end

#getObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/loadable_component.rb', line 45

def get
  if loaded?
    return self
  end

  end_time = Time.now + @timeout
  until Time.now >= end_time
    return self if loaded?
    check_error
    sleep sleep_interval
  end

  unless loaded?
    raise UnableToLoadComponent, unable_to_load_message
  end

  self
end

#sleep_intervalObject



73
74
75
# File 'lib/loadable_component.rb', line 73

def sleep_interval
  0.2
end

#unable_to_load_messageObject



77
78
79
# File 'lib/loadable_component.rb', line 77

def unable_to_load_message
  "#{super} after #{@timeout} seconds"
end