This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-06-21
Channels
- # aws (2)
- # aws-lambda (1)
- # beginners (62)
- # cider (31)
- # cljs-dev (16)
- # cljsrn (8)
- # clojure (115)
- # clojure-greece (3)
- # clojure-israel (2)
- # clojure-italy (13)
- # clojure-nl (8)
- # clojure-russia (5)
- # clojure-spec (3)
- # clojure-uk (146)
- # clojurescript (108)
- # clojutre (5)
- # code-reviews (3)
- # cursive (48)
- # datomic (22)
- # editors (20)
- # emacs (7)
- # fulcro (16)
- # graphql (10)
- # mount (2)
- # off-topic (47)
- # onyx (22)
- # re-frame (100)
- # reagent (5)
- # reitit (7)
- # ring-swagger (6)
- # rum (5)
- # shadow-cljs (51)
- # specter (2)
- # tools-deps (95)
- # vim (10)
- # yada (7)
Hi,
Hoping someone can shed some light on some behaviour I'm observing when using a partially applied map
function as the computational function with reg-sub
.
Example:
(ns me.scribble
(:require [re-frame.core :as r]
[re-frame.db :as rdb]))
(defn x [key items]
(map key items))
(r/reg-sub
::msgs
(partial map :msg))
(r/reg-sub
::msgs2
(partial x :msg))
(comment
(defn test-me []
(reset! rdb/app-db [{:msg "hello"} {:msg "world"}])
(r/clear-subscription-cache!)
[(deref (r/subscribe [::msgs]))
(deref (r/subscribe [::msgs2]))])
(test-me) ;; [("hello") ("hello" "world")])
If you load the above namespace into a REPL for a Re-frame ("0.10.5") project and call the test-me
function, then you should observe the following return value:
[("hello") ("hello" "world")]
From my point-of-view this is unexpected, I would expect:
[("hello" "world") ("hello" "world")]
I'm unclear why partially applying map
produces different results to partially applying x
?Sorry i just mean, can you show me the shape of the data you get for ur reg-sub functions
because when i try
((partial map :msg) [{:msg "hello"} {:msg "world"}])
i get the same thing as
((partial x :msg) [{:msg "hello"} {:msg "world"}])
yep when you run the functions outside of a subscription handler you get the same result for both functions
yep tried that and get the following returned from test-me
: [#object[G__10856] ("hello" "world")]
dumb question have you tried killing the repl and restarting it, just incase you had some older functions in it?
(rf/reg-sub
:time
(fn [db _] ;; db is current app state. 2nd unused param is query vector
(:time db))) ;; return a query computation over the application state
Taken from https://github.com/Day8/re-frame/blob/master/examples/simple/src/simple/core.cljs an exampleIn my example I'm using reg-sub
with no explicit input signals, which means it will default to app-db
So I'm registering a computation function that will be automatically passed app-db
(a vector in my example)
That to me says its something to do with map and processing a series of collections potentially
BTW - I have observed this behaviour in the browser - so it's not just a REPL related thing
i changed the example in re-frame and did somestuff to get it but its essentially v similar
because the way map works is it only maps over the same number of items in the given collections i believe
As to how to fix it? I think if you wrap it in a fn and destructure it a different way that might work?
my db is like
{:time (js/Date.) ;; What it returns becomes the new application state
:time-color "#f88"
:msgs [{:msg "hello"} {:msg "world"}]}
So for your example to get it working in your test-me
function you could do something like
Where you make ur app-db a map instead of a vector, you would need to change both functions i believe
I can also confirm that my x
function also receives the additional argument (as you would expect) - although obviously x
doesn't use it as map
does (hence x
worked!)
yeah also one thing to note, (reset! rdb/app-db [{:msg "hello"} {:msg "world"}])
I think this is a little unusual
also
;; -- Application State --------------------------------------------------------------------------
;;
;; Should not be accessed directly by application code.
;; Read access goes through subscriptions.
;; Updates via event handlers.
(def app-db (ratom {}))
😅- yeah I'm just using reset!
to enable testing in the REPL - I do use a map
in real life 😉