Fork me on GitHub
#hyperfiddle
<
2023-10-04
>
wei00:10:27

what's the correct way to write this?

(e/defn Input []
  (let [message "hello"
        submit! (e/fn []
                  (js/alert message) ;; use local scope
                  (e/server
                   ;; do something
                   ))]
    (dom/div
     (dom/textarea
      (dom/on "keydown"
       (e/fn [e]
         (when (= "Enter" (.-key e))
           (.preventDefault e)
           (submit!)))))
     (ui/button
      (e/fn []
        (submit!))
      (set! (.-innerHTML dom/node) send-icon)))))

wei00:10:05

basically trying to define an e/fn in the let statement (in order to use vars in the local scope). getting this error that I don't understand

wei00:10:03

it works if I move submit! to its own e/defn, but wondering if there's a way to do the above, similar to regular fn

xificurC06:10:59

Looks like you're only missing a new on submit!

wei06:10:37

ah that's it, thanks!

wei00:10:47

I'm seeing the message missionary.Cancelled: Subscription cancelled in my app, but everything seems to work fine. Should I be concerned, and how can I pinpoint the cause?

xificurC06:10:25

The easiest is to comment bisect. You shouldn't see the error

šŸ‘€ 1
Fabim10:10:56

how can I reset the client atom without breaking the value for the server? I am building drag and drop. unfortunately only the first time works. on subsequent drops (after the reset! ) the lead-dragging is not java.lang.long anymore and does not transact (no error shown).

(dom/on "drop" (e/fn [_]
                                        (e/server
                                          (e/discard
                                            (d/transact! !conn
                                                         [{:db/id lead-dragging
                                                           :lead/stage id}])))
                                        (reset! !lead-dragging nil)))
initialization is:
#?(:cljs (defonce !lead-dragging (atom nil)))
(e/def lead-dragging (e/client (e/watch !lead-dragging)))

xificurC10:10:03

2 observations you might be unaware of ā€¢ (e/server ...) and (reset! ...) run concurrently ā€¢ the reset! fires the lead-dragging watch

Fabim10:10:31

good to know I actually let the lead-dragging (let [lead-dragging lead-dragging] . this should give me the value right or do i deref?

xificurC10:10:59

it's still a cycle

xificurC10:10:36

you can first try (e/on-unmount #(reset! !lead-dragging nil)), maybe that'll be enough to resolve the cycle

xificurC10:10:45

otherwise yes, derefing breaks the cycle since you no longer reference the watch but the atom

Fabim10:10:54

the deref does not seem to get a value @!lead-dragging

xificurC10:10:16

if the reset! to nil runs first it will be nil, which is why I suggested to reset on unmount

xificurC10:10:43

also, is the deref on the client?

Fabim10:10:20

yes its in the surrounding (if-let [lead-dragging @!lead-dragging) but then does not update when I start dragging (set the lead-dragging value)

Fabim10:10:54

@U09FL65DK putting the unmount in the on drop fn worked. thanks for your help.

šŸ˜‰ 1
telekid15:10:51

I have an atom that I'd like to share between my standard ring HTTP routes and electric. As far as I can tell, the only way to accomplish this is to bind the atom to a dynamic var that closes over jetty ā€“ there's no way to pass a value as an argument to electric, right? NBD, just curious

xificurC19:10:40

no, there's no way to pass an argument to electric. Can you share some code/snippet or more concrete information?

Geoffrey Gaillard20:10:22

In electric e/*http-request* is bound to the WS UPGRADE ring request. You could write a ring middleware to assoc your atom in the request map before electric starts.

telekid20:10:32

> You could write a ring middleware to assoc your atom in the request map before electric starts Ah, of course. That would do, thanks.