Module: Hyalite::Mount

Defined in:
lib/hyalite/mount.rb

Constant Summary collapse

ID_ATTR_NAME =
'data-hyalite-id'
CHECKSUM_ATTR_NAME =
'data-react-checksum'

Class Method Summary collapse

Class Method Details

.container_for_id(id) ⇒ Object

cf. ReactMount#findReactContainerForID



168
169
170
171
# File 'lib/hyalite/mount.rb', line 168

def container_for_id(id)
  root_id = InstanceHandles.root_id_from_node_id(id)
  @containers_by_root_id[root_id]
end

.contains_node(outer_node, inner_node) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/hyalite/mount.rb', line 234

def contains_node(outer_node, inner_node)
  case
  when outer_node.nil? || inner_node.nil?
    false
  when outer_node == inner_node
    true
  when outer_node.text?
    false
  else
    contains_node(outer_node, inner_node.parent)
  end
end

.find_component_root(ancestor_node, target_id) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/hyalite/mount.rb', line 193

def find_component_root(ancestor_node, target_id)
  deepest_ancestor = find_deepest_cached_ancestor(target_id) || ancestor_node

  first_children = [ deepest_ancestor.children.first ]

  while first_children.length > 0
    child = first_children.shift

    while child
      child_id = node_id(child)
      if child_id
        if target_id == child_id
          return child
        elsif InstanceHandles.is_ancestor_id_of(child_id, target_id)
          first_children = [ child.children.first ]
        end
      else
        first_children.push(child.children.first)
      end

      child = child.next_sibling
    end
  end

  raise "can't find component_root ancestor_node: #{ancestor_node}, target_id: #{target_id}"
end

.find_deepest_cached_ancestor(target_id) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/hyalite/mount.rb', line 220

def find_deepest_cached_ancestor(target_id)
  deepest_node_so_far = nil
  InstanceHandles.traverse_ancestors(target_id) do |ancestor_id|
    ancestor = node_cache[ancestor_id]
    if ancestor && is_valid(ancestor, ancestor_id)
      deepest_node_so_far = ancestor;
    else
      false
    end
  end

  deepest_node_so_far
end

.find_first_hyalite_dom(node) ⇒ Object



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/hyalite/mount.rb', line 247

def find_first_hyalite_dom(node)
  while node && node.parent
    unless node.element? && node_id = internal_id(node)
      node = node.parent
      next
    end

    root_id = InstanceHandles.root_id_from_node_id(node_id)

    current = node
    loop do
      last_id = internal_id(current)
      return nil unless current = current.parent
      break if last_id == root_id
    end

    return node if current == @containers_by_root_id[root_id]

    node = node.parent
  end

  nil
end

.instances_by_root_id(root_id) ⇒ Object



21
22
23
# File 'lib/hyalite/mount.rb', line 21

def instances_by_root_id(root_id)
  @instances_by_root_id[root_id]
end

.internal_id(node) ⇒ Object



161
162
163
164
165
# File 'lib/hyalite/mount.rb', line 161

def internal_id(node)
  if node.element?
    node.attributes[ID_ATTR_NAME]
  end
end

.is_rendered(node) ⇒ Object



59
60
61
62
63
64
# File 'lib/hyalite/mount.rb', line 59

def is_rendered(node)
  return false unless node.element?

  id = node_id(node)
  id ? id[0] == SEPARATOR : false
end

.is_valid(node, id) ⇒ Object



182
183
184
185
186
187
188
189
190
191
# File 'lib/hyalite/mount.rb', line 182

def is_valid(node, id)
  if node
    container = @containers_by_root_id[id]
    if container && contains_node(container, node)
      return true
    end
  end

  false
end

.mount_component_into_node(component_instance, root_id, container, should_reuse_markup) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/hyalite/mount.rb', line 99

def mount_component_into_node(component_instance, root_id, container, should_reuse_markup)
  Hyalite.updates.reconcile_transaction.perform do |transaction|
    markup = Reconciler.mount_component(component_instance, root_id, transaction.mount_ready, {})
    component_instance.rendered_component.top_level_wrapper = component_instance
    mount_image_into_node(markup, container, should_reuse_markup)
  end
end

.mount_image_into_node(markup, container, should_reuse_markup) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/hyalite/mount.rb', line 107

