mount

madvas 2017-12-04T21:33:32.000419Z

Guys how so mount doesn’t support async start/stop. For example, if I need to load config asynchronously. How is that usually handled?

tolitius 2017-12-04T21:35:53.000093Z

@madvas can you describe what you are trying to do?

tolitius 2017-12-04T21:36:00.000285Z

(and why)

madvas 2017-12-04T21:40:35.000418Z

I need to load some configuration from locally running blockchain (understand server). Cannot be done in sync way. Other components might need this configuration data in their :start function

tolitius 2017-12-04T21:43:17.000463Z

so other components won't be able to start until the config is loaded?

madvas 2017-12-04T21:43:50.000037Z

no, they need loaded information

tolitius 2017-12-04T21:44:54.000178Z

right, so what makes it async?

madvas 2017-12-04T21:47:41.000097Z

not sure what you mean. that request to locally running server to fetch config. It’s separate app from the cljs app

tolitius 2017-12-04T21:56:32.000148Z

ok, so you have two apps?

tolitius 2017-12-04T21:57:10.000606Z

one in cljs and another one is clj (i.e. backend)

tolitius 2017-12-04T21:57:40.000305Z

and you need to load configuration from the server whenever someone visits a page / or the cljs app starts?

madvas 2017-12-04T21:59:23.000371Z

no, it’s different. Have just one, it’s cljs nodejs app, the other one is blockchain, it’s 3rd party software beyond my control

tolitius 2017-12-04T22:00:34.000439Z

ok, and you need to load config from a 3rd party app (i.e. a server) on cljs nodejs app's start?

madvas 2017-12-04T22:00:48.000280Z

yes

tolitius 2017-12-04T22:03:15.000806Z

mount will respect the order of states that need to be started, which means components which need this configuration will wait until a component that loads it from the server is started. i.e. if you have something like:

(defstate config :start (load-it-from-server "<http://localhost:8080/config>"))
and other components depend on this config they will wait

tolitius 2017-12-04T22:04:04.000382Z

from what you described so far I don't see what is "async" about it

madvas 2017-12-04T22:05:15.000330Z

but load-it-from-server returns what? you mean to do it like synchronous load?

madvas 2017-12-04T22:06:47.000463Z

the thing is that library to interact with blockchain server doesn’t support synchronous requests. Must pass callback

tolitius 2017-12-04T22:10:30.000125Z

right, so you can put it in a channel once the response is received. pseudo code:

(let [c (async/chan)]
  (http/get url #(&gt;!! c %))
  (&lt;!! c))

tolitius 2017-12-04T22:11:19.000570Z

I would wrap it in a load-it-from-server function, with a better name of course

madvas 2017-12-04T22:11:48.000206Z

there’s no &gt;!! and &lt;!! in cljs

madvas 2017-12-04T22:11:57.000095Z

only non-blocking

tolitius 2017-12-04T22:14:35.000341Z

ah.. cljs that's right, single event loop. then you would provide a callback that does (mount/start). once the config is loaded, mount will start all the components

madvas 2017-12-04T22:16:12.000319Z

I see, that’s what I started thinking, okay I’ll figure out something, thank you very much for your time 😉

tolitius 2017-12-04T22:16:42.000611Z

sure, sorry took some time to understand where you are coming from

tolitius 2017-12-04T22:17:51.000053Z

something that can be useful: (-&gt; (with-args {:config ....}) (mount/start))

tolitius 2017-12-04T22:18:16.000430Z

since once you receive the config you need to pass it to all other components

tolitius 2017-12-04T22:18:28.000075Z

i.e. https://github.com/tolitius/mount#composers-toolbox

madvas 2017-12-04T22:19:09.000373Z

yeah, I have that in mind, thanks but would be nice if I could just pass channel or something to defstate and it would wait with execution until results come and then pass it

tolitius 2017-12-04T22:25:10.000382Z

might be useful only for cljs side. and not in defstate, since the waiting should happen outside of mount: i.e. before calling (mount/start)

madvas 2017-12-04T22:33:43.000328Z

I think there’s more use cases where async start might be useful and cannot be done before mount/start. But yeah, clj’s advantage over cljs here is that it can make any asynchronous call synchronous