@@ -2,6 +2,8 @@ local M = {}
22
33local pid = require (" rustmail.pid" )
44local daemon_job = nil
5+ local daemon_ready = false
6+ local pending_callbacks = {}
57local prev_keymap = nil
68local augroup = vim .api .nvim_create_augroup (" rustmail" , { clear = true })
79
@@ -20,7 +22,7 @@ function M.setup(opts)
2022 prev_keymap = cfg .toggle_keymap
2123 vim .keymap .set (" n" , cfg .toggle_keymap , function ()
2224 M .toggle ()
23- end , { desc = " Toggle Rustmail TUI" })
25+ end , { desc = " Toggle RustMail TUI" })
2426 end
2527
2628 vim .api .nvim_create_autocmd (" VimLeavePre" , {
5759function M .ensure_daemon (on_ready )
5860 if daemon_job then
5961 if on_ready then
60- on_ready ()
62+ if daemon_ready then
63+ on_ready ()
64+ else
65+ table.insert (pending_callbacks , on_ready )
66+ end
6167 end
6268 return
6369 end
6470
6571 local stale_pid = pid .read ()
6672 if stale_pid then
6773 if pid .is_rustmail (stale_pid ) then
68- pid .clear ()
6974 vim .fn .jobstart ({ " kill" , tostring (stale_pid ) }, {
7075 on_exit = function (_ , code )
7176 vim .schedule (function ()
7277 if code == 0 then
78+ pid .clear ()
7379 M .ensure_daemon (on_ready )
7480 else
7581 vim .notify (" [rustmail] failed to kill stale daemon (pid " .. stale_pid .. " )" , vim .log .levels .WARN )
@@ -115,6 +121,8 @@ function M.ensure_daemon(on_ready)
115121 detach = true ,
116122 on_exit = function ()
117123 daemon_job = nil
124+ daemon_ready = false
125+ pending_callbacks = {}
118126 pid .clear ()
119127 end ,
120128 })
@@ -123,32 +131,38 @@ function M.ensure_daemon(on_ready)
123131 pid .write (vim .fn .jobpid (daemon_job ))
124132 vim .notify (" [rustmail] started daemon on :" .. cfg .port , vim .log .levels .INFO )
125133 if on_ready then
126- local attempts = 0
127- local max_attempts = 20
128- local function poll ()
129- attempts = attempts + 1
130- vim .fn .jobstart ({
131- " curl" ,
132- " -sf" ,
133- " --max-time" ,
134- " 1" ,
135- " http://" .. cfg .host .. " :" .. cfg .port .. " /api/v1/messages?limit=1" ,
136- }, {
137- on_exit = function (_ , poll_code )
138- vim .schedule (function ()
139- if poll_code == 0 then
140- on_ready ()
141- elseif attempts < max_attempts then
142- vim .defer_fn (poll , 250 )
143- else
144- vim .notify (" [rustmail] daemon failed to become ready" , vim .log .levels .WARN )
134+ table.insert (pending_callbacks , on_ready )
135+ end
136+ local attempts = 0
137+ local max_attempts = 20
138+ local function poll ()
139+ attempts = attempts + 1
140+ vim .fn .jobstart ({
141+ " curl" ,
142+ " -sf" ,
143+ " --max-time" ,
144+ " 1" ,
145+ " http://" .. cfg .host .. " :" .. cfg .port .. " /api/v1/messages?limit=1" ,
146+ }, {
147+ on_exit = function (_ , poll_code )
148+ vim .schedule (function ()
149+ if poll_code == 0 then
150+ daemon_ready = true
151+ local cbs = pending_callbacks
152+ pending_callbacks = {}
153+ for _ , cb in ipairs (cbs ) do
154+ cb ()
145155 end
146- end )
147- end ,
148- })
149- end
150- vim .defer_fn (poll , 250 )
156+ elseif attempts < max_attempts then
157+ vim .defer_fn (poll , 250 )
158+ else
159+ vim .notify (" [rustmail] daemon failed to become ready" , vim .log .levels .WARN )
160+ end
161+ end )
162+ end ,
163+ })
151164 end
165+ vim .defer_fn (poll , 250 )
152166 else
153167 vim .notify (" [rustmail] failed to start daemon" , vim .log .levels .ERROR )
154168 end
@@ -165,6 +179,8 @@ function M.stop_daemon()
165179 if daemon_job then
166180 vim .fn .jobstop (daemon_job )
167181 daemon_job = nil
182+ daemon_ready = false
183+ pending_callbacks = {}
168184 pid .clear ()
169185 vim .notify (" [rustmail] daemon stopped" , vim .log .levels .INFO )
170186 return
0 commit comments