Skip to content
This repository has been archived by the owner on Jul 24, 2019. It is now read-only.

Commit

Permalink
Now most of Calva works again, keeping the socket open
Browse files Browse the repository at this point in the history
  • Loading branch information
PEZ committed Dec 20, 2018
1 parent 39ad390 commit 8aa4061
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 298 deletions.
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
"devDependencies": {
"shadow-cljs": "^2.7.8"
},
"dependencies": {
"paredit.js": "^0.3.4",
"parinfer": "^3.12.0"
},
"dependencies": {},
"license": "MIT"
}
}
34 changes: 1 addition & 33 deletions packages/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions packages/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"dependencies": {
"paredit.js": "^0.3.4",
"parinfer": "^3.12.0",
"bencoder": "^0.0.5",
"bencode": "^2.0.0",
"buffer": "^5.2.1"
"bencoder": "^0.0.5"
}
}
3 changes: 1 addition & 2 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{:dependencies [[pez/cljfmt "0.0.3-SNAPSHOT"]
#_[zprint "0.4.11"]
[cider/cider-nrepl "0.19.0-SNAPSHOT"]
[binaryage/oops "0.6.4"]]
[cider/cider-nrepl "0.19.0-SNAPSHOT"]]

:source-paths ["src" "test"]

Expand Down
116 changes: 39 additions & 77 deletions src/calva/repl/client.cljs
Original file line number Diff line number Diff line change
@@ -1,101 +1,62 @@
(ns calva.repl.client
(:require
["net" :as net]
[clojure-party-repl.bencode :as bencode]
[clojure.string :as string]
["bencoder" :as bencoder]
["buffer" :as buf]
[talky.socket-client :as talky]
[calva.js-utils :refer [cljify jsify]]))

(def ^:private CONTINUATION_ERROR_MESSAGE
"Unexpected continuation: \"")

(defn make-socket-client
[{:socket/keys [host port config on-connect on-close on-data]
:or {config
{:socket/encoder
(fn [data]
;; See https://nodejs.org/api/net.html#net_socket_write_data_encoding_callback
data)

;; You can also set the encoding.
;; See https://nodejs.org/api/net.html#net_socket_setencoding_encoding
;; :socket/encoding "utf8"
(defn- decode [buffers]
(mapcat
(fn [^js buffer]
(try
(-> (bencoder/decode buffer)
(cljify)
(vector))
(catch js/Error e
(let [exception-message (.-message e)]
(if (string/includes? exception-message CONTINUATION_ERROR_MESSAGE)
(let [recoverable-content (subs exception-message (count CONTINUATION_ERROR_MESSAGE))
recoverable-buffer (.slice buffer 0 (- (.-length buffer) (count recoverable-content)))]
(decode [recoverable-buffer (buf/Buffer.from recoverable-content)]))
(js/console.error "FAILED TO DECODE" exception-message))))))
buffers))

:socket/decoder
(fn [buffer-or-string]
;; See https://nodejs.org/api/net.html#net_event_data
buffer-or-string)}

on-connect
(fn [socket]
;; Do stuff and returns nil.
nil)

on-close
(fn [socket error?]
;; Do stuff and returns nil.
nil)

on-data
(fn [socket buffer-or-string]
;; Do stuff and returns nil.
nil)}
:as socket}]
(let [net-socket (doto (net/connect #js {:host host :port port})
(.once "connect"
(fn []
(on-connect socket)))
(.once "close"
(fn [error?]
(on-close socket error?)))
(.on "data"
(fn [buffer]
(let [{:socket/keys [decoder]} config]
(on-data socket (decoder buffer))))))

net-socket (if-let [encoding (:socket/encoding config)]
(.setEncoding net-socket encoding)
net-socket)]
{:socket.api/write!
(fn write [data]
(let [{:socket/keys [encoder]} config]
(.write ^js net-socket (encoder data))))

:socket.api/end!
(fn []
(.end ^js net-socket))

:socket.api/connected?
(fn []
(not (.-pending ^js net-socket)))}))


(defn make-nrepl-client
(defn- make-nrepl-client
[{:keys [host port on-connect]}]
(let [*results (atom {})

config
{:socket/encoding "binary"
{:socket/encoding "utf8"

:socket/decoder
(fn [buffer-or-string]
(bencode/decode buffer-or-string))
(fn [chunk]
(let [buffer (buf/Buffer.from chunk)]
(when (= 0 (.-length buffer))
(js/console.warn "EMPTY BUFFER"))
(not-empty (decode [buffer]))))
;;(bencode/decode chunk))

:socket/encoder
(fn [data]
(bencode/encode (jsify data)))}

; on-connect
; (fn [_]
; (js/console.log "Connected."))
(bencoder/encode (jsify data)))}

on-close
(fn [_ error?]
(js/console.log "Disconnected."))

;; TODO
on-data
(fn [_ decoded-messages]
(dorun (map (fn [decoded]
(when-let [d-id (:id decoded)]
(let [cb (get-in @*results [d-id :callback])
results (get-in (swap! *results update-in [d-id :results] (fnil conj []) decoded) [d-id :results])]
results (get-in (swap! *results update-in [d-id :results]
(fnil conj []) decoded) [d-id :results])]
(when (some #{"done"} (:status decoded))
(println (pr-str "*results cb" @*results))
(swap! *results dissoc d-id)
Expand All @@ -104,7 +65,7 @@
(println (pr-str "*results pending" @*results)))

{:socket.api/keys [write! connected?]}
(make-socket-client
(talky/make-socket-client
#:socket {:host host
:port (js/parseInt port)
:config config
Expand All @@ -114,11 +75,12 @@
{:nrepl.api/send!
(fn [message callback]
(when connected?
(let [id (str (random-uuid))
message (assoc message :id id)]
(swap! *results assoc id {:nrepl.message/id id
:nrepl.message/callback callback})
(write! message)
(let [id (str (random-uuid))]
(swap! *results assoc id {:id id
:callback callback
:message :message
:results []})
(write! (assoc message :id id))
id)))}))


Expand Down
78 changes: 0 additions & 78 deletions src/calva/repl/nrepl.cljs

This file was deleted.

Loading

0 comments on commit 8aa4061

Please sign in to comment.