This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-01-10
Channels
- # babashka (17)
- # beginners (57)
- # calva (19)
- # cider (1)
- # clj-kondo (21)
- # clojure (36)
- # clojure-austin (15)
- # clojure-australia (1)
- # clojure-china (1)
- # clojure-europe (35)
- # clojure-filipino (1)
- # clojure-hk (1)
- # clojure-indonesia (1)
- # clojure-japan (1)
- # clojure-korea (2)
- # clojure-my (1)
- # clojure-nl (1)
- # clojure-norway (5)
- # clojure-sg (1)
- # clojure-spec (6)
- # clojure-taiwan (1)
- # clojure-uk (3)
- # clojurescript (7)
- # clr (9)
- # community-development (5)
- # cursive (14)
- # datalevin (1)
- # emacs (5)
- # events (5)
- # exercism (2)
- # figwheel-main (2)
- # fulcro (6)
- # funcool (3)
- # introduce-yourself (2)
- # joyride (7)
- # leiningen (4)
- # london-clojurians (9)
- # malli (3)
- # membrane (1)
- # missionary (54)
- # music (1)
- # nbb (2)
- # pathom (5)
- # pedestal (55)
- # rdf (13)
- # re-frame (10)
- # reitit (3)
- # shadow-cljs (17)
- # vim (58)
- # web-security (12)
e.g. how to turn this function into a flow that can be sampled infinitely?
(defn cb-fn [cb]
(cb (rand-int 1000)))
(m/ap (m/? (m/?> (m/seed (repeat (m/sp (cb (rand-int 1000))))))))
or
(m/ap (loop [] (m/amb (cb (rand-int 1000)) (recur)))
Untested, so hopefully no typosI see I misunderstood your question, sorry about that. Can you provide a more concrete example? Which API are we talking about?
location api
(defn location
([]
#?(:cljs
(p/promise [resolve reject]
(location resolve))))
([cb]
#?(:cljs
(when-let [gl (j/get js/navigator :geolocation)]
(j/call gl :getCurrentPosition
(fn [pos]
(let [{:keys [accuracy altitude latitude longitude]}
(j/lookup (j/get pos :coords))]
(cb
{:accuracy accuracy
:altitude altitude
:latitude latitude
:longitude longitude}))))))))
ideally I’d have a p/def in photon that updates once 1 minute with the users current location
simplified to
(defn cb-fn [cb]
(cb (rand-int 1000)))
(p/def get-location
(m/observe (fn [!]
(cb-fn !)
((m/sp (loop []
(m/? (m/sleep 10))
(m/amb (cb-fn !) (recur)))) {} {}))))
(p/defn App [route]
(p/server
(binding [db (p/watch db/conn)]
(p/server
#_(dom/div "route: " route)
;(Home.)
(p/client
(dom/div
(new get-location)))
))))
are a few things missing here? there are some empty maps in the fn body https://clojurians.slack.com/archives/CL85MBPEF/p1673385114656969?thread_ts=1673383809.732179&cid=CL85MBPEF
this works
(p/def t
(m/observe
(fn [!]
(cb-fn !)
((m/sp (loop []
(m/? (m/sleep 1000))
(sdom/location !)
(recur)))
{} {}))))
however:
(p/def user-location-working
(new
(m/observe
(fn [!]
;; this works
(! nil)
((m/sp
(loop []
(m/? (m/sleep 1000))
(sdom/location !)
(recur)))
{} {})))))
(p/def user-location-breaking
(new
(m/observe
(fn [!]
;; this breaks
(sdom/location !)
((m/sp
(loop []
(m/? (m/sleep 1000))
(sdom/location !)
(recur)))
{} {})))))
i think that loop can crash if it is faster than the consumer backpressure
right, it would be better to have a flow-like abstraction that lazily gets the location when sampled
it might be complex you can see the photon dom/clock if you are interested i believe it schedules next sample as a side effect of sampling or something like that
you’re right i think
here's an implementation of a task that returns the result of running a callback and a flow wrapper around that task. You'll need to sample it with a clock. (see Leo's answer below)
it's a discrete flow, to use it in photon you'll need to wrap it e.g. (m/reductions {} nil location>)
to provide an initial value
> it would be better to have a flow-like abstraction that lazily gets the location when sampled This is not possible because sampling is synchronous and the location api is asynchronous. @U09FL65DK’s approach is correct, first build a discrete flow that eagerly polls the api at regular intervals then turn that into a continuous flow with reductions + relieve
@U09FL65DK I tried your impl but cannot get a value to render
the following renders a single value that does not update. the other permutations froze up the browser tab
(p/defn MockLoc []
(p/client
(let [location (->> location>
(m/eduction (take 1))
(m/reductions {} nil)
#_(throttle 1000)
#_(m/latest identity)
new)]
(dom/div
"value " (pr-str location)))))
(def location>
(m/ap (loop []
(m/? (m/sleep 100))
(m/amb (m/? (get-location)) (recur)))))