edtris/editor/replsession.fnl

36 lines
1.3 KiB
Plaintext
Raw Normal View History

(local util (require :lib.util))
(local fennel (require :lib.fennel))
(local replsession (util.hot-table ...))
(set replsession.sessions {})
(fn replsession.new-session []
(let [run (fn [session]
(fennel.repl {:readChunk coroutine.yield
:onValues #(session.callback {:vals $1})
:onError #(session.callback {:errType $1 :err $2 :luaSource $3 :traceback (fennel.traceback)})
:pp #$1
:env (lume.clone _G)}))
session {:coro (coroutine.create run) :submit replsession.submit}]
(coroutine.resume session.coro session)
session))
(fn replsession.submit [session chunk callback]
(assert (= session.callback nil))
(set session.callback callback)
(coroutine.resume session.coro (.. chunk "\n"))
(assert (= session.callback callback))
(set session.callback nil))
(fn replsession.activate [id] (set replsession.active-session-id id))
(fn replsession.session-id [?id] (or ?id replsession.active-session-id :REPL))
(fn replsession.session [?id]
(let [id (replsession.session-id ?id)
session (or (. replsession.sessions id) (replsession.new-session))]
(tset replsession.sessions id session)
session))
replsession.hot