Class: Dommy::Internal::MutationCoordinator
- Inherits:
-
Object
- Object
- Dommy::Internal::MutationCoordinator
- Defined in:
- lib/dommy/internal/mutation_coordinator.rb
Overview
Coordinates mutation notification to observers and custom element lifecycle callbacks. Isolates mutation observation and custom element logic from Document’s public API.
Instance Method Summary collapse
-
#initialize(document, observer_manager) ⇒ MutationCoordinator
constructor
A new instance of MutationCoordinator.
- #notify_attribute_changed(element, name, old_value, new_value) ⇒ Object
-
#notify_attribute_mutation(target_node:, attribute_name:, old_value:) ⇒ Object
Fire MutationObserver attribute records.
-
#notify_character_data_mutation(target_node:, old_value:) ⇒ Object
Fire MutationObserver characterData records.
-
#notify_child_list_mutation(target_node:, added_nodes:, removed_nodes:, previous_sibling: nil, next_sibling: nil) ⇒ Object
Fire MutationObserver childList records.
-
#notify_connected(element) ⇒ Object
Fire CustomElement lifecycle: connected (synchronous, before mutation delivery).
-
#notify_connected_subtree(nk) ⇒ Object
Walk a subtree and fire connected/disconnected callbacks for all elements.
- #notify_disconnected(element) ⇒ Object
- #notify_disconnected_subtree(nk) ⇒ Object
- #register_observer(observer) ⇒ Object
- #unregister_observer(observer) ⇒ Object
Constructor Details
#initialize(document, observer_manager) ⇒ MutationCoordinator
Returns a new instance of MutationCoordinator.
8 9 10 11 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 8 def initialize(document, observer_manager) @document = document @observer_manager = observer_manager end |
Instance Method Details
#notify_attribute_changed(element, name, old_value, new_value) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 63 def notify_attribute_changed(element, name, old_value, new_value) return unless element&.respond_to?(:attribute_changed_callback) klass = element.class return unless klass.respond_to?(:observed_attributes) return unless klass.observed_attributes.include?(name.to_s.downcase) element.attribute_changed_callback(name, old_value, new_value) rescue StandardError nil end |
#notify_attribute_mutation(target_node:, attribute_name:, old_value:) ⇒ Object
Fire MutationObserver attribute records
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 120 def notify_attribute_mutation(target_node:, attribute_name:, old_value:) target = @document.wrap_node(target_node) return nil unless target attr = attribute_name.to_s.downcase new_value = target_node[attr] # Custom Element attributeChangedCallback (synchronous) notify_attribute_changed(target, attr, old_value, new_value) @observer_manager.observers_matching(target).each do |observer| entry = observer.find_matching_entry(target) next unless entry && entry[:attributes] filter = entry[:attribute_filter] next if filter && !filter.include?(attr) observer.enqueue( MutationRecord.new( type: "attributes", target: target, attribute_name: attr, old_value: entry[:attribute_old_value] ? old_value : nil ) ) end nil end |
#notify_character_data_mutation(target_node:, old_value:) ⇒ Object
Fire MutationObserver characterData records
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 151 def notify_character_data_mutation(target_node:, old_value:) target = @document.wrap_node(target_node) return nil unless target @observer_manager.observers_matching(target).each do |observer| entry = observer.find_matching_entry(target) next unless entry && entry[:character_data] observer.enqueue( MutationRecord.new( type: "characterData", target: target, old_value: entry[:character_data_old_value] ? old_value : nil ) ) end nil end |
#notify_child_list_mutation(target_node:, added_nodes:, removed_nodes:, previous_sibling: nil, next_sibling: nil) ⇒ Object
Fire MutationObserver childList records
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 76 def notify_child_list_mutation( target_node:, added_nodes:, removed_nodes:, previous_sibling: nil, next_sibling: nil ) target = @document.wrap_node(target_node) return nil unless target return nil if added_nodes.empty? && removed_nodes.empty? wrapped_added = added_nodes.map { |node| @document.wrap_node(node) }.compact wrapped_removed = removed_nodes.map { |node| @document.wrap_node(node) }.compact # Fire Custom Element lifecycle callbacks (synchronous, before MutationObserver microtask) added_nodes.each { |nk| notify_connected_subtree(nk) } removed_nodes.each { |nk| notify_disconnected_subtree(nk) } # Capture previousSibling / nextSibling (the position within target) prev_w = previous_sibling next_w = next_sibling if (prev_w.nil? && next_w.nil?) && !added_nodes.empty? first_nk = added_nodes.first last_nk = added_nodes.last prev_w ||= @document.wrap_node(first_nk.previous) if first_nk.respond_to?(:previous) next_w ||= @document.wrap_node(last_nk.next) if last_nk.respond_to?(:next) end record = MutationRecord.new( type: "childList", target: target, added_nodes: wrapped_added, removed_nodes: wrapped_removed, previous_sibling: prev_w, next_sibling: next_w ) @observer_manager.observers_matching(target).each do |observer| observer.enqueue(record) end nil end |
#notify_connected(element) ⇒ Object
Fire CustomElement lifecycle: connected (synchronous, before mutation delivery)
24 25 26 27 28 29 30 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 24 def notify_connected(element) return unless element&.respond_to?(:connected_callback) element.connected_callback rescue StandardError nil end |
#notify_connected_subtree(nk) ⇒ Object
Walk a subtree and fire connected/disconnected callbacks for all elements
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 41 def notify_connected_subtree(nk) return unless nk.respond_to?(:element?) if nk.element? wrapped = @document.wrap_node(nk) notify_connected(wrapped) if wrapped end nk.children.each { |c| notify_connected_subtree(c) } if nk.respond_to?(:children) end |
#notify_disconnected(element) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 32 def notify_disconnected(element) return unless element&.respond_to?(:disconnected_callback) element.disconnected_callback rescue StandardError nil end |
#notify_disconnected_subtree(nk) ⇒ Object
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 52 def notify_disconnected_subtree(nk) return unless nk.respond_to?(:element?) if nk.element? wrapped = @document.wrap_node(nk) notify_disconnected(wrapped) if wrapped end nk.children.each { |c| notify_disconnected_subtree(c) } if nk.respond_to?(:children) end |
#register_observer(observer) ⇒ Object
13 14 15 16 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 13 def register_observer(observer) @observer_manager.register(observer) nil end |
#unregister_observer(observer) ⇒ Object
18 19 20 21 |
# File 'lib/dommy/internal/mutation_coordinator.rb', line 18 def unregister_observer(observer) @observer_manager.unregister(observer) nil end |