Skip to content

Commit f22df47

Browse files
authored
Merge pull request #33 from tomgeorge/observer
Decouple message processing and handling
2 parents 1d28662 + b08329f commit f22df47

File tree

3 files changed

+42
-17
lines changed

3 files changed

+42
-17
lines changed

lua/eca/observer.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
local observer = {}
2+
3+
---@type { [integer]: fun(message: table) }
4+
local subscriptions = {}
5+
6+
---@param id integer
7+
---@param on_update fun(message: table)
8+
function observer.subscribe(id, on_update)
9+
subscriptions[id] = on_update
10+
end
11+
12+
function observer.unsubscribe(id)
13+
if subscriptions[id] then
14+
subscriptions[id] = nil
15+
end
16+
end
17+
18+
function observer.notify(message)
19+
for _, fn in pairs(subscriptions) do
20+
fn(message)
21+
end
22+
end
23+
24+
return observer

lua/eca/server.lua

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ local Logger = require("eca.logger")
1212
---@field on_start? function Callback when the server process starts
1313
---@field on_stop function Callback when the server stops
1414
---Called when a notification is received(message without an ID)
15-
---@field on_notification fun(server: eca.Server, method: string, params: table)
16-
---@field capabilities table Server capabilities
15+
---@field on_notification fun(server: eca.Server, message: table)
16+
---@field capabilities eca.ServerCapabilities Server capabilities
1717
---@field private path_finder eca.PathFinder Server path finder
1818
---@field pending_requests {id: fun(err, data)} -- outgoing requests with callbacks
1919
local M = {}
@@ -31,10 +31,11 @@ function M.new(opts)
3131
on_stop = function()
3232
require("eca.logger").notify("Server stopped", vim.log.levels.INFO)
3333
end,
34-
---@param server eca.Server
35-
on_notification = function(server, method, params)
34+
---@param _ eca.Server
35+
---@param message table
36+
on_notification = function(_, message)
3637
return vim.schedule(function()
37-
server:handle_content(method, params)
38+
require("eca.observer").notify(message)
3839
end)
3940
end,
4041
path_finder = PathFinder:new(),
@@ -207,16 +208,6 @@ function M:stop()
207208
self.initialized = false
208209
end
209210

210-
function M:handle_content(method, params)
211-
if method == "chat/contentReceived" then
212-
local eca = require("eca")
213-
local sidebar = eca.get(false)
214-
if sidebar and params then
215-
sidebar:handle_chat_content_received(params)
216-
end
217-
end
218-
end
219-
220211
---@return boolean
221212
function M:is_running()
222213
return self.process and not self.process:is_closing()
@@ -235,7 +226,7 @@ function M:handle_message(message)
235226
end
236227
elseif message.method and not message.id then
237228
if self.on_notification then
238-
self.on_notification(self, message.method, message.params)
229+
self:on_notification(message)
239230
end
240231
end
241232
end

lua/eca/sidebar.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ function M.new(id)
5555
instance._augroup = vim.api.nvim_create_augroup("eca_sidebar_" .. id, { clear = true })
5656
instance._response_start_time = 0
5757
instance._max_response_length = 50000 -- 50KB max response
58+
59+
require("eca.observer").subscribe(id, function(message)
60+
instance:handle_chat_content(message)
61+
end)
5862
return instance
5963
end
6064

@@ -1160,7 +1164,7 @@ function M:_send_message(message)
11601164
-- Include active contexts in the message
11611165
local contexts = self:get_contexts()
11621166
eca.server:send_request("chat/prompt", {
1163-
chatId = nil,
1167+
chatId = self.id,
11641168
requestId = tostring(os.time()),
11651169
message = message,
11661170
contexts = contexts or {},
@@ -1180,6 +1184,12 @@ function M:_send_message(message)
11801184
end
11811185
end
11821186

1187+
function M:handle_chat_content(message)
1188+
if message.params then
1189+
self:handle_chat_content_received(message.params)
1190+
end
1191+
end
1192+
11831193
---@param params table Server content notification
11841194
function M:handle_chat_content_received(params)
11851195
if not params or not params.content then

0 commit comments

Comments
 (0)