honeylisp/link/nrepl-session.fnl

72 lines
2 KiB
Plaintext
Raw Normal View History

2020-11-08 20:36:38 +00:00
(local Object (require :core.object))
(local nrepl (require :link.nrepl))
(local lume (require :lib.lume))
(local Session (Object:extend))
(fn Session.new [self ?handlers]
(set self.queue [])
(set self.in-progress false)
(set self.handlers ?handlers))
(fn Session.init-session [self]
(when (nrepl:connected?)
(self:do #(nrepl:new-session
#(do (set self.session $2)
(self:done-msg))
(self:make-handlers)))))
2020-11-10 03:58:57 +00:00
(fn Session.shutdown-session [self]
(set self.queue [])
(set self.in-progress false)
(set self.sesion nil))
2020-11-08 20:36:38 +00:00
(fn Session.cleanup-handlers [self]
{:status/done #(self:done-msg)
:status/interrupted #(self:done-msg)})
(fn Session.make-handlers [self]
(lume.merge
(or self.handlers {})
(nrepl:chain-handlers [:status/done :status/interrupted]
(or self.handlers {})
(self:cleanup-handlers))))
(fn Session.coro-handlers [self coro ?handlers]
(lume.merge
(or ?handlers {})
(nrepl:chain-handlers [:status/done :status/interrupted]
(self:cleanup-handlers)
{:status/done #(coroutine.resume coro)
:status/interrupted #(coroutine.resume coro)})))
2020-11-10 03:58:57 +00:00
2020-11-08 20:36:38 +00:00
(fn Session.do [self f]
(if self.in-progress (table.insert self.queue f)
(do (set self.in-progress true)
(f))))
(fn Session.done-msg [self]
(if (> (length self.queue) 0) ((table.remove self.queue 1))
(set self.in-progress false)))
(fn Session.send [self message ?handlers]
(self:do #(nrepl:send message ?handlers self.session)))
(fn Session.send-oob [self message ?handlers]
(local handlers
(lume.merge
(nrepl:chain-handlers [:status/done :status/interrupted]
(or self.handlers {}))
(or ?handlers {})))
(nrepl:send message handlers self.session))
(fn Session.eval [self code ?handlers]
(self:send {:op :eval : code} ?handlers))
2020-11-10 03:58:57 +00:00
(fn Session.eval-input [self code input ?handlers]
(self:send {:op :eval : code}
(lume.merge (or ?handlers {})
{:status/need-input #(self:send-oob {:op :stdin :stdin input})})))
2020-11-08 20:36:38 +00:00
Session