bhauman / lein-figwheel

Figwheel builds your ClojureScript code and hot loads it into the browser as you are coding!
Eclipse Public License 1.0
2.89k stars 210 forks source link

How to work with multimethods? #117

Closed mikavilpas closed 9 years ago

mikavilpas commented 9 years ago

I'd like to try setting up sente event handlers with multimethods. Are they supported? I can see some changes if I (def my-multi-method nil) before re-declaring the multimethod, but it seems both the old multimethod and the new one being reloaded run.

Reloading my app's web page manually ends up only using the new implementation, as expected.

Here's a snippet with something I'm trying out:

(def chat-event-dispatcher nil)
(defmulti chat-event-dispatcher first)
(defmethod chat-event-dispatcher :default [event]
  (print "unknown server event" (:id event)))

(defmethod chat-event-dispatcher :server/send-new-messages
  [[_event-id {:keys [data]}] event]
  (print "old implementation")
  (swap! update-in app-state [:messages] #(concat % data)))

If I change "old implementation" to "new implementation" both multimethods are ran with the file being saved.

bhauman commented 9 years ago

Mark more than likely you are adding new handlers to the channel events on save.

Or you are passing the dispatch handler by value to sente. Or a number of things, but I can't tell anything from the code you have given me.

On Fri, Mar 27, 2015 at 9:18 AM, Mika Vilpas notifications@github.com wrote:

I'd like to try setting up sente event handlers with multimethods. Are they supported? I can see some changes if I (def my-multi-method nil) before re-declaring the multimethod, but it seems both the old multimethod and the new one being reloaded run.

Reloading my app's web page manually ends up only using the new implementation, as expected.

Here's a snippet with something I'm trying out:

(def chat-event-dispatcher nil) (defmulti chat-event-dispatcher first) (defmethod chat-event-dispatcher :default [event](print "unknown server event" %28:id event)))

(defmethod chat-event-dispatcher :server/send-new-messages [_event-id {:keys [data]}] event (swap! update-in app-state [:messages] #(concat % data)))

If I change "old implementation" to "new implementation" both multimethods are ran with the file being saved.

— Reply to this email directly or view it on GitHub https://github.com/bhauman/lein-figwheel/issues/117.

mikavilpas commented 9 years ago

Ah those are good suggestions. I'll review my code. I take it multimethods are supported then? 27.3.2015 16.15 kirjoitti "Bruce Hauman" notifications@github.com:

Mark more than likely you are adding new handlers to the channel events on save.

Or you are passing the dispatch handler by value to sente. Or a number of things, but I can't tell anything from the code you have given me.

On Fri, Mar 27, 2015 at 9:18 AM, Mika Vilpas notifications@github.com wrote:

I'd like to try setting up sente event handlers with multimethods. Are they supported? I can see some changes if I (def my-multi-method nil) before re-declaring the multimethod, but it seems both the old multimethod and the new one being reloaded run.

Reloading my app's web page manually ends up only using the new implementation, as expected.

Here's a snippet with something I'm trying out:

(def chat-event-dispatcher nil) (defmulti chat-event-dispatcher first) (defmethod chat-event-dispatcher :default [event](print "unknown server event" %28:id event)))

(defmethod chat-event-dispatcher :server/send-new-messages [_event-id {:keys [data]}] event (swap! update-in app-state [:messages] #(concat % data)))

If I change "old implementation" to "new implementation" both multimethods are ran with the file being saved.

— Reply to this email directly or view it on GitHub https://github.com/bhauman/lein-figwheel/issues/117.

— Reply to this email directly or view it on GitHub https://github.com/bhauman/lein-figwheel/issues/117#issuecomment-86952976 .

cichli commented 9 years ago

defmulti has the same semantics as defonce - see CLJ-900. I think you can just extract the implementation out into a separate function to make it reloadable.

bhauman commented 9 years ago

figwheel does nothing special for multimethods. You have to be aware of their loading semantics. They actually don't have defonce behavior right now. http://dev.clojure.org/jira/browse/CLJS-1175

mikavilpas commented 9 years ago

Yep, looks like you guys were right. My problem was I had a sente-setup function to which I passed my chat-event-dispatcher. I removed the argument and hard coded the dependency to this specific function. Now it looks like I can just change the multimethod in place and figwheel reloads the new code immediately, like I expected it to. Awesome! :)