Class: Dommy::BroadcastChannel
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
included
#__dommy_dump_event_failure__, #__internal_deliver_event__, #__internal_process_event_handler_return__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, #event_name_from_on, #invoke_listener_isolated, js_truthy?, #on_handler, #remove_event_listener, #set_on_handler
Constructor Details
Returns a new instance of BroadcastChannel.
209
210
211
212
213
214
215
|
# File 'lib/dommy/message_channel.rb', line 209
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.
207
208
209
|
# File 'lib/dommy/message_channel.rb', line 207
def name
@name
end
|
Instance Method Details
#__internal_event_parent__ ⇒ Object
291
292
293
|
# File 'lib/dommy/message_channel.rb', line 291
def __internal_event_parent__
nil
end
|
#__js_call__(method, args) ⇒ Object
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
# File 'lib/dommy/message_channel.rb', line 276
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], args[2])
when "dispatchEvent"
dispatch_event(args[0])
end
end
|
#__js_get__(key) ⇒ Object
250
251
252
253
254
255
256
257
258
259
|
# File 'lib/dommy/message_channel.rb', line 250
def __js_get__(key)
case key
when "name"
@name
when "onmessage"
@onmessage
else
Bridge::ABSENT
end
end
|
#__js_set__(key, value) ⇒ Object
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/dommy/message_channel.rb', line 261
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
else
return Bridge::UNHANDLED
end
nil
end
|
#close ⇒ Object
238
239
240
241
242
243
244
|
# File 'lib/dommy/message_channel.rb', line 238
def close
return if @closed
@closed = true
@@registries[@window][@name].delete(self)
nil
end
|
#closed? ⇒ Boolean
246
247
248
|
# File 'lib/dommy/message_channel.rb', line 246
def closed?
@closed
end
|
#post_message(data) ⇒ Object
Also known as:
postMessage
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/dommy/message_channel.rb', line 217
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.set_timeout(
proc do
peer.dispatch_event(MessageEvent.new("message", "data" => cloned))
end,
0
)
end
nil
end
|