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
#__internal_deliver_event__, #add_event_listener, capture_flag, #deliver_at, #dispatch_event, js_truthy?, #remove_event_listener
Constructor Details
Returns a new instance of BroadcastChannel.
198
199
200
201
202
203
204
|
# File 'lib/dommy/message_channel.rb', line 198
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.
196
197
198
|
# File 'lib/dommy/message_channel.rb', line 196
def name
@name
end
|
Instance Method Details
#__internal_event_parent__ ⇒ Object
275
276
277
|
# File 'lib/dommy/message_channel.rb', line 275
def __internal_event_parent__
nil
end
|
#__js_call__(method, args) ⇒ Object
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
# File 'lib/dommy/message_channel.rb', line 260
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
236
237
238
239
240
241
242
243
|
# File 'lib/dommy/message_channel.rb', line 236
def __js_get__(key)
case key
when "name"
@name
when "onmessage"
@onmessage
end
end
|
#__js_set__(key, value) ⇒ Object
245
246
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/dommy/message_channel.rb', line 245
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
224
225
226
227
228
229
230
|
# File 'lib/dommy/message_channel.rb', line 224
def close
return if @closed
@closed = true
@@registries[@window][@name].delete(self)
nil
end
|
#closed? ⇒ Boolean
232
233
234
|
# File 'lib/dommy/message_channel.rb', line 232
def closed?
@closed
end
|
#post_message(data) ⇒ Object
Also known as:
postMessage
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/dommy/message_channel.rb', line 206
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
|