def mount_image_into_node(markup, container, should_reuse_markup)
  if should_reuse_markup
    root_element = root_element_in_container(container)
    checksum = Adler32.checksum markup
    checksum_attr = root_element.attributes[CHECKSUM_ATTR_NAME]
    if checksum == checksum_attr
      return
    end

    root_element.attributes.remove(CHECKSUM_ATTR_NAME)
    root_element.attributes[CHECKSUM_ATTR_NAME] = checksum
  end

  container.inner_dom = markup
end

.node(id) ⇒ Object



173
174
175
176
177
178
179
180
# File 'lib/hyalite/mount.rb', line 173

def node(id)
  node =  node_cache[id]
  unless node && is_valid(node, id)
    root_id = InstanceHandles.root_id_from_node_id(id)
    node = node_cache[id] = find_component_root(@containers_by_root_id[root_id], id)
  end
  node
end

.node_cacheObject



136
137
138
# File 'lib/hyalite/mount.rb', line 136

def node_cache
  @node_cache ||= {}
end

.node_id(node) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/hyalite/mount.rb', line 144

def node_id(node)
  id = internal_id(node)
  if id
    if node_cache.has_key?(id)
      cached = node_cache[id]
      if cached != node
        #raise "Mount: Two valid but unequal nodes with the same `#{ID_ATTR_NAME}`: #{id}"
        node_cache[id] = node
      end
    else
      node_cache[id] = node
    end
  end

  id
end

.purge_id(id) ⇒ Object



140
141
142
# File 'lib/hyalite/mount.rb', line 140

def purge_id(id)
  @node_cache.delete(id)
end

.register_component(next_component, container) ⇒ Object



77
78
79
80
81
82
83
# File 'lib/hyalite/mount.rb', line 77

def register_component(next_component, container)
  #ReactBrowserEventEmitter.ensureScrollValueMonitoring();

  root_id = register_container(container)
  @instances_by_root_id[root_id] = next_component;
  root_id
end

.register_container(container) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/hyalite/mount.rb', line 85

def register_container(container)
  root_id = root_id(container)
  if root_id
    root_id = InstanceHandles.root_id_from_node_id(root_id)
  end

  unless root_id
    root_id = InstanceHandles.create_root_id
  end

  @containers_by_root_id[root_id] = container
  root_id
end

.render_new_root_component(next_element, container, should_reuse_markup) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/hyalite/mount.rb', line 66

def render_new_root_component(next_element, container, should_reuse_markup)
  component_instance = Hyalite.instantiate_component(next_element)
  root_id = register_component(component_instance, container)

  Hyalite.updates.batched_updates do
    mount_component_into_node(component_instance, root_id, container, should_reuse_markup)
  end

  component_instance
end

.render_subtree_into_container(next_element, container, &block) ⇒ Object



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
57
# File 'lib/hyalite/mount.rb', line 25

def render_subtree_into_container(next_element, container, &block)
  next_wrapped_element = ElementObject.new(TopLevelWrapper, nil, nil, nil, next_element)
  prev_component = @instances_by_root_id[root_id(container)]
  if prev_component
    prev_wrapped_element = prev_component.current_element
    prev_element = prev_wrapped_element.props;
    if Reconciler.should_update_component(prev_element, next_element)
      return update_root_component(
        prev_component,
        next_wrapped_element,
        &block
      ).rendered_component.public_instance
    else
      unmount_component_at_node(container)
    end
  end

  root_element = root_element_in_container(container)
  container_has_markup = root_element && is_rendered(root_element)
  should_reuse_markup = container_has_markup && prev_component.nil?

  component = render_new_root_component(
    next_wrapped_element,
    container,
    should_reuse_markup
  ).rendered_component.public_instance

  if block_given?
    yield component
  end

  component
end

.root_element_in_container(container) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/hyalite/mount.rb', line 123

def root_element_in_container(container)
  if container.document?
    $document
  else
    container.children.first
  end
end

.root_id(container) ⇒ Object



131
132
133
134
# File 'lib/hyalite/mount.rb', line 131

def root_id(container)
  root_element = root_element_in_container(container)
  root_element && node_id(root_element)
end

.update_root_component(prev_component, next_element, &callback) ⇒ Object



271
272
273
274
275
276
277
278
# File 'lib/hyalite/mount.rb', line 271

def update_root_component(prev_component, next_element, &callback)
  UpdateQueue.enqueue_element_internal(prev_component, next_element)
  if block_given?
    UpdateQueue.enqueue_callback_internal(prev_component, &callback)
  end

  prev_component
end