prepare for message batching

This commit is contained in:
XeroOl 2025-09-14 18:24:26 -05:00
parent cd9821cd80
commit 03addbf312
2 changed files with 21 additions and 20 deletions

View File

@ -49,7 +49,7 @@
(local server {}) (local server {})
(while true (while true
(let [msg (json-rpc.read in)] (let [msg (json-rpc.read in)]
(dispatch.handle server send msg)))) (dispatch.handle server send [msg]))))
(local {: version} (require :fennel-ls.utils)) (local {: version} (require :fennel-ls.utils))
(local help "Usage: fennel-ls [FLAG] [FILES] (local help "Usage: fennel-ls [FLAG] [FILES]

View File

@ -25,7 +25,7 @@ In general, this involves:
id)))) id))))
(λ handle-response [_server _send _id _result] (λ handle-response [_server _send _id _result]
;; I don't care about responses yet ;; fennel-ls doesn't care about responses yet
nil) nil)
(λ handle-bad-response [_server _send _id err] (λ handle-bad-response [_server _send _id err]
@ -38,7 +38,7 @@ In general, this involves:
callback (callback server send ?params))) callback (callback server send ?params)))
;; Silent error for unknown notifications ;; Silent error for unknown notifications
(λ handle [server send msg] (λ handle [server send batch]
"Figures out what to do with a message. "Figures out what to do with a message.
This can involve updating the state of the server, and/or sending messages to the This can involve updating the state of the server, and/or sending messages to the
server. server.
@ -46,27 +46,28 @@ server.
Takes: Takes:
* `server`, which is the state of the server, * `server`, which is the state of the server,
* `send`, which is a callback for sending responses, and * `send`, which is a callback for sending responses, and
* `msg`, which is the message to receive." * `msgs`, which is a list of incoming messages."
(case (values msg (type msg)) (each [_ msg (ipairs batch)]
{:jsonrpc "2.0" : id : method :params ?params} (case (values msg (type msg))
(handle-request server send id method ?params) {:jsonrpc "2.0" : id : method :params ?params}
{:jsonrpc "2.0" : method :params ?params} (handle-request server send id method ?params)
(handle-notification server send method ?params) {:jsonrpc "2.0" : method :params ?params}
{:jsonrpc "2.0" : id : result} (handle-notification server send method ?params)
(handle-response server send id result) {:jsonrpc "2.0" : id : result}
{:jsonrpc "2.0" : id :error err} (handle-response server send id result)
(handle-bad-response server send id err) {:jsonrpc "2.0" : id :error err}
(str :string) (handle-bad-response server send id err)
(send (message.create-error :ParseError str)) (str :string)
_ (send (message.create-error :ParseError str))
(send (message.create-error :BadMessage nil msg.id))) _
(while (next server.queue) (send (message.create-error :BadMessage nil msg.id)))
(send (table.remove server.queue 1)))) (while (next server.queue)
(send (table.remove server.queue 1)))))
(λ handle* [server msg] (λ handle* [server msg]
"handles a message, and returns all the responses in a table" "handles a message, and returns all the responses in a table"
(let [out []] (let [out []]
(handle server (partial table.insert out) msg) (handle server (partial table.insert out) [msg])
out)) out))
{: handle {: handle