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
(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
// 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
(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
(ns cchange | |
(:require [mount.core :as mount])) | |
(defprotocol ChangeListener | |
(add-watcher [this ks watcher]) | |
(on-change [this k])) | |
(deftype RestartListener [watchers] | |
ChangeListener |
While I have not used spec for work before, I followed the hype for several months, and really like the idea.
Overall this workshop, while was the first one of its kind, really "moved" me to actually apply spec to my current Clojure work. In both areas: open source and products for customers.
I believe a "needs improvement" feedback is the most useful one, so don't take it as something I did not like, but rather something others can benefit from in the future attending this workshop.
All that follows is definitely connected to the way I learn things, perceive and digest the information.
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 pcall [f & args] | |
(try [true (apply f args)] | |
(catch Exception e | |
[false e]))) | |
(defn matched? [[status value]] | |
(if status | |
(when (and value | |
(not= value :nginx-null)) | |
value) |
this is the snippet that manages the http server lifecycle:
(defstate web-server :start (start-www config)
:stop (.stop web-server))