Class: Dommy::Js::CustomElementBridge

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/js/custom_element_bridge.rb

Overview

Bridges JS-defined custom elements to Dommy's custom element pipeline. customElements.define(name, JSClass) on the JS side calls in here, which registers a Dommy::HTMLElement subclass for name whose lifecycle reactions (connected/disconnected/adopted/attributeChanged) route back to the JS instance through the bridge. The JS class's constructor itself runs on the JS side via the construction-stack upgrade in host_runtime.js.

Named distinctly from Dommy::CustomElementRegistry (the DOM window.customElements registry); this is the JS<->Dommy wiring, not the registry itself.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bridge) ⇒ CustomElementBridge

Returns a new instance of CustomElementBridge.



18
19
20
21
# File 'lib/dommy/js/custom_element_bridge.rb', line 18

def initialize(bridge)
  @bridge = bridge
  @window = nil
end

Instance Attribute Details

#window=(value) ⇒ Object (writeonly)

Sets the attribute window

Parameters:

  • value

    the value to set the attribute window to.



16
17
18
# File 'lib/dommy/js/custom_element_bridge.rb', line 16

def window=(value)
  @window = value
end

Instance Method Details

#create(name) ⇒ Object

Direct new MyElement(): create a fresh, unattached backing element for a registered tag (its ownerDocument is the window's document). The JS ctor is already running, so the element must NOT be re-upgraded — the bridge crosses it with upgrade suppressed. Returns nil when the tag is undefined.



43
44
45
46
47
48
# File 'lib/dommy/js/custom_element_bridge.rb', line 43

def create(name)
  return unless @window.respond_to?(:custom_elements)
  return unless @window.custom_elements.get(name.to_s)

  @window.document.create_element(name.to_s)
end

#define(name, observed) ⇒ Object



23
24
25
26
27
28
# File 'lib/dommy/js/custom_element_bridge.rb', line 23

def define(name, observed)
  return unless @window.respond_to?(:custom_elements)

  @window.custom_elements.define(name, build_class(name, observed))
  nil
end

#upgrade(root) ⇒ Object

customElements.upgrade(root): delegate to Dommy's registry so a subtree attached without firing reactions gets its registered elements upgraded.



32
33
34
35
36
37
# File 'lib/dommy/js/custom_element_bridge.rb', line 32

def upgrade(root)
  return unless @window.respond_to?(:custom_elements)

  @window.custom_elements.upgrade(root)
  nil
end