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



65
66
67
68
# File 'lib/react/rendering_context.rb', line 65

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

.buildObject



57
58
59
60
61
62
63
# File 'lib/react/rendering_context.rb', line 57

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

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



7
8
9
10
11
12
# File 'lib/react/rendering_context.rb', line 7

def self.build_only(name, *args, &block)
  React::Component.deprecation_warning(
    '..._as_node is deprecated.  Render component and then use the .node method instead'
  )
  React::RenderingContext.build { React::RenderingContext.render(name, *args, &block) }.to_n
end

.remove_nodes_from_args(args) ⇒ Object



76
77
78
79
80
# File 'lib/react/rendering_context.rb', line 76

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

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



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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/react/rendering_context.rb', line 14

def self.render(name, *args, &block)
  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
      result = block.call
      # Todo figure out how children rendering should happen, probably should have special method that pushes children into the buffer
      # i.e. render_child/render_children that takes Element/Array[Element] and does the push into the buffer
      if !name && (  # !name means called from outer render so we check that it has rendered correctly
          (@buffer.count > 1) || # should only render one element
          (@buffer.count == 1 && @buffer.last != result) || # it should return that element
          (@buffer.count == 0 && !(result.is_a?(String) || (result.respond_to?(:acts_as_string?) && result.acts_as_string?) || result.is_a?(Element))) #for convience we will also convert the return value to a span if its a string
        )
        raise "a components render method must generate and return exactly 1 element or a string"
      end

      @buffer << result.to_s if result.is_a? String || (result.respond_to?(:acts_as_string?) && result.acts_as_string?) # For convience we push the last return value on if its a string
      @buffer << result if result.is_a?(Element) && @buffer.count == 0
      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) }
        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
    # I BELIEVE WAITING ON RESOURCES SHOULD ALREADY BE SET
  else
    element = React.create_element(name, *args)
    element.waiting_on_resources = waiting_on_resources
  end
  @buffer << element
  self.waiting_on_resources = nil
  element
end

.replace(e1, e2) ⇒ Object



72
73
74
# File 'lib/react/rendering_context.rb', line 72

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