Fork me on GitHub
#re-frame
<
2023-03-13
>
alex14:03:55

Hi, I can't find something about the issue that I have with re-frame on the web. So I try my luck here. I try to add a webworker to my re-frame application, but I do get some error from re-frame-10x.events. It tries to call a function on window, which does not exist in the webworker.

(rf/reg-event-fx
  :global/add-unload-hook
  (fn [_ _]
    (js/window.addEventListener "beforeunload" #(rf/dispatch-sync [:global/unloading? true]))
    nil))
Does someone have an idea to prevent this error?

p-himik14:03:54

First of all, side effects should be performed in effect handlers, not event handlers. Second of all, it has nothing to do with re-frame itself. You're just using functionality in web workers that's not intended to work with web workers. In this case, it's via re-frame-10. As far as I know, re-frame-10x isn't intended to be used with web workers at all, so all you need to do is to not use it there. :) I'm gonna guess that it's due to preloads, and when you compile the web worker code you can simply remove the re-frame-10x preload.

alex14:03:05

yes, it seems like a configuration issue with the preload. thanks

👍 2
Ryan16:03:02

I’m having a momentary struggle with structuring an event handler.. I’m upgrading an existing handler that can take 1 item to one that takes a vector of items, and I’m not sure where to do the iteration. The handler in question, halfway between supporting multiple values:

;; e.g. (rf/dispatch [::persist-entities {:people :person-id [{:person-id 1 :name "Bob"} {:person-id 2 :name "Tim"}]

(reg-event-fx ::persist-entities
  (fn-traced [{:keys [db]} [_ {:keys [entity-type-keyword entity-id-key value]}]]
    (let [id (:entity-id-key value)]
      {:db (assoc-in db [:app :entities entity-name id] value)})))
Is it a good bet to build 1 map that has the items key’d to their ids, then do one

update-in data [:app :entities entity-name] assoc id-keyed-map``` Or like.. a loop or for in the let to assoc-in a bunch of times? First options seems cleaner but wasn’t sure i there were hidden traps that way.

p-himik16:03:12

Seems like not re-frame related at all and just a generic data handling question. If so, then by itself it doesn't matter at all, as long as your data processing gets you the right data and that data shape is suitable for your needs.

p-himik16:03:48

If you can get away with just assoc'ing a single map - sure, do that. If you need to do more than that, I'd use reduce.

Ryan17:03:23

Awesome, thanks. I wasn’t sure if there was a re-frame wrinkle like some people seem to do this sort of work in subscription handlers etc, but newest docs seem to point at event handlers for this! Thanks as always

👍 2
p-himik17:03:10

There's no all-size-fits-all solution. All depends on what exactly your scenario is. In the end, it might not be important at all.

👏 2