forked from nickjvandyke/opencode.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevents.lua
More file actions
67 lines (58 loc) · 1.72 KB
/
events.lua
File metadata and controls
67 lines (58 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
local M = {}
---@class opencode.events.Opts
---
---Whether to subscribe to Server-Sent Events (SSE) from `opencode` and execute `OpencodeEvent:<event.type>` autocmds.
---@field enabled? boolean
---
---Reload buffers edited by `opencode` in real-time.
---Requires `vim.o.autoread = true`.
---@field reload? boolean
---
---@field permissions? opencode.events.permissions.Opts
local heartbeat_timer = vim.uv.new_timer()
local OPENCODE_HEARTBEAT_INTERVAL_MS = 30000
---Subscribe to `opencode`'s Server-Sent Events (SSE) to execute `OpencodeEvent:<event.type>` autocmds.
function M.subscribe()
if not require("opencode.config").opts.events.enabled then
return
end
require("opencode.cli.server")
.get_port(false)
:next(function(port)
require("opencode.cli.client").sse_subscribe(
port,
---@param response opencode.cli.client.Event
function(response)
heartbeat_timer:stop()
heartbeat_timer:start(
OPENCODE_HEARTBEAT_INTERVAL_MS + 5000,
0,
vim.schedule_wrap(require("opencode.events").unsubscribe)
)
vim.api.nvim_exec_autocmds("User", {
pattern = "OpencodeEvent:" .. response.type,
data = {
event = response,
port = port,
},
})
end
)
end)
:catch(function(err)
vim.notify("Failed to subscribe to SSE: " .. err, vim.log.levels.WARN)
end)
end
function M.unsubscribe()
heartbeat_timer:stop()
require("opencode.cli.client").sse_unsubscribe()
vim.api.nvim_exec_autocmds("User", {
pattern = "OpencodeEvent:server.disconnected",
data = {
event = {
type = "server.disconnected",
},
},
})
end
return M