Class: Interfacets::Client::Channels::React::Dom

Inherits:
Object
  • Object
show all
Defined in:
lib/interfacets/client/channels/react/dom.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(facet:) ⇒ Dom

Returns a new instance of Dom.



10
11
12
13
14
15
# File 'lib/interfacets/client/channels/react/dom.rb', line 10

def initialize(facet:)
  @children = []
  @portals = []
  @handlers = {}
  @facet = facet
end

Instance Attribute Details

#children ⇒ Object

Returns the value of attribute children.



8
9
10
# File 'lib/interfacets/client/channels/react/dom.rb', line 8

def children
  @children
end

#facet ⇒ Object

Returns the value of attribute facet.



8
9
10
# File 'lib/interfacets/client/channels/react/dom.rb', line 8

def facet
  @facet
end

#handlers ⇒ Object

Returns the value of attribute handlers.



8
9
10
# File 'lib/interfacets/client/channels/react/dom.rb', line 8

def handlers
  @handlers
end

Instance Method Details

#call(type, str = nil, **attrs) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/interfacets/client/channels/react/dom.rb', line 49

def call(type, str = nil, **attrs)
  raise("cannot pass both a block and a string") if str && block_given?
  raise if type == :facet

  new_element = {
    type:,
    children: [],
    attributes: (
      attrs.transform_values { |value|
        if value.is_a?(Proc)
          register_handler(
            id: value.object_id.to_s,
            callback: value,
            facet:,
          )

          {
            type: "interfacets:react-dom:action",
            payload: { id: value.object_id.to_s },
          }
        else
          value
        end
      }
    ),
  }

  if block_given? || str
    # recurse
    original_children = @children
    @children = new_element[:children]
    yield if block_given?
    string_node(str) if str
    @children = original_children
  end

  @children << new_element
end

#handle(facet:, payload:) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/interfacets/client/channels/react/dom.rb', line 23

def handle(facet:, payload:)
  handler_id = payload.fetch("id")
  handler = handlers.fetch(handler_id)
  callback = handler.fetch(:callback)
  og_facet = handler.fetch(:facet)

  Evaluator.call(og_facet, callback, payload["event"])
end

#register_handler(id:, callback:, facet:) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/interfacets/client/channels/react/dom.rb', line 32

def register_handler(id:, callback:, facet:)
  @handlers[id] = { facet:, callback: }

  {
    type: "interfacets:react-dom:memoized-action",
    payload: { id: },

  }
end

#reset ⇒ Object



17
18
19
20
21
# File 'lib/interfacets/client/channels/react/dom.rb', line 17

def reset
  @children = []
  @portals = []
  @handlers = {}
end

#string_node(str) ⇒ Object



42
43
44
45
46
47
# File 'lib/interfacets/client/channels/react/dom.rb', line 42

def string_node(str)
  @children << {
    type: "interfacets:string-node",
    attributes: { value: str },
  }
end