-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to update a state with a runtime function invocation result #64
Comments
who and when calls
following the same mount example you linked to, there is also a config state that http server uses. At runtime config state is created before the webserver, hence it becomes a "runtime argument" for it. let's looks at this example: dev=> (def conf {:routes "my routes" :port 4242})
#'dev/conf
dev=>
dev=> (defn launch-server [{:keys [routes port]}] (println "running: " routes {:port port}) {:routes routes :port port})
#'dev/launch-server
dev=>
dev=> (defn stop-http [server] (println "stopping:" server))
#'dev/stop-http
dev=>
dev=> (defstate http-server :start (launch-server conf) :stop (stop-http http-server))
"|| mounting... #'dev/http-server"
#'dev/http-server
dev=>
dev=> (mount/start #'dev/http-server)
running: my routes {:port 4242}
{:started ["#'dev/http-server"]}
dev=> (mount/stop #'dev/http-server)
stopping: {:routes my routes, :port 4242}
{:stopped ["#'dev/http-server"]}
the difference between the example above and your example is But of course I'd need more details to understand your use case better. There are a couple more project examples here you can look at. |
The routes are generated dynamically at runtime; the user passes in a set of data, and routes are generated from that data, and a http server is spun up dynamically to serve those routes. This could happen several times during program execution. Normally the port wouldn't change as each set of routes replaces its predecessor, but there will be some situations where I'll need a couple of sets of routes offered on different ports. I guess what I was thinking of was some sort of Thanks for the pointers about the config handling in neo & smsio! |
I see in Mount's examples that a state captures the output of the :start fn and that the :stop fn interacts with that result.
What if the arguments to the :start fn aren't known until runtime? For example, I'd like to pass in a runtime-generated set of routes to my web handler. So if I have the following function to launch a server:
How do I define a state to capture the output of that function at runtime, that I can then use to close the server in my :stop fn?
Currently I'm rigging my own solution with an atom but it feels like I'm side-stepping mount rather than using it correctly:
The text was updated successfully, but these errors were encountered: