Episode 010: From Mud to Bricks

Play Episode

Christoph can't stand the spaghetti mess in main. Time to refactor.

Clojure in this episode:

Related projects:

Code sample from this episode:

(ns app.main
  (:require
    [clojure.core.async :as async]
    [com.stuartsierra.component :as component]
    [app.component
     [fetcher :as fetcher]
     [web :as web]])
  (:gen-class))

(defn new-system
  []
  (component/system-map
    :new-search-chan (async/chan)
    :web (component/using (web/new-component) [:new-search-chan])
    :fetcher (component/using (fetcher/new-component) [:new-search-chan])

(defn -main
  [& args]
  (let [system (component/start (new-system))
        lock (promise)
        stop (fn []
               (component/stop system)
               (deliver lock :release))]
    (.addShutdownHook (Runtime/getRuntime) (Thread. stop))
    @lock
    (System/exit 0)))