   (println "Song has finished!")
   (.emit socket "track-complete"))
 
+(defn while-loading []
+  (letfn [(remove-once [pred coll]
+            ((fn inner [coll]
+              (lazy-seq
+                (when-let [[x & xs] (seq coll)]
+                  (if (pred x)
+                    xs
+                    (cons x (inner xs))))))
+              coll))]
+    (this-as sound
+      (if (some #(= (.-id sound) %) (:tracks-to-delete @app-state))
+        (do
+          (println "DESTROYING SOUND FROM whileloading()")
+          (.destruct sound)
+          (swap! app-state assoc :tracks-to-delete
+            (vec (remove #(= (.-id sound) %) (:tracks-to-delete @app-state)))))))))
+
 (defn play-track [track-id position]
   (let [reader (new js/FileReader)
         song-blob (get (:music-files @app-state) track-id)]

(.on socket "track-change"
  (fn [track-id]
    (println "CHANGING TO TRACK " track-id)
    (swap! app-state assoc :current-track-id track-id)

    (if-not (nil? (:current-sound @app-state))
      (.destruct (:current-sound @app-state)))

    (if (nil? (get (:music-files @app-state) track-id))
      (.emit socket "file-download-request" track-id)

                          :download-slots default-download-slots
                          :num-of-uploads 0
                          :num-of-downloads 0
                          :download-progress {}
                          :tracks-to-delete []}))

(enable-console-print!)

