Class: Terminalwire::V2::Transport::Memory
- Inherits:
-
Object
- Object
- Terminalwire::V2::Transport::Memory
- 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
- #close ⇒ Object
-
#initialize(read_queue:, write_queue:) ⇒ Memory
constructor
A new instance of Memory.
-
#read ⇒ String?
The next frame's bytes, or nil once closed.
- #write(bytes) ⇒ Object
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
.pair ⇒ Object
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
#close ⇒ Object
35 36 37 |
# File 'lib/terminalwire/v2/transport/memory.rb', line 35 def close @write_queue.push(EOF) end |
#read ⇒ String?
Returns 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 |