     (let [stdout-sniffer (sniffer/make-sniffer *out* (partial flush-handler driver :stdout))
           stderr-sniffer (sniffer/make-sniffer *out* (partial flush-handler driver :stderr))]                                 ; *out* is here on purpose, see :bind-err and its effect when true (default)
       (try
-        (vreset! (get-in driver [:sniffers :stdout]) stdout-sniffer)
-        (vreset! (get-in driver [:sniffers :stderr]) stderr-sniffer)
+        (set-sniffer! driver :stdout stdout-sniffer)
+        (set-sniffer! driver :stderr stderr-sniffer)
         (registry/register-driver! :current-repl driver)
         (binding [*out* stdout-sniffer
                   *err* stderr-sniffer]

  (announce-job-end driver (get-current-job driver))
  (vreset! (:current-job driver) nil))

; -- print recording --------------------------------------------------------------------------------------------------------

(defn recording? [driver]
  @(:recording? driver))

(defn start-recording! [driver]
  (sniffer/clear-content! @(get-in driver [:sniffers :stdout]))
  (sniffer/clear-content! @(get-in driver [:sniffers :stderr]))
  (vreset! (:recording? driver) true))

(defn flush-remainder! [driver sniffer-key]
  (let [sniffer (get-in driver [:sniffers sniffer-key])
        content (sniffer/extract-content! @sniffer)]
    (if-not (nil? content)
      (let [{:keys [request-id]} (get-current-job driver)]
        (messaging/report-output (:server driver) request-id sniffer-key content)))))

(defn stop-recording! [driver]
  (when (recording? driver)
    (flush-remainder! driver :stdout)
    (flush-remainder! driver :stderr)
    (vreset! (:recording? driver) false)))

; -- detection of active REPL options ---------------------------------------------------------------------------------------

(defn resolve-repl-opts []
  (if-let [opts (resolve 'cljs.repl/*repl-opts*)]
    @opts))

(defn resolve-active-repl-opts-if-needed! [driver]
  (if-not @(:active-repl-opts driver)


; -- driver construction ----------------------------------------------------------------------------------------------------

(defn get-initial-settings []
  {:commands-channel                     (chan)                                                                               ; channel for REPL commands (actually command records) from *in* and also network
   :current-job                          (volatile! nil)                                                                      ; command-record currently being processed {:request-id :kind :command}
   :active-repl-opts                     (volatile! nil)                                                                      ; cached repl opts
   :recording?                           (volatile! false)                                                                    ; should we record printing into *out* and *err*?
   :suppress-flushing                    (volatile! #{})                                                                      ; temporary suppression of flushing, contains a set of sniffer-keys
   :suppress-print-recording-until-flush (volatile! #{})})                                                                    ; temporary suppression of recording, contains a set of sniffer-keys)

(defn make-driver [extra-settings]
  (merge (get-initial-settings) extra-settings))

(defn set-sniffer! [driver sniffer-key sniffer]
  (vreset! (get-in driver [:sniffers sniffer-key]) sniffer))

(defn get-sniffer [driver sniffer-key]
  (let [sniffer& (get-in driver [:sniffers sniffer-key])]
    (assert sniffer&)
    @sniffer&))

; -- announcements ----------------------------------------------------------------------------------------------------------

