This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2017-07-11
Channels
- # aws (15)
- # beginners (55)
- # boot (116)
- # bristol-clojurians (2)
- # cider (4)
- # cljs-dev (439)
- # cljsrn (14)
- # clojure (135)
- # clojure-argentina (3)
- # clojure-czech (4)
- # clojure-italy (60)
- # clojure-russia (1)
- # clojure-spec (48)
- # clojure-uk (42)
- # clojurescript (170)
- # cloverage (11)
- # core-async (19)
- # cursive (13)
- # datomic (48)
- # emacs (2)
- # graphql (3)
- # hoplon (8)
- # jobs (1)
- # jobs-discuss (5)
- # klipse (11)
- # luminus (5)
- # lumo (5)
- # mount (48)
- # off-topic (96)
- # om (17)
- # onyx (14)
- # parinfer (30)
- # protorepl (1)
- # re-frame (90)
- # reagent (2)
- # remote-jobs (1)
- # spacemacs (12)
- # specter (20)
- # uncomplicate (1)
- # untangled (65)
- # vim (2)
- # yada (8)
Hi @onetom, I see your problem. I think it can be solved using:
(let [started (promise)]
(state :start @(deliver started (my-start-expr))
:stop (my-stop-expr @started)))
But that is not really nice. Maybe we could bind the result of the start expression to something? Any suggestions?intuitively i was expecting state
to handle the value of the stop parameter specially if it's a function
Hmm, that may be a bit too implicit, and could also clash with a "proper" %
in the stop expression..
Here the %
is used by the anonymous function, and should not be bound to the result of the :start
expression.
(if start
`(#'map->State (merge ~(dissoc fields :start :stop :name)
{:start-fn (fn [] ~start)
:stop-fn (fn [] ~stop)
:name ~name}))
(throw (ex-info "missing :start expression" {})))
so this is the state macro...
what if the :stop-fn
would receive the value of the current state by default?
it's a bit automagical, i agree, but is there a reason why stopping is not done like that?
as u can tell im kinda stumbling in the dark, because i've only used component & danielsz/system and only looked into mount and mount-lite seriously in the past ~3 days. im also confused by the slight differences between the two and i also went thru their evolution once again in just 3 days. i read all the latest commits between the lastest releases and the latest snapshot versions in both repos. so forgive me if im mixing things up a bit or don't make much sense 🙂
on top of that i've only used boot
in the past ~4years of my clojure career and im just learning lein
, which means there is suddenly no more boot.pods
; everything is under one classpath...
have you personally used the (with-session ...)
construct in your projects?
it would be good if you could show some examples how does it look like for u.
im also confused a bit by your recommendations in the Design consideration section of the docs.
> Having a global state does not mean you should forego on the good practice of passing state along as arguments to functions.
seems to contradict this statement:
> Try to use your defstate as if it were private. Better yet, declare it as private. This will keep you from refering to your state from every corner of your application, making it more componentized.
if my state is private, then it means i have some functions implicitly using it, no?
like the tx!
, db
& q
functions in my example:
https://gitlab.com/onetom/mount-lite-example/blob/master/src/app/db.clj#L13-15
I haven't used boot
a lot, so I can't really say something about that, as far as it is related to mount(-lite) 🙂
In the end the project that inspired us to create version 2 of mount-lite
did not require multiple sessions after all, so no, we have not used with-session
a lot.
What I mean in the documentation, is as follows:
(defn- do-stuff* [impl arg1 arg2]
...)
(defstate ^:private impl ...)
(defn do-stuff [arg1 arg2]
(do-stuff* @impl arg1 arg2))
This way, the actual implementation function receives the state directly, which is easier to test. But the public API of this namespace, does not see which states are used at all. It could be none, it could be many; the caller does not care and does not have to pass any states explicitly (i.e. it is private).okay, so according to ur example, i would say u did forego on the good practice of passing the state along as arguments to functions 🙂
Maybe I should update the documentation then, if it is not clear what I mean. I hope above example clears it up a bit.
so then your tests would look like:
(ns app.stuff-test
(:require [app.stuff :as stuff]))
(def do-stuff* #'stuff/do-stuff*)
(deftest stuff-test
(let [mock-state (...)]
(is (do-stuff* mock-state 1 2))))
yeah, this
sounds quite good, however, it would i need to use it as just this
or @this
?
so how would you explain it then?
> if you are using the anonymous state constructor macro called state
the stop expression will implicitly have access to the value of the anonymous state with the this
symbol
(defstate some-state
:start 123
:stop (println "Stopping" @some-state))
as an anonymous state, some-state
would be defined as:
(state some-state
:start 123
:stop (println "Stopping" this))
Source of the screenshot: https://youtu.be/13cmHf_kt-Q?t=985
If you are promoting opaqueness from the public API point of view, but suggest testing via a private API which receives the states it operates on explicitly, then it means the test code will be coupled to the implementation details
The this
would also be bound in the defstate
stop expression, but maybe it won't be used.
@onetom: Customers
should not be a coupling object inside a component at all. It is better to have two components: db
and email
. In which case, they can be stubbed for testing easily with mount/start-with
. Protocols and Records in this case are "very" optional, but since Component is all about it and your question is specifically about this piece of code:
(defprotocol EmailSender
(send [this address message]))
(defprotocol Database
(query [this q]))
(let [customers {:db (reify Database (query [_ q] (println "querying DB with" q)))
:email (reify EmailSender (send [_ a m] (println "sending email to" a "body:" m)))}]
(mount/start-with {#'app/customers customers})
if we agree not to have a coupling Customers
object, and just have db
and email
instead, we can simply do: