class React::RenderingContext

Attributes

waiting_on_resources[RW]

Public Class Methods

as_node(element) click to toggle source
# File lib/react/rendering_context.rb, line 69
def self.as_node(element)
  @buffer.delete(element)
  element
end
Also aliased as: delete
build() { |buffer| ... } click to toggle source
# 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) click to toggle source
# 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
delete(element)
Alias for: as_node
remove_nodes_from_args(args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/react/rendering_context.rb, line 76
def self.replace(e1, e2)
  @buffer[@buffer.index(e1)] = e2
end