#68: swap-states and start-with-states take values
in a form of {:start fn :stop fn}
This commit is contained in:
parent
087683968d
commit
e2ef7bba55
4 changed files with 53 additions and 30 deletions
20
README.md
20
README.md
|
|
@ -274,7 +274,7 @@ Each "tool" has a single responsibility and can be composed with other tools in
|
|||
* `only` will return _only_ states that it is given + exist (seen by mount) in the application
|
||||
* `except` will return all the states that it is given _except_ a given set
|
||||
* `swap` will take a map with keys as states and values as their substitute values
|
||||
* `swap-states` will take a map with keys as states and values as their substitute states
|
||||
* `swap-states` will take a map with keys as states and values with `{:start fn :stop fn}` as their substitute states
|
||||
* `with-args` will take a map that could later be accessed by `(mount/args)`
|
||||
|
||||
All these functions take one or two arguments. If called with two arguments, the first one will be treated as the universe of states to work with. If called with one argument, it will work with _all known_ to mount states.
|
||||
|
|
@ -312,7 +312,8 @@ Here is a more "involved" example:
|
|||
(with-args {:a 42})
|
||||
(except [#'foo/c
|
||||
#'bar/d])
|
||||
(swap-states {#'foo/a #'test/a})
|
||||
(swap-states {#'foo/a {:start #(create-connection test-conf)
|
||||
:stop #(disconnect a)}})
|
||||
(swap {#'baz/e {:datomic {:uri "datomic:mem://composable-mount"}}})
|
||||
mount/start)
|
||||
```
|
||||
|
|
@ -381,14 +382,21 @@ When running tests it would be great _not_ to send the real text messages, but r
|
|||
|
||||
### Swapping States with States
|
||||
|
||||
The `start-with-states` function takes other states as substitutes:
|
||||
The `start-with-states` function takes values in a form of `{:start fn :stop fn}` as substitutes:
|
||||
|
||||
```clojure
|
||||
(mount/start-with-states {#'app.neo/db #'app.test/test-db
|
||||
#'app.neo/publisher #'app.test/test-publisher})
|
||||
(mount/start-with-states {#'app.neo/db {:start #(connect test-config)
|
||||
:stop #(disconnect db)}
|
||||
#'app.neo/publisher {:start #(create-pub test-config)
|
||||
:stop #(close-pub publisher)}})
|
||||
```
|
||||
|
||||
`start-with-states` takes a map of states with their substitutes. For example `#'app.nyse/db` here is the real deal (remote) DB that is being substituted with `#'app.test/test-db` state, which could be anything, a map, an in memory DB, etc.
|
||||
`start-with-states` takes a map of states with their substitutes. For example `#'app.nyse/db` here is the real deal (remote) DB that is being
|
||||
substituted with `#(connect test-config)` function, which could endup being anything, a map, an in memory DB, etc.
|
||||
|
||||
The `:stop` functions of substitutes can be anything, and could refer to the original state references. As in the example above: `db` and `publisher`
|
||||
are real references. They would need to be accessible from the namespace of course, so you might need to `(:require [app.neo :refer [db]])`
|
||||
in order to use `db` in `:stop #(disconnect db)` example above.
|
||||
|
||||
--
|
||||
|
||||
|
|
|
|||
|
|
@ -231,9 +231,7 @@
|
|||
origin (@meta-state state)
|
||||
sub (if (= :value mode)
|
||||
{:start (fn [] with) :status :stopped}
|
||||
(@meta-state with))]
|
||||
(when (= :state mode)
|
||||
(update-meta! [with :sub?] true))
|
||||
(assoc with :status :stopped))]
|
||||
(update-meta! [state] (merge-lifecycles origin (lifecycle-fns origin) sub))))
|
||||
|
||||
(defn- unsub [state]
|
||||
|
|
@ -302,7 +300,7 @@
|
|||
([states with]
|
||||
(doseq [[from to] with]
|
||||
(substitute! (var-to-str from)
|
||||
(var-to-str to) :state))
|
||||
to :state))
|
||||
states))
|
||||
|
||||
;; restart on events
|
||||
|
|
@ -354,7 +352,7 @@
|
|||
(defn start-with-states [with]
|
||||
(doseq [[from to] with]
|
||||
(substitute! (var-to-str from)
|
||||
(var-to-str to) :state))
|
||||
to :state))
|
||||
(start))
|
||||
|
||||
(defn start-without [& states]
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@
|
|||
[tapp.audit-log :refer [log]]]
|
||||
:clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]]
|
||||
[clojure.set :refer [intersection]]
|
||||
[clojure.tools.nrepl.server :refer [start-server stop-server]]
|
||||
[mount.core :as mount :refer [defstate only except swap swap-states with-args]]
|
||||
[tapp.conf :refer [config]]
|
||||
[tapp.nyse :refer [conn]]
|
||||
|
|
@ -21,6 +22,12 @@
|
|||
|
||||
(defstate test-nrepl :start [])
|
||||
|
||||
(def swap-conn {:start (fn [] 42)
|
||||
:stop #(println "stopping test-conn-state")})
|
||||
#?(:clj
|
||||
(def swap-nrepl {:start #(start-server :bind "localhost" :port 3442)
|
||||
:stop #(stop-server @nrepl)}))
|
||||
|
||||
#?(:clj
|
||||
(deftest only-states
|
||||
|
||||
|
|
@ -93,19 +100,19 @@
|
|||
(deftest swap-states-with-states
|
||||
|
||||
(testing "swap-states should swap states with states and return all mount states if none is given"
|
||||
(let [states (swap-states {#'tapp.nyse/conn #'mount.test.composable-fns/test-conn
|
||||
#'tapp.example/nrepl #'mount.test.composable-fns/test-nrepl})]
|
||||
(let [states (swap-states {#'tapp.nyse/conn swap-conn
|
||||
#'tapp.example/nrepl swap-nrepl})]
|
||||
(is (= states (#'mount.core/find-all-states)))
|
||||
(mount/start)
|
||||
(is (map? (dval config)))
|
||||
(is (vector? (dval nrepl)))
|
||||
(is (instance? clojure.tools.nrepl.server.Server (dval nrepl)))
|
||||
(is (= 42 (dval conn)))
|
||||
(mount/stop)))
|
||||
|
||||
(testing "swap-states should swap states with states and return only states that it is given"
|
||||
(let [t-states #{"#'is.not/here" #'mount.test.composable-fns/test-conn #'tapp.nyse/conn}
|
||||
states (swap-states t-states {#'tapp.nyse/conn #'mount.test.composable-fns/test-conn
|
||||
#'tapp.example/nrepl #'mount.test.composable-fns/test-nrepl})]
|
||||
states (swap-states t-states {#'tapp.nyse/conn swap-conn
|
||||
#'tapp.example/nrepl swap-nrepl})]
|
||||
(is (= states t-states))
|
||||
(apply mount/start states)
|
||||
(is (instance? mount.core.NotStartedState (dval config)))
|
||||
|
|
@ -127,14 +134,14 @@
|
|||
(with-args {:a 42})
|
||||
(except [#'mount.test.composable-fns/test-nrepl
|
||||
#'mount.test.composable-fns/test-conn])
|
||||
(swap-states {#'tapp.example/nrepl #'mount.test.composable-fns/test-nrepl})
|
||||
(swap-states {#'tapp.example/nrepl swap-nrepl})
|
||||
(swap {#'tapp.conf/config {:datomic {:uri "datomic:mem://composable-mount"}}}))]
|
||||
(is (= #{"#'tapp.nyse/conn" "#'tapp.conf/config" "#'tapp.example/nrepl"} (set states)))
|
||||
(mount/start states)
|
||||
(is (= {:a 42} (mount/args)))
|
||||
(is (= {:datomic {:uri "datomic:mem://composable-mount"}} (dval config)))
|
||||
(is (instance? datomic.peer.LocalConnection (dval conn)))
|
||||
(is (vector? (dval nrepl)))
|
||||
(is (instance? clojure.tools.nrepl.server.Server (dval nrepl)))
|
||||
(mount/stop)))
|
||||
|
||||
(testing "should compose and start in a single composition"
|
||||
|
|
@ -147,13 +154,13 @@
|
|||
(with-args {:a 42})
|
||||
(except [#'mount.test.composable-fns/test-nrepl
|
||||
#'mount.test.composable-fns/test-conn])
|
||||
(swap-states {#'tapp.example/nrepl #'mount.test.composable-fns/test-nrepl})
|
||||
(swap-states {#'tapp.example/nrepl swap-nrepl})
|
||||
(swap {#'tapp.conf/config {:datomic {:uri "datomic:mem://composable-mount"}}})
|
||||
mount/start)
|
||||
(is (= {:a 42} (mount/args)))
|
||||
(is (= {:datomic {:uri "datomic:mem://composable-mount"}} (dval config)))
|
||||
(is (instance? datomic.peer.LocalConnection (dval conn)))
|
||||
(is (vector? (dval nrepl)))
|
||||
(is (instance? clojure.tools.nrepl.server.Server (dval nrepl)))
|
||||
(mount/stop)))
|
||||
|
||||
(testing "should not start anything on empty seq of states"
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
[tapp.audit-log :refer [log]]]
|
||||
:clj [[clojure.test :as t :refer [is are deftest testing use-fixtures]]
|
||||
[mount.core :as mount :refer [defstate]]
|
||||
[clojure.tools.nrepl.server :refer [start-server stop-server]]
|
||||
[tapp.conf :refer [config]]
|
||||
[tapp.nyse :refer [conn]]
|
||||
[tapp.example :refer [nrepl]]])
|
||||
|
|
@ -19,20 +20,29 @@
|
|||
|
||||
(defstate test-nrepl :start [])
|
||||
|
||||
(def swap-conn {:start (fn [] 42)
|
||||
:stop #(println "stopping test-conn-state")})
|
||||
#?(:clj
|
||||
(def swap-nrepl {:start #(start-server :bind "localhost" :port 3442)
|
||||
:stop #(stop-server @nrepl)})
|
||||
:cljs
|
||||
(def swap-nrepl {:start (fn [] :nrepl)
|
||||
:stop (fn [] :stopped-nrepl)}))
|
||||
|
||||
#?(:cljs
|
||||
(deftest start-with-states
|
||||
|
||||
(testing "should start with substitutes"
|
||||
(let [_ (mount/start-with-states {#'tapp.websockets/system-a #'mount.test.start-with-states/test-conn
|
||||
#'mount.test.helper/helper #'mount.test.start-with-states/test-nrepl})]
|
||||
(let [_ (mount/start-with-states {#'tapp.websockets/system-a swap-conn
|
||||
#'mount.test.helper/helper swap-nrepl})]
|
||||
(is (map? (dval config)))
|
||||
(is (vector? (dval helper)))
|
||||
(is (= (:nrepl (dval helper))))
|
||||
(is (= (dval system-a) 42))
|
||||
(is (instance? datascript.db/DB @(dval log)))
|
||||
(mount/stop)))
|
||||
|
||||
(testing "should not start the substitute itself"
|
||||
(let [_ (mount/start-with-states {#'tapp.websockets/system-a #'mount.test.start-with-states/test-conn})]
|
||||
#_(testing "should not start the substitute itself" ;; was true when subbing with exsiting states
|
||||
(let [_ (mount/start-with-states {#'tapp.websockets/system-a swap-conn})]
|
||||
(is (instance? mount.core.NotStartedState (dval test-conn)))
|
||||
(is (= 42 (dval system-a)))
|
||||
(mount/stop)))
|
||||
|
|
@ -62,15 +72,15 @@
|
|||
(deftest start-with-states
|
||||
|
||||
(testing "should start with substitutes"
|
||||
(let [_ (mount/start-with-states {#'tapp.nyse/conn #'mount.test.start-with-states/test-conn
|
||||
#'tapp.example/nrepl #'mount.test.start-with-states/test-nrepl})]
|
||||
(let [_ (mount/start-with-states {#'tapp.nyse/conn swap-conn
|
||||
#'tapp.example/nrepl swap-nrepl})]
|
||||
(is (map? (dval config)))
|
||||
(is (vector? (dval nrepl)))
|
||||
(is (instance? clojure.tools.nrepl.server.Server (dval nrepl)))
|
||||
(is (= (dval conn) 42))
|
||||
(mount/stop)))
|
||||
|
||||
(testing "should not start the substitute itself"
|
||||
(let [_ (mount/start-with-states {#'tapp.nyse/conn #'mount.test.start-with-states/test-conn})]
|
||||
#_(testing "should not start the substitute itself" ;; was true when subbing with exsiting states
|
||||
(let [_ (mount/start-with-states {#'tapp.nyse/conn swap-conn})]
|
||||
(is (instance? mount.core.NotStartedState (dval test-conn)))
|
||||
(is (= (dval conn) 42))
|
||||
(mount/stop)))
|
||||
|
|
|
|||
Loading…
Reference in a new issue