1

I have code that looks something like the below:

(defn on-message [event]
  (do-stuff))

(defn build-websocket []
  (let [ws (js.window.WebSocket. "ws://localhost:8888/ws/")]
    (set! (.-onopen ws) on-open)
    (set! (.-onclose ws) on-close)
    (set! (.-onerror ws) on-error)
    (set! (.-onmessage ws) on-message)
    ws))

I'm using figwheel. I have noticed that when I change my callbacks (on-message, on-error, etc), the app does reload, but old versions of these functions still get called.

(defn on-message [event]
  ; Changed. Requires hard reload to be called on websocket message.
  (do-other-stuff))

I understand that this is because the old version of the function is still referenced by the websocket.

Is there a pattern that would make this code reloadable? Perhaps I should be using core/async? If so, what would that look like?

1 Answer 1

3

Perhaps your code is not reloadable. Another thing to look at, for quick fix, is to always reload the namespace when you save from your editor/IDE:

(ns ^:figwheel-always my.namespace
  (:require [clojure.string :as str])

Notice the ^:figwheel-always metadata.

For more on writing reloadable code see here.

1
  • I think you're right, non-"reloadable" code is exactly the issue. Thank you for the link and terminology, I've updated the question, and will research reloadable code.
    – meshy
    Apr 10, 2016 at 20:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.