-
Notifications
You must be signed in to change notification settings - Fork 70
/
repl.fnl
66 lines (57 loc) · 1.95 KB
/
repl.fnl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
(local coroutine (require :coroutine))
(local fennel (require :fennel))
(local jeejah (require :jeejah))
(local {:merge merge} (require :lib.functional))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; nREPL support
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This module adds support to start an nREPL server. This allows a client to
;; connect to the running server and interact with it while it is running, which
;; can help avoid repeatedly reloading the config.
;;
;; Example usage:
;;
;; - To your ~/.spacehammer/config.fnl add:
;; (local repl (require :repl))
;; (repl.run (repl.start))
;;
;; repl.start takes an optional 'opts' table with the following fields:
;; - host: Define the host to listen on (default "localhost")
;; - port: Define the port to listen on (default 7888)
;; - fennel: Expect fennel code (as opposed to lua) (default true)
;; - serialize: Provide a function that converts objects to strings
;; (default hs.inspect)
(fn fennel-middleware
[f msg]
(match msg.op
"load-file" (let [f (assert (io.open msg.filename "rb"))]
(tset msg
:op "eval"
:code (-> f
(: :read "*all")
(: :gsub "^#![^\n]*\n" "")))
(: f :close))
_ (f msg)))
(local default-opts
{:port nil
:fennel true
:middleware fennel-middleware
:serialize hs.inspect})
(local repl-coro-freq 0.05)
(fn run
[server]
(let [repl-coro server
repl-spin (fn [] (coroutine.resume repl-coro))
repl-chk (fn [] (not= (coroutine.status repl-coro) "dead"))]
(hs.timer.doWhile repl-chk repl-spin repl-coro-freq)))
(fn start
[custom-opts]
(let [opts (merge {} default-opts custom-opts)
server (jeejah.start opts.port opts)]
server))
(fn stop
[server]
(jeejah.stop server))
{: run
: start
: stop}