Class: ReactOnRailsPro::AsyncValue

Inherits:
Object
  • Object
show all
Defined in:
lib/react_on_rails_pro/async_value.rb

Overview

AsyncValue wraps an Async task to provide a simple interface for retrieving the result of an async react_component render.

Examples:

async_value = async_react_component("MyComponent", props: { name: "World" })
# ... do other work ...
html = async_value.value  # blocks until result is ready

Instance Method Summary collapse

Constructor Details

#initialize(task:) ⇒ AsyncValue

Returns a new instance of AsyncValue.



26
27
28
# File 'lib/react_on_rails_pro/async_value.rb', line 26

def initialize(task:)
  @task = task
end

Instance Method Details

#html_safeObject



44
45
46
# File 'lib/react_on_rails_pro/async_value.rb', line 44

def html_safe
  value.html_safe
end

#resolved?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/react_on_rails_pro/async_value.rb', line 36

def resolved?
  @task.finished?
end

#to_sObject



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

def to_s
  value.to_s
end

#valueObject

Blocks until result is ready, returns HTML string. If the async task raised an exception, it will be re-raised here.



32
33
34
# File 'lib/react_on_rails_pro/async_value.rb', line 32

def value
  @task.wait
end