Fork me on GitHub
#mount
<
2018-02-07
>
tolitius03:02:59

@mjmeintjes it'd be helpful to know the actual use case: * who calls a .close function: is done via mount of called somewhere else? * what is it that is needed to be done in a done function? * if .close is called as a part of "mount/stop" why does mount need to wait for a callback to be called?

mjmeintjes03:02:19

Just a disclaimer - there is a distinct possibility that I'm doing this wrong, as I'm pretty new to node. I have 2 functions, start and stop, that gets called when app is started and stopped/reloaded. In start I call mount/start, in stop I call mount/stop. The stop function is async, and takes a done callback that it just calls to indicate that processing is done.

(defn start
  "Hook to start. Also used as a hook for hot code reload."
  []
  (js/console.warn "start called")
  (mount/start))

(defn stop
  "Hot code reload hook to shut down resources so hot code reload can work"
  [done]
  (js/console.warn "stop called")
  (mount/stop)
  ;; not really done, as server might still be closing
  (done))
I want to be able to pass that done function to the stop function, in order to signal that shutdown is finished:
(defstate server
  :start   (let [host (or (:host @env) "127.0.0.1")
                 port (or (some-> @env :port js/parseInt) 3000)]
             (http/start
              {:handler    (wrap-defaults router)
               :host       host
               :port       port
               :on-success #(info "funnels-cljs started on" host ":" port)}))
  :stop (do
          (println "CLOSING SERVER")
          (.close @server
                  (fn []
                    ;; want to call the done callback here
                    ))))

tolitius05:02:27

@mjmeintjes > in order to signal that shutdown is finished If I understood correctly you need to log a message that confirms that the server is shutdown. Instead of calling (done) in a defined "stop" function, could you just pass it as it is intended to .close:

(.close @server
                  (fn []
                    (done)
                    ))
?

mjmeintjes05:02:57

No, not as far as I know, as I only get a reference to the done callback in that stop function, which is called by shadow-cljs when code is reloaded. The stop function is just there to tell shadow-cljs that the app has stopped and that it can now reload safely.

tolitius21:02:33

@mjmeintjes I have not used shadow-cljs, it looks like it also in a way manages state hence the hiccup. If a done function is something known in advance:

(.close @server
                  (fn []
                    (done)
                    ))
will be an option in case done is an internal fn passed by shadow-cljs, you could probably place :done on the async channel in the callback, and then read it to report that is was called