Skip to content

Commit b163979

Browse files
committed
Introduce an observer
In a larger refactor of the sidebar, this piece decouples message production from their handlers. Introduces an observer pattern to subscribe to notifications. When the server receives a notification it notifies any subscribers
1 parent 9fc8119 commit b163979

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
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 & 8 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,12 +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-
---@param method string
36-
---@param params table
37-
on_notification = function(server, method, params)
34+
---@param _ eca.Server
35+
---@param message table
36+
on_notification = function(_, message)
3837
return vim.schedule(function()
39-
server:handle_content(method, params)
38+
require("eca.observer").notify(message)
4039
end)
4140
end,
4241
path_finder = PathFinder:new(),
@@ -237,7 +236,7 @@ function M:handle_message(message)
237236
end
238237
elseif message.method and not message.id then
239238
if self.on_notification then
240-
self.on_notification(self, message.method, message.params)
239+
self:on_notification(message)
241240
end
242241
end
243242
end

lua/eca/sidebar.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ local WINDOW_MARGIN = 3 -- Additional margin for window borders and spacing
3333
local UI_ELEMENTS_HEIGHT = 2 -- Reserve space for statusline and tabline
3434
local SAFETY_MARGIN = 2 -- Extra margin to prevent "Not enough room" errors
3535

36+
---@param sidebar eca.Sidebar
37+
---@return fun(message: table)
38+
local function make_handler(sidebar)
39+
return function(message)
40+
sidebar:handle_chat_content(message)
41+
end
42+
end
43+
3644
---@param id integer Tab ID
3745
---@return eca.Sidebar
3846
function M.new(id)
@@ -55,6 +63,8 @@ function M.new(id)
5563
instance._augroup = vim.api.nvim_create_augroup("eca_sidebar_" .. id, { clear = true })
5664
instance._response_start_time = 0
5765
instance._max_response_length = 50000 -- 50KB max response
66+
67+
require("eca.observer").subscribe(id, make_handler(instance))
5868
return instance
5969
end
6070

@@ -1180,6 +1190,12 @@ function M:_send_message(message)
11801190
end
11811191
end
11821192

1193+
function M:handle_chat_content(message)
1194+
if message.params then
1195+
self:handle_chat_content_received(message.params)
1196+
end
1197+
end
1198+
11831199
---@param params table Server content notification
11841200
function M:handle_chat_content_received(params)
11851201
if not params or not params.content then

0 commit comments

Comments
 (0)