Class: Dommy::MessageChannel

Inherits:
Object
  • Object
show all
Defined in:
lib/dommy/message_channel.rb

Overview

MessageChannel — creates a pair of MessagePorts connected to each other. port1.postMessage(x) queues a TASK (the "post message" task source, not a microtask) that fires a message event on port2, and vice versa — so the message is delivered in a later event-loop turn, after the current task's microtask checkpoint. React's scheduler relies on this to yield as a macrotask.

Spec: https://html.spec.whatwg.org/multipage/web-messaging.html

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(window) ⇒ MessageChannel

Returns a new instance of MessageChannel.



15
16
17
18
19
20
# File 'lib/dommy/message_channel.rb', line 15

def initialize(window)
  @port1 = MessagePort.new(window)
  @port2 = MessagePort.new(window)
  @port1.__internal_entangle__(@port2)
  @port2.__internal_entangle__(@port1)
end

Instance Attribute Details

#port1Object (readonly)

Returns the value of attribute port1.



13
14
15
# File 'lib/dommy/message_channel.rb', line 13

def port1
  @port1
end

#port2Object (readonly)

Returns the value of attribute port2.



13
14
15
# File 'lib/dommy/message_channel.rb', line 13

def port2
  @port2
end

Instance Method Details

#__js_get__(key) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/dommy/message_channel.rb', line 22

def __js_get__(key)
  case key
  when "port1"
    @port1
  when "port2"
    @port2
  else
    Bridge::ABSENT
  end
end