Clojure is powerful, simple and fun. Depending on how the application state is managed, these 3 superpowers can either stay, go somewhat, or go completely. Apps we build for clients are quite different from tools and libraries on github; they are full of state. While there are frameworks that allow you to join the "application context party", this talk will take a very different approach to manage and reload Clojure and ClojureScript state with the help of a tiny library called "mount".
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (require '[clojure.string :as s] | |
| '[gregor.core :as gregor]) | |
| (defn to-prop [k] | |
| (-> k name (s/replace #"-" "."))) | |
| (defn to-props | |
| "ranames keys by converting them to strings and substituting dashes with periods | |
| only does top level keys" | |
| [conf] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Changing "DEFAULT_PREFIX": https://logging.apache.org/log4j/2.0/log4j-core/apidocs/src-html/org/apache/logging/log4j/core/config/ConfigurationFactory.html#line.108 | |
| Field prefix = ConfigurationFactory.class.getField( "DEFAULT_PREFIX" ) | |
| prefix.setAccessible( true ); | |
| Field mods = Field.class.getDeclaredField( "modifiers" ); | |
| mods.setAccessible( true ); | |
| mods.setInt( prefix, prefix.getModifiers() & ~Modifier.FINAL ); | |
| field.set( null, "logger-config" ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn throw-err [e] | |
| (when (instance? Throwable e) | |
| (throw e)) | |
| e) | |
| (defn listen [f ch] | |
| (let [running? (atom true) | |
| stop-ch (chan)] | |
| (go-loop [] | |
| (when @running? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;; [tolitius:/tmp/ido]$ cat src/ido/core.clj | |
| (ns ido.core | |
| (:require [mount.core :refer [defstate]])) | |
| (defstate kafka :start 42 | |
| :stop 0) | |
| ;; [tolitius:/tmp/ido]$ | |
| ;; [tolitius:/tmp/ido]$ lein install | |
| ;; Created /private/tmp/ido/target/ido-0.1.0-SNAPSHOT.jar |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn deref-state [name] | |
| (let [{:keys [status inst] :as state} (@meta-state name)] | |
| (when-not (:started status) | |
| (up name state (atom #{}))) | |
| @inst)) | |
| (defn throw-not-started [name] | |
| (throw-runtime (str "can't use \"" name "\" state, since it was not started. " | |
| "this would usually happen if you forget to start it explicitly " | |
| "or to \":require\" this state or its namespace, " |
Alan Dipert (@alandipert) and Micha Niskin (@micha)
for Boot, slides and help with Boot intel.
Juno Terepi (@deraen)
for slides and help with Lein pitfals
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (deftask build [] | |
| (comp | |
| (pom) ;; <<< names the file correctly + no need for --file in "(ubermain)" or "(push)" | |
| (ubermain) | |
| (sift :include #{#"stream-on.*.jar$"}) | |
| (target))) | |
| (deftask deploy-snapshot [] | |
| (comp | |
| (build) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defn connect [db-spec] | |
| (jdbc/get-connection db-spec)) | |
| (defn disconnect [conn] | |
| (.close conn)) |