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_resources ⇒ Object

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



69
70
71
72
# File 'lib/react/rendering_context.rb', line 69

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

.build(&block) ⇒ Object



58
59
60
61
62
63
64
65
66
67
# File 'lib/react/rendering_context.rb', line 58

def self.build(&block)
  current = @buffer
  @buffer = []
  return_val = yield @buffer
  @buffer = current
  return_val
#ensure
#  @buffer = current
#  return_val
end

.build_or_render(node_only, name, *args, &block) ⇒ Object



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

def self.build_or_render(node_only, name, *args, &block)
  if node_only
    React::RenderingContext.build { React::RenderingContext.render(name, *args, &block) }.to_n
  else
    React::RenderingContext.render(name, *args, &block)
  end
end

.remove_nodes_from_args(args) ⇒ Object



80
81
82
83
84
# File 'lib/react/rendering_context.rb', line 80

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



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
56
# File 'lib/react/rendering_context.rb', line 15

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



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

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