Fork me on GitHub
#re-frame
<
2022-09-27
>
beders02:09:48

hello there, has anyone out there been crazy enough to use vars as id for reg-event-* and reg-sub ? IIRC there’s no requirement that id needs to be a keyword. Or is there? Here’s an example:

(defn submit [db] (assoc db :bubu :lala))
(defn bubu [db] (:bubu db))
(rf/reg-event-db #'submit #'submit)
(rf/reg-sub #'bubu #'bubu)
(rf/dispatch [#'submit])
@(rf/subscribe [#'bubu])
=> :lala
This gives you compile-time checks on dispatches/subscriptions, but now you need to organize the order of your fns or use declare Using var instead of symbol allows for re-binding of submit/bubu. What are cons I’m not thinking of?

thheller03:09:27

constant var usage like that will substantially bloat your JS output size

beders04:09:58

is that because the names can’t be minified? i.e. why is that different from (rf/reg-sub ::submit submit)

thheller04:09:59

just look at the generated source for #'submit

beders04:09:31

thanks, Thomas. Will do

thheller04:09:13

new cljs.core.Var(
    function () {
      return hello.world.example;
    },
    new cljs.core.Symbol(
      "hello.world",
      "example",
      "hello.world/example",
      2147299913,
      null
    ),
    cljs.core.PersistentHashMap.fromArrays(
      [
        new cljs.core.Keyword(null, "ns", "ns", 441598760),
        new cljs.core.Keyword(null, "name", "name", 1843675177),
        new cljs.core.Keyword(null, "file", "file", -1269645878),
        new cljs.core.Keyword(null, "end-column", "end-column", 1425389514),
        new cljs.core.Keyword(null, "column", "column", 2078222095),
        new cljs.core.Keyword(null, "line", "line", 212345235),
        new cljs.core.Keyword(null, "end-line", "end-line", 1837326455),
        new cljs.core.Keyword(null, "arglists", "arglists", 1661989754),
        new cljs.core.Keyword(null, "doc", "doc", 1913296891),
        new cljs.core.Keyword(null, "test", "test", 577538877),
      ],
      [
        new cljs.core.Symbol(
          null,
          "hello.world",
          "hello.world",
          1080792214,
          null
        ),
        new cljs.core.Symbol(null, "example", "example", -115247617, null),
        null,
        14,
        1,
        18,
        18,
        cljs.core.list(cljs.core.PersistentVector.EMPTY),
        null,
        cljs.core.truth_(hello.world.example)
          ? hello.world.example.cljs$lang$test
          : null,
      ]
    )
  )

thheller04:09:56

includes all metadata and stuff. :advanced will shrink this a little but still a substantial amount more than just re_frame.core.reg_sub(new cljs.core.Keyword(null, "submit"), your.ns.submit)

thheller04:09:42

especially if you use the var twice per call

beders04:09:49

thanks for the insight. Very useful. I guess if I use symbols instead, I’d have to deal with re-registering subs and events to make sure they resolve to the correct fn

beders04:09:28

(for hot-reload)

thheller04:09:44

for the shadow-cljs UI (not re-frame) I went with tagging event functions via metadata and then having a macro find them all and reg-sub/event'ing it basically https://github.com/thheller/shadow-cljs/blob/master/src/main/shadow/cljs/ui/db/generic.cljs#L11

thheller04:09:09

could do something similar for re-frame I guess

beders04:09:23

true. That’s a good alternative. Thanks again for the quick help and guidance!

dvingo12:09:13

This isn't supported in re-frame, but Danny Freeman has a tiny lib that lets you use plain functions as subscriptions: https://git.sr.ht/~dannyfreeman/repose the main advantages are jump to definition integration for editors as well as proper module splitting/code motion support

👍 1
lispers-anonymous14:10:26

I am honored to have you out here hawking my wares @U051V5LLP. I need to circle back to that library and make it production ready 🙂 The event system is really primitive.

🧑‍🚀 1
awesome 1