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
-
.container_for_id(id) ⇒ Object
cf.
- .contains_node(outer_node, inner_node) ⇒ Object
- .find_component_root(ancestor_node, target_id) ⇒ Object
- .find_deepest_cached_ancestor(target_id) ⇒ Object
- .find_first_hyalite_dom(node) ⇒ Object
- .has_non_root_child(node) ⇒ Object
- .instances_by_root_id(root_id) ⇒ Object
- .internal_id(node) ⇒ Object
- .is_rendered(node) ⇒ Object
- .is_valid(node, id) ⇒ Object
- .mount_component_into_node(component_instance, root_id, container, should_reuse_markup) ⇒ Object
- .mount_image_into_node(markup, container, should_reuse_markup) ⇒ Object
- .node(id) ⇒ Object
- .node_cache ⇒ Object
- .node_id(node) ⇒ Object
- .purge_id(id) ⇒ Object
- .register_component(next_component, container) ⇒ Object
- .register_container(container) ⇒ Object
- .render_new_root_component(next_element, container, should_reuse_markup) ⇒ Object
- .render_subtree_into_container(next_element, container, &block) ⇒ Object
- .root_element_in_container(container) ⇒ Object
- .root_id(container) ⇒ Object
- .unmount_component_at_node(container) ⇒ Object
- .unmount_component_from_node(instance, container) ⇒ Object
- .update_root_component(prev_component, next_element, &callback) ⇒ Object
Class Method Details
.container_for_id(id) ⇒ Object
cf. ReactMount#findReactContainerForID
202 203 204 205 |
# File 'lib/hyalite/mount.rb', line 202 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
268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/hyalite/mount.rb', line 268 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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File 'lib/hyalite/mount.rb', line 227 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
254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/hyalite/mount.rb', line 254 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
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/hyalite/mount.rb', line 281 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 |
.has_non_root_child(node) ⇒ Object
98 99 100 101 |
# File 'lib/hyalite/mount.rb', line 98 def has_non_root_child(node) root_id = root_id(container) root_id ? root_id != InstanceHandles.root_id_from_node_id(root_id) : false 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
195 196 197 198 199 |
# File 'lib/hyalite/mount.rb', line 195 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] == Hyalite::Reconciler::SEPARATOR : false end |
.is_valid(node, id) ⇒ Object
216 217 218 219 220 221 222 223 224 225 |
# File 'lib/hyalite/mount.rb', line 216 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
133 134 135 136 137 138 139 |
# File 'lib/hyalite/mount.rb', line 133 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/hyalite/mount.rb', line 141 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
207 208 209 210 211 212 213 214 |
# File 'lib/hyalite/mount.rb', line 207 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_cache ⇒ Object
170 171 172 |
# File 'lib/hyalite/mount.rb', line 170 def node_cache @node_cache ||= {} end |
.node_id(node) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/hyalite/mount.rb', line 178 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
174 175 176 |
# File 'lib/hyalite/mount.rb', line 174 def purge_id(id) @node_cache.delete(id) end |
.register_component(next_component, container) ⇒ Object
111 112 113 114 115 116 117 |
# File 'lib/hyalite/mount.rb', line 111 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
119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/hyalite/mount.rb', line 119 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
157 158 159 160 161 162 163 |
# File 'lib/hyalite/mount.rb', line 157 def root_element_in_container(container) if container.document? container else container.children.first end end |
.root_id(container) ⇒ Object
165 166 167 168 |
# File 'lib/hyalite/mount.rb', line 165 def root_id(container) root_element = root_element_in_container(container) root_element && node_id(root_element) end |
.unmount_component_at_node(container) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/hyalite/mount.rb', line 77 def unmount_component_at_node(container) root_id = root_id(container) component = @instances_by_root_id[root_id] unless component container_has_non_root_child = has_non_root_child(container) container_id = internal_id(container) container_id && container_id == InstanceHandles.root_id_from_node_id(container_id) return false end Hyalite.updates.batched_updates do unmount_component_from_node(component, container) end @instances_by_root_id.delete(root_id) @containers_by_root_id.delete(root_id) true end |
.unmount_component_from_node(instance, container) ⇒ Object
103 104 105 106 107 108 109 |
# File 'lib/hyalite/mount.rb', line 103 def unmount_component_from_node(instance, container) Reconciler.unmount_component(instance) while container.children.length > 0 container.children.last.remove end end |
.update_root_component(prev_component, next_element, &callback) ⇒ Object
305 306 307 308 309 310 311 312 |
# File 'lib/hyalite/mount.rb', line 305 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 |