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.



13
14
15
# File 'lib/react_on_rails_pro/async_value.rb', line 13

def initialize(task:)
  @task = task
end

Instance Method Details

#html_safeObject



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

def html_safe
  value.html_safe
end

#resolved?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/react_on_rails_pro/async_value.rb', line 23

def resolved?
  @task.finished?
end

#to_sObject



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

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.



19
20
21
# File 'lib/react_on_rails_pro/async_value.rb', line 19

def value
  @task.wait
end