Class: Rfd::Preview::Client
- Inherits:
-
Object
- Object
- Rfd::Preview::Client
- Defined in:
- lib/rfd/preview/client.rb
Overview
Client interface for communicating with the preview server from the main process
Instance Attribute Summary collapse
-
#socket_io ⇒ Object
readonly
Returns the value of attribute socket_io.
Instance Method Summary collapse
-
#cancel(request_id) ⇒ Object
Cancel an in-flight request.
- #close ⇒ Object
- #connect ⇒ Object
- #connected? ⇒ Boolean
-
#initialize(socket_path) ⇒ Client
constructor
A new instance of Client.
-
#poll_result ⇒ Object
Non-blocking check for available result.
-
#ready? ⇒ Boolean
Check if there are results ready.
-
#request(item:, width:, height:) ⇒ Object
Submit a preview request for the given item Returns the request ID.
- #shutdown ⇒ Object
-
#wait_result(timeout: 5) ⇒ Object
Blocking wait for result with timeout.
Constructor Details
#initialize(socket_path) ⇒ Client
Returns a new instance of Client.
15 16 17 18 19 20 21 22 |
# File 'lib/rfd/preview/client.rb', line 15 def initialize(socket_path) @socket_path = socket_path @socket = nil @buffer = +'' @results = Queue.new @current_request_id = nil @connected = false end |
Instance Attribute Details
#socket_io ⇒ Object (readonly)
Returns the value of attribute socket_io.
13 14 15 |
# File 'lib/rfd/preview/client.rb', line 13 def socket_io @socket_io end |
Instance Method Details
#cancel(request_id) ⇒ Object
Cancel an in-flight request
63 64 65 66 |
# File 'lib/rfd/preview/client.rb', line 63 def cancel(request_id) return unless request_id && @connected ({type: 'cancel', id: request_id}) end |
#close ⇒ Object
100 101 102 103 104 |
# File 'lib/rfd/preview/client.rb', line 100 def close @connected = false @reader_thread.kill if @reader_thread @socket.close rescue nil if @socket end |
#connect ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'lib/rfd/preview/client.rb', line 24 def connect return if @connected @socket = UNIXSocket.new(@socket_path) @socket.sync = true @connected = true start_reader_thread rescue Errno::ENOENT, Errno::ECONNREFUSED @connected = false end |
#connected? ⇒ Boolean
34 35 36 |
# File 'lib/rfd/preview/client.rb', line 34 def connected? @connected end |
#poll_result ⇒ Object
Non-blocking check for available result
69 70 71 72 73 74 |
# File 'lib/rfd/preview/client.rb', line 69 def poll_result return nil if @results.empty? @results.pop(true) rescue ThreadError nil end |
#ready? ⇒ Boolean
Check if there are results ready
77 78 79 |
# File 'lib/rfd/preview/client.rb', line 77 def ready? !@results.empty? end |
#request(item:, width:, height:) ⇒ Object
Submit a preview request for the given item Returns the request ID
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rfd/preview/client.rb', line 44 def request(item:, width:, height:) return nil unless @connected # Cancel previous request if any cancel(@current_request_id) if @current_request_id @current_request_id = SecureRandom.uuid req = Request.new( id: @current_request_id, file_path: item.path, file_type: item.preview_type, width: width, height: height ) (req.to_h) @current_request_id end |
#shutdown ⇒ Object
92 93 94 95 96 97 98 |
# File 'lib/rfd/preview/client.rb', line 92 def shutdown return unless @connected ({type: 'shutdown'}) @connected = false @reader_thread.kill if @reader_thread @socket.close rescue nil if @socket end |
#wait_result(timeout: 5) ⇒ Object
Blocking wait for result with timeout
82 83 84 85 86 87 88 89 90 |
# File 'lib/rfd/preview/client.rb', line 82 def wait_result(timeout: 5) deadline = Time.now + timeout while Time.now < deadline result = poll_result return result if result sleep 0.01 end nil end |