<commit_before>(ns lein-ui.nrepl.http
  (:require [org.httpkit.server :as server]
            [clojure.tools.nrepl :as nrepl]
            [lein-ui.core :as ui]
            [lein-ui.nrepl :as ui-nrepl]
            [lein-ui.nrepl.client-manager :as client-manager]
            [lein-ui.util :refer [pprint-str]]))


;; TODO figure out users
;; TODO register watcher for messages

(defmulti handle-message (fn [event & args] event))

(defmethod handle-message [:ws :connect] [_ session channel])

(defmethod handle-message [:ws :receive] [_ session msg]
  ;; (validate-nrepl-msg! msg :or send-error :to session)
  ;; (some-> (validate-nrepl-msg! msg) (send-error session))
  (let [session (client-manager/load-repl-session session (:subject msg))]
    (client-manager/send-message! session (:subject msg) (:body msg))))

(defmethod handle-message [:ws :close] [_ session status]
  (println "Session" session "disconnected with" status))

(defn nrepl-socket-handler [request]
  (let [user :current-user]
    (server/with-channel request channel
      (server/on-receive channel (fn [msg]
                                   (try
                                     (handle-message msg user (request :lein-ui/app-state))
                                     (catch Throwable t
                                       (println t)
                                       (server/send! channel (pprint-str
                                                              {:type :error
                                                               :exception (str t)}))))))
      (server/on-close channel (fn [status]
                                 ;; (schedule (-> 10 minutes) #(cleanup user)
                                 (println "User" user "disconnected"))))))<commit_msg>Comment out stuff that doesn't actually work<commit_after>(ns lein-ui.nrepl.http
  (:require [org.httpkit.server :as server]
            [clojure.tools.nrepl :as nrepl]
            [lein-ui.core :as ui]
            [lein-ui.nrepl :as ui-nrepl]
            [lein-ui.nrepl.client-manager :as client-manager]
            [lein-ui.util :refer [pprint-str]]))


;; TODO figure out users
;; TODO register watcher for messages

(comment
  (defmulti handle-message (fn [event & args] event))

         (defmethod handle-message [:ws :connect] [_ session]
           (let [user (session :user)
                 channel (session :ws/channel)]
             (ui-nrepl/subscribe! user
                                  (fn [msg]
                                    (server/send! channel {:type :nrepl/message
                                                           :body msg})))))

         (defmethod handle-message [:ws :receive] [_ session msg]
           ;; (validate-nrepl-msg! msg :or send-error :to session)
           ;; (some-> (validate-nrepl-msg! msg) (send-error session))
           (let [session (client-manager/load-repl-session session (:subject msg))]
             (client-manager/send-message! session (:subject msg) (:body msg))))

         (defmethod handle-message [:ws :close] [_ session status]
           (println "Session" session "disconnected with" status))

         (defn nrepl-socket-handler [session]
           (let [user :current-user]
             (server/with-channel request channel
               (handle-message [:ws :connect] session)
               (server/on-receive channel (fn [msg]
                                            (try
                                              (handle-message msg user (request :lein-ui/app-state))
                                              (catch Throwable t
                                                (println t)
                                                (server/send! channel (pprint-str
                                                                       {:type :error
                                                                        :exception (str t)}))))))
               (server/on-close channel (fn [status]
                                          ;; (schedule (-> 10 minutes) #(cleanup user)
                                          (println "User" user "disconnected")))))))
{'fraction_non_alphanumeric': 0.11520848397204146, 'fraction_numerical': 0.0009640877319836105, 'mean_word_length': 1.3646723646723646, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 35, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5256323', 'n_tokens_mistral': 1075, 'n_tokens_neox': 1021, 'n_words': 282}