Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Oct 27, 2024
1 parent f1a4e9c commit 3a43bc9
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 32 deletions.
2 changes: 1 addition & 1 deletion examples/browser-repl/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"vite": "^5.4.8"
},
"scripts": {
"dev": "concurrently 'squint watch --repl' 'vite dev' 'squint nrepl-server --target browser'"
"dev": "concurrently 'squint watch --repl' 'vite dev' 'squint nrepl-server --target browser --port 13333'"
}
}
23 changes: 19 additions & 4 deletions src/squint/nrepl.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,34 @@ function nreplWebSocket () {
async function evalMe(code) {
const updatedCode = code.replace(/import\('(.+?)'\)/g, 'import(\'/@resolve-deps/$1\')');
console.log(updatedCode);
const res = await eval(updatedCode);
var res;
try {
res = await eval(updatedCode);
} catch (e) {
console.log('ex', e);
res = e;
}
console.log(res);
return res;
}

function handleNreplMessage(event) {
async function handleNreplMessage(event) {
let data = event.data;
data = JSON.parse(data);
const id = data.id;
console.log('data', data);
const op = data.op;
var code, ws, res, msg;
switch (op) {
case 'eval':
const code = data.code;
code = data.code;
console.log(code);
evalMe(code);
res = await evalMe(code);
console.log(res);
msg = JSON.stringify({op: 'eval', value: res, id: id});
console.log(msg);
ws = nreplWebSocket();
ws.send(msg);
break;
default: break;
}
Expand Down
70 changes: 43 additions & 27 deletions src/squint/repl/nrepl_server.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -111,34 +111,42 @@

(def !ws-conn (atom nil))
(def !target (atom nil))
(def !response-handler (atom nil))

(defn do-handle-eval [{:keys [ns code file
_load-file? _line] :as request} send-fn]
(->
(js/Promise.resolve code)
(.then compile)
(.then (fn [v]
(println "About to eval:")
(println v)
(if (= :browser @!target)
(if-let [conn @!ws-conn]
(.send conn (js/JSON.stringify
#js {:op "eval"
:code v}))
;; TODO: here comes the websocket code
)
(js/eval v))))
(.then (fn [val]
(send-fn request {"ns" (str @last-ns)
"value" (format-value (:nrepl.middleware.print/print request)
(:nrepl.middleware.print/options request)
val)})))
(.catch (fn [e]
(js/console.error e)
(handle-error send-fn request e)))
(.finally (fn []
(send-fn request {"ns" (str @last-ns)
"status" ["done"]})))))
(let [browser? (= :browser @!target)
error? (atom nil)]
(->
(js/Promise.resolve code)
(.then compile)
(.then (fn [v]
(println "About to eval:")
(println v)
(if browser?
(if-let [conn @!ws-conn]
(.send conn (js/JSON.stringify
#js {:op "eval"
:code v
:id (:id request)}))
(println "No websocket connection to send result to")
;; TODO: here comes the websocket code
)
(js/eval v))))
(.then (fn [val]
(when-not browser?
(send-fn request {"ns" (str @last-ns)
"value" (format-value (:nrepl.middleware.print/print request)
(:nrepl.middleware.print/options request)
val)}))))
(.catch (fn [e]
(js/console.error e)
(reset! error? e)
(handle-error send-fn request e)))
(.finally (fn []
(when (or (not browser?) @error?)
(send-fn request {"ns" (str @last-ns)
"status" ["done"]})))))))

(defn handle-eval [{:keys [ns] :as request} send-fn]
(prn :ns ns)
Expand Down Expand Up @@ -265,6 +273,7 @@
(.setNoDelay ^node-net/Socket socket true)
(let [handler (make-request-handler opts)
response-handler (make-reponse-handler socket)
_ (reset! !response-handler response-handler)
pending (atom nil)]
(.on ^node-net/Socket socket "data"
(fn [data]
Expand All @@ -286,6 +295,13 @@

(def !server (atom nil))

(defn ws-message-handler [data]
(let [data (js/JSON.parse data)
data (js->clj data)]
(when-let [id (get data "id")]
(prn :sending-reply data)
(@!response-handler {:id id} data))))

(defn start-server
"Start nRepl server. Accepts options either as JS object or Clojure map."
[opts]
Expand Down Expand Up @@ -313,9 +329,9 @@
(println "Websocket server running on port 1340")
(.on wss "connection" (fn [conn]
(reset! !ws-conn conn)
#_(.on conn "message"
(.on conn "message"
(fn [data]
(js/console.log "data" data)))
(ws-message-handler data)))
#_(.send conn "something")))
#_(reset! !ws-server wss)))
(.listen server
Expand Down

0 comments on commit 3a43bc9

Please sign in to comment.