Class: Terminalwire::V2::Transport::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/terminalwire/v2/transport/memory.rb

Overview

A blocking in-memory duplex transport. Two ends share a pair of queues, so one end's #write is the other end's #read. Used for tests and in-process wiring; production uses a WebSocket-backed transport with the same interface (#read -> bytes/nil, #write(bytes), #close).

Constant Summary collapse

EOF =
:__eof__

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(read_queue:, write_queue:) ⇒ Memory

Returns a new instance of Memory.



20
21
22
23
# File 'lib/terminalwire/v2/transport/memory.rb', line 20

def initialize(read_queue:, write_queue:)
  @read_queue = read_queue
  @write_queue = write_queue
end

Class Method Details

.pairObject



14
15
16
17
18
# File 'lib/terminalwire/v2/transport/memory.rb', line 14

def self.pair
  a = ::Queue.new
  b = ::Queue.new
  [new(read_queue: a, write_queue: b), new(read_queue: b, write_queue: a)]
end

Instance Method Details

#closeObject



35
36
37
# File 'lib/terminalwire/v2/transport/memory.rb', line 35

def close
  @write_queue.push(EOF)
end

#readString?

Returns the next frame's bytes, or nil once closed.

Returns:

  • (String, nil)

    the next frame's bytes, or nil once closed.



26
27
28
29
# File 'lib/terminalwire/v2/transport/memory.rb', line 26

def read
  value = @read_queue.pop
  value == EOF ? nil : value
end

#write(bytes) ⇒ Object



31
32
33
# File 'lib/terminalwire/v2/transport/memory.rb', line 31

def write(bytes)
  @write_queue.push(bytes)
end