Fix race condition in vim connect with ready signal
This commit is contained in:
parent
bbfc9998a5
commit
68a7517dec
3 changed files with 30 additions and 3 deletions
|
|
@ -15,6 +15,9 @@ function send(msg: object) {
|
||||||
console.log(JSON.stringify(msg));
|
console.log(JSON.stringify(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// signal to vim that bridge is ready
|
||||||
|
send({ type: "ready" });
|
||||||
|
|
||||||
function connect(roomName: string) {
|
function connect(roomName: string) {
|
||||||
doc = new Y.Doc();
|
doc = new Y.Doc();
|
||||||
text = doc.getText("content");
|
text = doc.getText("content");
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ vim9script
|
||||||
var bridge_job: job
|
var bridge_job: job
|
||||||
var bridge_channel: channel
|
var bridge_channel: channel
|
||||||
var connected = false
|
var connected = false
|
||||||
|
var ready = false
|
||||||
var room = ""
|
var room = ""
|
||||||
var suppressing = false
|
var suppressing = false
|
||||||
|
|
||||||
|
|
@ -29,7 +30,9 @@ def OnOutput(ch: channel, msg: string)
|
||||||
return
|
return
|
||||||
endtry
|
endtry
|
||||||
|
|
||||||
if data.type == 'connected'
|
if data.type == 'ready'
|
||||||
|
ready = true
|
||||||
|
elseif data.type == 'connected'
|
||||||
connected = true
|
connected = true
|
||||||
echom '[collab] connected to room: ' .. data.room
|
echom '[collab] connected to room: ' .. data.room
|
||||||
elseif data.type == 'disconnected'
|
elseif data.type == 'disconnected'
|
||||||
|
|
@ -72,6 +75,7 @@ export def Connect(room_name: string)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
room = room_name
|
room = room_name
|
||||||
|
ready = false
|
||||||
bridge_job = job_start(['bun', 'run', bridge_script], {
|
bridge_job = job_start(['bun', 'run', bridge_script], {
|
||||||
mode: 'nl',
|
mode: 'nl',
|
||||||
out_cb: OnOutput,
|
out_cb: OnOutput,
|
||||||
|
|
@ -79,8 +83,19 @@ export def Connect(room_name: string)
|
||||||
})
|
})
|
||||||
bridge_channel = job_getchannel(bridge_job)
|
bridge_channel = job_getchannel(bridge_job)
|
||||||
|
|
||||||
# give it a moment to start
|
# wait for bridge ready signal
|
||||||
sleep 100m
|
var timeout = 50
|
||||||
|
while !ready && timeout > 0
|
||||||
|
sleep 10m
|
||||||
|
timeout -= 1
|
||||||
|
endwhile
|
||||||
|
|
||||||
|
if !ready
|
||||||
|
echoerr '[collab] bridge failed to start'
|
||||||
|
Disconnect()
|
||||||
|
return
|
||||||
|
endif
|
||||||
|
|
||||||
Send({type: 'connect', room: room_name})
|
Send({type: 'connect', room: room_name})
|
||||||
|
|
||||||
# set up autocmds to send changes
|
# set up autocmds to send changes
|
||||||
|
|
@ -98,6 +113,7 @@ export def Disconnect()
|
||||||
bridge_channel = null
|
bridge_channel = null
|
||||||
endif
|
endif
|
||||||
connected = false
|
connected = false
|
||||||
|
ready = false
|
||||||
room = ""
|
room = ""
|
||||||
augroup CollabVim
|
augroup CollabVim
|
||||||
autocmd!
|
autocmd!
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,14 @@ export type ServerMessage =
|
||||||
| { type: "awareness"; data: number[] }
|
| { type: "awareness"; data: number[] }
|
||||||
| { type: "peers"; count: number };
|
| { type: "peers"; count: number };
|
||||||
|
|
||||||
|
export type BridgeMessage =
|
||||||
|
| { type: "ready" }
|
||||||
|
| { type: "connected"; room: string }
|
||||||
|
| { type: "disconnected" }
|
||||||
|
| { type: "content"; text: string }
|
||||||
|
| { type: "peers"; count: number }
|
||||||
|
| { type: "error"; message: string };
|
||||||
|
|
||||||
export function encode(msg: ServerMessage): string {
|
export function encode(msg: ServerMessage): string {
|
||||||
return JSON.stringify(msg);
|
return JSON.stringify(msg);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue