Class: Mayu::VDOM::UpdateContext

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mayu/vdom/update_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeUpdateContext

Returns a new instance of UpdateContext.



12
13
14
15
16
17
# File 'lib/mayu/vdom/update_context.rb', line 12

def initialize
  @patches = T.let([], T::Array[T.untyped])
  @parents = T.let([], T::Array[VNode])
  @dom_parent_ids = T.let([], T::Array[VNode::Id])
  @stylesheets = T.let(Set.new, T::Set[String])
end

Instance Attribute Details

#patchesObject (readonly)

Returns the value of attribute patches.



9
10
11
# File 'lib/mayu/vdom/update_context.rb', line 9

def patches
  @patches
end

Instance Method Details

#css(vnode, attr, value = nil) ⇒ Object



119
120
121
122
123
124
125
# File 'lib/mayu/vdom/update_context.rb', line 119

def css(vnode, attr, value = nil)
  if value
    add_patch(:css, id: vnode.dom_id, attr:, value:)
  else
    add_patch(:css, id: vnode.dom_id, attr:)
  end
end

#dom_parent_idObject



32
# File 'lib/mayu/vdom/update_context.rb', line 32

def dom_parent_id = @dom_parent_ids.last || "probably root"

#enter(vnode, &blk) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mayu/vdom/update_context.rb', line 35

def enter(vnode, &blk)
  # Sleep so that the fiber yields,
  # so that other things can run..
  sleep(0)

  dom_parent_id =
    (vnode.descriptor.element? ? vnode.id : vnode.dom_parent_id)

  @parents.push(vnode)
  @dom_parent_ids.push(dom_parent_id) if dom_parent_id
  yield
ensure
  @dom_parent_ids.pop if dom_parent_id
  @parents.pop
end

#insert(vnode, before: nil, after: nil) ⇒ Object



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
87
88
89
# File 'lib/mayu/vdom/update_context.rb', line 58

def insert(vnode, before: nil, after: nil)
  html = vnode.to_html
  ids = vnode.id_tree

  if before
    add_patch(
      :insert,
      id: vnode.dom_id,
      parent: dom_parent_id,
      before: before.dom_id,
      html:,
      ids:
    )
  elsif after
    add_patch(
      :insert,
      id: vnode.dom_id,
      parent: dom_parent_id,
      after: after.dom_id,
      html:,
      ids:
    )
  else
    add_patch(
      :insert,
      id: vnode.dom_id,
      parent: dom_parent_id,
      html:,
      ids:
    )
  end
end

#move(vnode, before: nil, after: nil) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/mayu/vdom/update_context.rb', line 98

def move(vnode, before: nil, after: nil)
  if before
    add_patch(
      :move,
      id: vnode.dom_id,
      parent: vnode.dom_parent_id,
      before: before.dom_id
    )
  elsif after
    add_patch(
      :move,
      id: vnode.dom_id,
      parent: vnode.dom_parent_id,
      after: after.dom_id
    )
  else
    add_patch(:move, id: vnode.dom_id, parent: vnode.dom_parent_id)
  end
end

#parentObject



29
# File 'lib/mayu/vdom/update_context.rb', line 29

def parent = @parents.last

#remove(vnode) ⇒ Object



142
143
144
145
146
147
148
149
150
# File 'lib/mayu/vdom/update_context.rb', line 142

def remove(vnode)
  if vnode.component
    if child = vnode.children.first
      return remove(child)
    end
  end
  # puts "\e[31mremove\e[0m #{vnode.key}"
  add_patch(:remove, id: vnode.dom_id, parent: vnode.dom_parent_id)
end

#remove_attribute(vnode, name) ⇒ Object



158
159
160
# File 'lib/mayu/vdom/update_context.rb', line 158

def remove_attribute(vnode, name)
  add_patch(:attr, id: vnode.dom_id, name: attr_name(name))
end

#set_attribute(vnode, name, value) ⇒ Object



153
154
155
# File 'lib/mayu/vdom/update_context.rb', line 153

def set_attribute(vnode, name, value)
  add_patch(:attr, id: vnode.dom_id, name: attr_name(name), value:)
end

#stylesheet(hash) ⇒ Object



128
129
130
# File 'lib/mayu/vdom/update_context.rb', line 128

def stylesheet(hash)
  @stylesheets.add(hash)
end

#stylesheet_patchObject



20
21
22
23
24
25
26
# File 'lib/mayu/vdom/update_context.rb', line 20

def stylesheet_patch
  return [] if @stylesheets.empty?

  paths = @stylesheets.to_a.map { "/__mayu/static/#{_1}" }

  [{ type: :stylesheet, paths: }]
end

#text(vnode, text, append: false) ⇒ Object



133
134
135
136
137
138
139
# File 'lib/mayu/vdom/update_context.rb', line 133

def text(vnode, text, append: false)
  if append
    add_patch(:text, id: vnode.dom_id, append: text)
  else
    add_patch(:text, id: vnode.dom_id, text:)
  end
end