Class: React::RenderingContext

Inherits:
Object
  • Object
show all
Defined in:
lib/react/rendering_context.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.waiting_on_resourcesObject

Returns the value of attribute waiting_on_resources.



4
5
6
# File 'lib/react/rendering_context.rb', line 4

def waiting_on_resources
  @waiting_on_resources
end

Class Method Details

.as_node(element) ⇒ Object Also known as: delete



49
50
51
52
# File 'lib/react/rendering_context.rb', line 49

def as_node(element)
  @buffer.delete(element)
  element
end

.buildObject



41
42
43
44
45
46
47
# File 'lib/react/rendering_context.rb', line 41

def build
  current = @buffer
  @buffer = []
  return_val = yield @buffer
  @buffer = current
  return_val
end

.improper_render(message, solution) ⇒ Object



114
115
116
117
# File 'lib/react/rendering_context.rb', line 114

def improper_render(message, solution)
  raise "a component's render method must generate and return exactly 1 element or a string.\n"\
        "    #{message}  #{solution}"
end

.raise_render_error(result) ⇒ Object

heurestically raise a meaningful error based on the situation



103
104
105
106
107
108
109
110
111
112
# File 'lib/react/rendering_context.rb', line 103

def raise_render_error(result)
  improper_render 'A different element was returned than was generated within the DSL.',
                  'Possibly improper use of Element#delete.' if @buffer.count == 1
  improper_render "Instead #{@buffer.count} elements were generated.",
                  'Do you want to wrap your elements in a div?' if @buffer.count > 1
  improper_render "Instead the component #{result} was returned.",
                  "Did you mean #{result}()?" if result.try :reactrb_component?
  improper_render "Instead the #{result.class} #{result} was returned.",
                  'You may need to convert this to a string.'
end

.remove_nodes_from_args(args) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/react/rendering_context.rb', line 64

def remove_nodes_from_args(args)
  args[0].each do |key, value|
    begin
      value.as_node if value.is_a?(Element)
    rescue Exception
    end
  end if args[0] && args[0].is_a?(Hash)
end

.render(name, *args, &block) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/react/rendering_context.rb', line 6

def render(name, *args, &block)
  was_outer_most = !@not_outer_most
  @not_outer_most = true
  remove_nodes_from_args(args)
  @buffer ||= [] unless @buffer
  if block
    element = build do
      saved_waiting_on_resources = waiting_on_resources
      self.waiting_on_resources = nil
      run_child_block(name.nil?, &block)
      if name
        buffer = @buffer.dup
        React.create_element(name, *args) { buffer }.tap do |element|
          element.waiting_on_resources = saved_waiting_on_resources || !!buffer.detect { |e| e.waiting_on_resources if e.respond_to?(:waiting_on_resources) }
          element.waiting_on_resources ||= buffer.last.is_a?(String) && waiting_on_resources
        end
      elsif @buffer.last.is_a? React::Element
        @buffer.last.tap { |element| element.waiting_on_resources ||= saved_waiting_on_resources }
      else
        @buffer.last.to_s.span.tap { |element| element.waiting_on_resources = saved_waiting_on_resources }
      end
    end
  elsif name.is_a? React::Element
    element = name
  else
    element = React.create_element(name, *args)
    element.waiting_on_resources = waiting_on_resources
  end
  @buffer << element
  self.waiting_on_resources = nil
  element
ensure
  @not_outer_most = @buffer = nil if was_outer_most
end

.rendered?(element) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/react/rendering_context.rb', line 56

def rendered?(element)
  @buffer.include? element
end

.replace(e1, e2) ⇒ Object



60
61
62
# File 'lib/react/rendering_context.rb', line 60

def replace(e1, e2)
  @buffer[@buffer.index(e1)] = e2
end

.run_child_block(is_outer_scope) ⇒ Object



94
95
96
97
98
99
# File 'lib/react/rendering_context.rb', line 94

def run_child_block(is_outer_scope)
  result = yield
  result = result.to_s.span if result.try :acts_as_string? || result.is_a?(String)
  @buffer << result if result.is_a?(String) || (result.is_a?(React::Element) && @buffer.empty?)
  raise_render_error(result) if is_outer_scope && @buffer != [result]
end