This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-04-18
Channels
- # ai (2)
- # announcements (11)
- # beginners (34)
- # biff (14)
- # clerk (14)
- # clj-kondo (25)
- # clojure (27)
- # clojure-austin (1)
- # clojure-conj (6)
- # clojure-denmark (1)
- # clojure-europe (20)
- # clojure-hamburg (1)
- # clojure-nl (1)
- # clojure-norway (28)
- # clojure-uk (2)
- # clojuredesign-podcast (6)
- # clojurescript (43)
- # cursive (4)
- # data-science (2)
- # emacs (9)
- # hyperfiddle (9)
- # introduce-yourself (2)
- # jobs (3)
- # lsp (32)
- # missionary (31)
- # nbb (8)
- # off-topic (23)
- # rdf (23)
- # re-frame (10)
- # reitit (11)
- # releases (3)
- # rewrite-clj (4)
- # shadow-cljs (7)
- # specter (6)
- # sql (7)
- # xtdb (7)
Is there something obviously wrong with this reg-cofx
that I am missing? When I inject it into my app startup fx handler, it only ever breaks the app or shows [nil nil]
for the position.
(reg-cofx
:config/geolocation-enabled?
(fn [coeffects]
(.. js/window -navigator -geolocation
(getCurrentPosition (fn [position] (let [lat (.. position -coords -latitude)
lon (.. position -coords -longitude)]
(-> coeffects
(assoc :current-lat lat)
(assoc :current-lon lon))))))))
I suppose the app startup handler is being queued before the cofx handler?
that getCurrentPosition
looks like an async API? at least according to https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition#return_value the return value is undefined
I can use console.log
on it without awaiting it or using .then
or any of that jazz but yeah it doesn't seem to return anything
the geolocation API also prompts the user to allow access, and so you have to click the little popup before it runs this code I'm guessing
I tried wrapping it in (.addEventListener js/window "DOMContentLoaded" (fn [] ...)
to no avail
I think what you might want to do is a) create a reg-fx
, which invokes that geolocation API and stores the result in a atom (and perhaps dispatches an event indicating that the data is available) and b) fetch the contents of that atom in your reg-cofx
handler.
or alternatively: just dispatch an event in the callback function and include the location data in the event and then store it in the app-db as normal.
Yeah, you can't use async functions in co-effects.
I would definitely go with the latter approach of dispatching 2 events - one to call the geolocation function and the other to store that data in app-db.
It won't solve the timing issue though, so you gotta reshuffle your events in a way that makes it work even when getCurrentPosition
takes a lot of time (I think it doesn't resolve the promise till the user confirms the request).
Sounds good, thanks for the help/suggestions @U0178V2SLAY @U2FRKM4TW