Class: Dommy::BroadcastChannel
- Inherits:
-
Object
- Object
- Dommy::BroadcastChannel
show all
- Includes:
- EventTarget
- Defined in:
- lib/dommy/message_channel.rb
Overview
‘BroadcastChannel` — same-origin pub/sub. Dommy keeps a per-window channel registry; sending posts to all other peers on the same name within the same Window.
Constant Summary
collapse
- @@registries =
Hash.new { |h, w| h[w] = Hash.new { |c, n| c[n] = [] } }
Instance Attribute Summary collapse
Instance Method Summary
collapse
#__deliver_event__, #add_event_listener, #dispatch_event, #invoke_listener, #remove_event_listener
Constructor Details
Returns a new instance of BroadcastChannel.
172
173
174
175
176
177
178
|
# File 'lib/dommy/message_channel.rb', line 172
def initialize(window, name)
@window = window
@name = name.to_s
@closed = false
@onmessage = nil
@@registries[window][@name] << self
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
170
171
172
|
# File 'lib/dommy/message_channel.rb', line 170
def name
@name
end
|
Instance Method Details
#__event_parent__ ⇒ Object
245
246
247
|
# File 'lib/dommy/message_channel.rb', line 245
def __event_parent__
nil
end
|
#__js_call__(method, args) ⇒ Object
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
# File 'lib/dommy/message_channel.rb', line 230
def __js_call__(method, args)
case method
when "postMessage"
post_message(args[0])
when "close"
close
when "addEventListener"
add_event_listener(args[0], args[1], args[2])
when "removeEventListener"
remove_event_listener(args[0], args[1])
when "dispatchEvent"
dispatch_event(args[0])
end
end
|
#__js_get__(key) ⇒ Object
210
211
212
213
214
215
216
217
|
# File 'lib/dommy/message_channel.rb', line 210
def __js_get__(key)
case key
when "name"
@name
when "onmessage"
@onmessage
end
end
|
#__js_set__(key, value) ⇒ Object
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/dommy/message_channel.rb', line 219
def __js_set__(key, value)
case key
when "onmessage"
remove_event_listener("message", @onmessage) if @onmessage
@onmessage = value
add_event_listener("message", value) if value
end
nil
end
|
#close ⇒ Object
198
199
200
201
202
203
204
|
# File 'lib/dommy/message_channel.rb', line 198
def close
return if @closed
@closed = true
@@registries[@window][@name].delete(self)
nil
end
|
#closed? ⇒ Boolean
206
207
208
|
# File 'lib/dommy/message_channel.rb', line 206
def closed?
@closed
end
|
#post_message(data) ⇒ Object
Also known as:
postMessage
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/dommy/message_channel.rb', line 180
def post_message(data)
return if @closed
peers = @@registries[@window][@name].reject { |p| p.equal?(self) || p.closed? }
cloned = Dommy.structured_clone(data)
peers.each do |peer|
@window.scheduler.queue_microtask(
proc do
peer.dispatch_event(MessageEvent.new("message", "data" => cloned))
end
)
end
nil
end
|