Class: IRuby::Comm

Inherits:
Object
  • Object
show all
Defined in:
lib/iruby/comm.rb

Overview

Comm is a new messaging system for bidirectional communication. Both kernel and front-end listens for messages.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_name, comm_id = SecureRandom.uuid) ⇒ Comm

Returns a new instance of Comm.



12
13
14
# File 'lib/iruby/comm.rb', line 12

def initialize(target_name, comm_id = SecureRandom.uuid)
  @target_name, @comm_id = target_name, comm_id
end

Instance Attribute Details

#on_close(&b) ⇒ Object



34
35
36
# File 'lib/iruby/comm.rb', line 34

def on_close(&b)
  @on_close = b
end

#on_msg(&b) ⇒ Object



30
31
32
# File 'lib/iruby/comm.rb', line 30

def on_msg(&b)
  @on_msg = b
end

Class Method Details

.commObject



9
# File 'lib/iruby/comm.rb', line 9

def comm;   @comm   ||= {} end

.targetObject



8
# File 'lib/iruby/comm.rb', line 8

def target; @target ||= {} end

Instance Method Details

#close(**data) ⇒ Object



25
26
27
28
# File 'lib/iruby/comm.rb', line 25

def close(**data)
  Kernel.instance.session.send(:publish, :comm_close, comm_id: @comm_id, data: data)
  Comm.comm.delete(@comm_id)
end

#handle_close(data) ⇒ Object



42
43
44
# File 'lib/iruby/comm.rb', line 42

def handle_close(data)
  @on_close.call(data) if @on_close
end

#handle_msg(data) ⇒ Object



38
39
40
# File 'lib/iruby/comm.rb', line 38

def handle_msg(data)
  @on_msg.call(data) if @on_msg
end

#open(**data) ⇒ Object



16
17
18
19
# File 'lib/iruby/comm.rb', line 16

def open(**data)
  Kernel.instance.session.send(:publish, :comm_open, comm_id: @comm_id, data: data, target_name: @target_name)
  Comm.comm[@comm_id] = self
end

#send(**data) ⇒ Object



21
22
23
# File 'lib/iruby/comm.rb', line 21

def send(**data)
  Kernel.instance.session.send(:publish, :comm_msg, comm_id: @comm_id, data: data)
end