diff --git a/README.md b/README.md index 425f3e4..be8e581 100644 --- a/README.md +++ b/README.md @@ -79,16 +79,16 @@ mount is an alternative to the [component](https://github.com/stuartsierra/compo Creating state is easy: ```clojure -(defstate conn :start (create-conn)) +(defstate conn :start create-conn) ``` -where `(create-conn)` is defined elsewhere, can be right above it. +where the `create-conn` function is defined elsewhere, can be right above it. In case this state needs to be cleaned / destryed between reloads, there is also `:stop` ```clojure -(defstate conn :start (create-conn) - :stop (disconnect conn)) +(defstate conn :start create-conn + :stop #(disconnect conn)) ``` That is pretty much it. But wait, there is more.. this state is _a top level being_, which means it can be simply @@ -101,6 +101,14 @@ dev=> conn #object[datomic.peer.LocalConnection 0x1661a4eb "datomic.peer.LocalConnection@1661a4eb"] ``` +#### Value of values + +Lifecycle functions start/stop/suspend/resume can take both functions and values. This is valuable and also works: + +```clojure +(mount/defstate answer-to-the-ultimate-question-of-life-the-universe-and-everything :start 42) +``` + ### Using State For example let's say an `app` needs a connection above. No problem: @@ -136,7 +144,7 @@ There are of course direct dependecies that `mount` respects: (:require [mount.core :refer [defstate]])) (defstate app-config - :start (load-config "test/resources/config.edn")) + :start #(load-config "test/resources/config.edn")) ``` this `app-config`, being top level, can be used in other namespaces, including the ones that create states: @@ -146,7 +154,7 @@ this `app-config`, being top level, can be used in other namespaces, including t (:require [mount.core :refer [defstate]] [app.config :refer [app-config]])) -(defstate conn :start (create-connection app-config)) +(defstate conn :start #(create-connection app-config)) ``` [here](https://github.com/tolitius/mount/blob/master/test/app/nyse.clj) @@ -310,9 +318,9 @@ and some other use cases. In additiong to `start` / `stop` functions, a state can also have `resume` and, if needed, `suspend` ones: ```clojure -(defstate web-server :start (start-server ...) - :resume (resume-server ...) - :stop (stop-server ...)) +(defstate web-server :start #(start-server ...) + :resume #(resume-server ...) + :stop #(stop-server ...)) ``` diff --git a/doc/differences-from-component.md b/doc/differences-from-component.md index 93f678e..28b0ba2 100644 --- a/doc/differences-from-component.md +++ b/doc/differences-from-component.md @@ -137,8 +137,8 @@ Depending on the number of application components the "extra" size may vary. Mount is pretty much: ```clojure -(defstate name :start (fn) - :stop (fn)) +(defstate name :start fn + :stop fn) ``` no "ceremony". diff --git a/doc/intro.md b/doc/intro.md deleted file mode 100644 index f1e987b..0000000 --- a/doc/intro.md +++ /dev/null @@ -1,3 +0,0 @@ -# Introduction to statuo - -TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) diff --git a/doc/runtime-arguments.md b/doc/runtime-arguments.md index d226cbb..b4d206b 100644 --- a/doc/runtime-arguments.md +++ b/doc/runtime-arguments.md @@ -63,8 +63,8 @@ For the example sake the app reads arguments in two places: * [inside](https://github.com/tolitius/mount/blob/with-args/test/app/nyse.clj#L17) a `defstate` ```clojure -(defstate conn :start (new-connection (mount/args)) - :stop (disconnect (mount/args) conn)) +(defstate conn :start #(new-connection (mount/args)) + :stop #(disconnect (mount/args) conn)) ``` * and from "any" [other place](https://github.com/tolitius/mount/blob/with-args/test/app/config.clj#L8) within a function: @@ -84,13 +84,13 @@ In order to demo all of the above, we'll build an uberjar: ```bash $ lein do clean, uberjar ... -Created .. mount/target/mount-0.2.0-SNAPSHOT-standalone.jar +Created .. mount/target/mount-0.1.5-SNAPSHOT-standalone.jar ``` Since we have a default for a Datomic URI, it'll work with no arguments: ```bash -$ java -jar target/mount-0.2.0-SNAPSHOT-standalone.jar +$ java -jar target/mount-0.1.5-SNAPSHOT-standalone.jar 22:12:03.290 [main] INFO mount - >> starting.. app-config 22:12:03.293 [main] INFO mount - >> starting.. conn @@ -101,7 +101,7 @@ $ java -jar target/mount-0.2.0-SNAPSHOT-standalone.jar Now let's ask it to help us: ```bash -$ java -jar target/mount-0.2.0-SNAPSHOT-standalone.jar --help +$ java -jar target/mount-0.1.5-SNAPSHOT-standalone.jar --help 22:13:48.798 [main] INFO mount - >> starting.. app-config 22:13:48.799 [main] INFO app.config - @@ -116,7 +116,7 @@ this is a sample mount app to demo how to pass and read runtime arguments And finally let's connect to the Single Malt Database. It's Friday.. ```bash -$ java -jar target/mount-0.2.0-SNAPSHOT-standalone.jar -d datomic:mem://single-malt-database +$ java -jar target/mount-0.1.5-SNAPSHOT-standalone.jar -d datomic:mem://single-malt-database 22:16:10.733 [main] INFO mount - >> starting.. app-config 22:16:10.737 [main] INFO mount - >> starting.. conn diff --git a/doc/uberjar.md b/doc/uberjar.md index d0d9444..40b2012 100644 --- a/doc/uberjar.md +++ b/doc/uberjar.md @@ -38,15 +38,14 @@ where `nyse-app` is _the_ app. It has the usual routes: and the reloadable state: ```clojure -(defn start-nyse [] - (create-nyse-schema) ;; creating schema (usually done long before the app is started..) +(defn start-nyse [{:keys [www]}] (-> (routes mount-example-routes) (handler/site) (run-jetty {:join? false - :port (get-in app-config [:www :port])}))) + :port (:port www)}))) -(defstate nyse-app :start (start-nyse) - :stop (.stop nyse-app)) ;; it's a "org.eclipse.jetty.server.Server" at this point +(defstate nyse-app :start #(start-nyse app-config) + :stop #(.stop nyse-app)) ;; it's a "org.eclipse.jetty.server.Server" at this point ``` In order not to block, and being reloadable, the Jetty server is started in the "`:join? false`" mode which starts the server, diff --git a/src/mount/core.clj b/src/mount/core.clj index 64e43e4..a26c0a0 100644 --- a/src/mount/core.clj +++ b/src/mount/core.clj @@ -35,16 +35,16 @@ (validate lifecycle) (let [s-meta (cond-> {:mount-state mount-state :order (make-state-seq state) - :start `(fn [] (~@start)) + :start `(fn [] ~start) :started? false} - stop (assoc :stop `(fn [] (~@stop))) - suspend (assoc :suspend `(fn [] (~@suspend))) - resume (assoc :resume `(fn [] (~@resume))))] + stop (assoc :stop `(fn [] ~stop)) + suspend (assoc :suspend `(fn [] ~suspend)) + resume (assoc :resume `(fn [] ~resume)))] `(defonce ~(with-meta state (merge (meta state) s-meta)) (NotStartedState. ~(str state)))))) (defn- record! [{:keys [ns name]} f done] - (let [state (f)] + (let [state (trampoline f)] (swap! done conj (ns-resolve ns name)) state)) diff --git a/test/app/app.clj b/test/app/app.clj index f529c95..037269f 100644 --- a/test/app/app.clj +++ b/test/app/app.clj @@ -11,8 +11,8 @@ (start-server :bind host :port port)) ;; nREPL is just another simple state -(defstate nrepl :start (start-nrepl (:nrepl app-config)) - :stop (stop-server nrepl)) +(defstate nrepl :start #(start-nrepl (:nrepl app-config)) + :stop #(stop-server nrepl)) ;; datomic schema (defn create-schema [conn] diff --git a/test/app/config.clj b/test/app/config.clj index 1e18a59..74303a5 100644 --- a/test/app/config.clj +++ b/test/app/config.clj @@ -10,4 +10,4 @@ edn/read-string)) (defstate app-config - :start (load-config "test/resources/config.edn")) + :start #(load-config "test/resources/config.edn")) diff --git a/test/app/nyse.clj b/test/app/nyse.clj index 649dfad..7de455f 100644 --- a/test/app/nyse.clj +++ b/test/app/nyse.clj @@ -17,5 +17,5 @@ (.release conn) ;; usually it's not released, here just to illustrate the access to connection on (stop) (d/delete-database uri))) -(defstate conn :start (new-connection app-config) - :stop (disconnect app-config conn)) +(defstate conn :start #(new-connection app-config) + :stop #(disconnect app-config conn)) diff --git a/test/check/parts_test.clj b/test/check/parts_test.clj index a8c506f..53f4b38 100644 --- a/test/check/parts_test.clj +++ b/test/check/parts_test.clj @@ -3,7 +3,7 @@ [app.nyse :refer [conn]] [clojure.test :refer :all])) -(defstate should-not-start :start (constantly 42)) +(defstate should-not-start :start #(constantly 42)) (defn with-parts [f] (m/start #'app.config/app-config #'app.nyse/conn) diff --git a/test/check/start_with_test.clj b/test/check/start_with_test.clj index b5fe507..5256f70 100644 --- a/test/check/start_with_test.clj +++ b/test/check/start_with_test.clj @@ -5,10 +5,10 @@ [app :refer [nrepl]] [clojure.test :refer :all])) -(defstate test-conn :start (long 42) - :stop (constantly 0)) +(defstate test-conn :start 42 + :stop #(constantly 0)) -(defstate test-nrepl :start (vector)) +(defstate test-nrepl :start vector) (deftest start-with diff --git a/test/check/suspend_resume_test.clj b/test/check/suspend_resume_test.clj index a4eb724..218d288 100644 --- a/test/check/suspend_resume_test.clj +++ b/test/check/suspend_resume_test.clj @@ -15,15 +15,15 @@ (defn suspend [s] (koncat s :suspended)) (defn resume [s] (koncat s :resumed)) -(defstate web-server :start (start :w) - :stop (stop :w) - :suspend (suspend :w) - :resume (resume :w)) +(defstate web-server :start #(start :w) + :stop #(stop :w) + :suspend #(suspend :w) + :resume #(resume :w)) -(defstate q-listener :start (start :q) - :stop (stop :q) - :suspend (suspend :q) - :resume (resume :q)) +(defstate q-listener :start #(start :q) + :stop #(stop :q) + :suspend #(suspend :q) + :resume #(resume :q)) (deftest suspendable