This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-02-29
Channels
- # admin-announcements (6)
- # announcements (1)
- # beginners (1)
- # boot (104)
- # braid-chat (5)
- # cbus (1)
- # cider (2)
- # clojure (147)
- # clojure-japan (1)
- # clojure-poland (1)
- # clojure-russia (31)
- # clojurescript (16)
- # core-async (4)
- # css (2)
- # cursive (14)
- # datomic (40)
- # devcards (5)
- # dirac (100)
- # emacs (5)
- # funcool (1)
- # immutant (52)
- # juxt (4)
- # ldnclj (128)
- # lein-figwheel (12)
- # leiningen (26)
- # luminus (3)
- # mount (22)
- # off-topic (11)
- # om (144)
- # onyx (2)
- # parinfer (1)
- # proton (7)
- # re-frame (55)
- # reagent (16)
- # slack-help (5)
- # yada (1)
hey, mount newbie here. Do you use partially applied functions as states in mount? E.g. I have (defn find-user [db id] …)
function and turn it into state that depends on db
state like (defstate user-finder :start (partial find-user db))
.
I mean it still keeps the “running” version of db
state. Is there a way to combine those two: partial application to avoid dragging all the “deps” as well as having “live-link” to db
state? Gist here: https://gist.github.com/mostr/372f65ece0ad6ea218d1
hm, so I did the following:
(defn stop []
(prn "Stopping users db")
#{})
(defstate db
:start users
:stop (stop))
but still get
Loading src/clj_sandbox/ms/users_db.clj... done
(mount/stop)
=> {:stopped []}
(mount/start)
=>
{:started ["#'clj-sandbox.ms.users-db/db"
"#'clj-sandbox.ms.user-finder/user-finder"
"#'clj-sandbox.ms.user-finder/user-finder2"]}
(user-finder 2)
=> {:id 2, :name "bar"}
(mount/stop #'clj-sandbox.ms.users-db/db)
"Stopping users db"
=> {:stopped ["#'clj-sandbox.ms.users-db/db"]}
(user-finder 2)
=> {:id 2, :name "bar”}
not sure if this is real-life case (stop db while running), but trying to understand how things work in mount
Actually this works as expected as there is (partial…)
in user-finder
state definition which just probably takes “current” value/reference to db
and doesn’t care about state changes. But I wonder whether there is a way to combine these two things: partial application + being aware of state changes
looking at it with less distractions... yea, this is expected, here is just plain Clojure example:
boot.user=> (def db {:a 42})
#'boot.user/db
boot.user=> (def find-key (partial db))
#'boot.user/find-key
boot.user=> (find-key :a)
42
boot.user=> (def a {})
#'boot.user/a
boot.user=> (find-key :a)
42
in case you are ok with closing state over in function, you can simply (:require [users-db :refer [db]])
in user-finder
and use it within a function.
you are of course have freedom of where to use defstate
, but I found it is best used for resources, like db
in this case
and API, such as user-finder
could just use it as functions, rather than be defstates themselves
for example, say you have your db API that have access to db state live in app.db
namespace. then you can do:
(:require [app.db :as db])
(GET "/find-user" [user]
(db/find-user user))
or, if you'd like to use your pure find-user
function, you can do that too:
(:require [app.db :refer [db]])
(GET "/find-user" [user]
(find-user db user))
you could notice that in the second example, a route closes over the db
state. which is ok, since this is the edge of your application (i.e. no impact or coupling with business logic).