Class: Mui::Lsp::RequestManager

Inherits:
Object
  • Object
show all
Defined in:
lib/mui/lsp/request_manager.rb

Overview

Manages pending JSON-RPC requests and their callbacks

Instance Method Summary collapse

Constructor Details

#initializeRequestManager



7
8
9
10
11
# File 'lib/mui/lsp/request_manager.rb', line 7

def initialize
  @next_id = 1
  @pending_requests = {}
  @mutex = Mutex.new
end

Instance Method Details

#cancel(id) ⇒ Object



45
46
47
# File 'lib/mui/lsp/request_manager.rb', line 45

def cancel(id)
  @mutex.synchronize { !@pending_requests.delete(id).nil? }
end

#cancel_allObject



49
50
51
# File 'lib/mui/lsp/request_manager.rb', line 49

def cancel_all
  @mutex.synchronize { @pending_requests.clear }
end

#cleanup_stale(timeout_seconds) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mui/lsp/request_manager.rb', line 53

def cleanup_stale(timeout_seconds)
  now = Time.now
  timed_out = []

  @mutex.synchronize do
    @pending_requests.each do |id, request|
      timed_out << id if now - request[:registered_at] > timeout_seconds
    end

    timed_out.each do |id|
      request = @pending_requests.delete(id)
      request[:callback]&.call(nil, { "code" => -32_603, "message" => "Request timed out" })
    end
  end

  timed_out
end

#handle_response(id, result: nil, error: nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mui/lsp/request_manager.rb', line 25

def handle_response(id, result: nil, error: nil)
  request = @mutex.synchronize { @pending_requests.delete(id) }
  return false unless request

  if error
    request[:callback].call(nil, error)
  else
    request[:callback].call(result, nil)
  end
  true
end

#pending?(id) ⇒ Boolean



37
38
39
# File 'lib/mui/lsp/request_manager.rb', line 37

def pending?(id)
  @mutex.synchronize { @pending_requests.key?(id) }
end

#pending_countObject



41
42
43
# File 'lib/mui/lsp/request_manager.rb', line 41

def pending_count
  @mutex.synchronize { @pending_requests.size }
end

#register(callback) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/mui/lsp/request_manager.rb', line 13

def register(callback)
  @mutex.synchronize do
    id = @next_id
    @next_id += 1
    @pending_requests[id] = {
      callback: callback,
      registered_at: Time.now
    }
    id
  end
end