Fork me on GitHub
#clojurescript
<
2021-09-24
>
fsd02:09:26

Hi everyone, Just quick question, is there any way to get user location from the browser using ClojureScript?

lsenjov02:09:21

js/window.location ?

fsd02:09:11

Sorry I meant Geo location, latitude and longitude

fsd03:09:31

Thanks for that info Sorry I am fairly new to Clojure would it be something like this (.. js/navigator -geolocation -getCurrentPosition ) in javascript i could do something like this, if (navigator.geolocation) { navigator.geolocation.getCurrentPosition(showPosition); // (defn showPosition [position] (println position) ) How can I add a function like showPosition to this (.. js/navigator -geolocation -getCurrentPosition ) ? Thanks

lsenjov03:09:19

Try

(some-> js/navigator .-geolocation .getCurrentPosition js->clj)

lsenjov03:09:43

some-> does cool stuff, but mainly here it automatically checks for nulls before trying the next step

lsenjov03:09:50

Ooops, gotta edit that

lsenjov03:09:14

.-geolocation is js interop for getting a field

lsenjov03:09:38

Oh I read that wrong. You want to feed that function in?

lsenjov03:09:05

(some-> js/navigator .-geolocation (.getCurrentPosition showPosition))

lsenjov03:09:30

So the -> bit of some-> puts each result as the first argument of the next body

lsenjov03:09:07

It’s basically

(.getCurrentPosition (.-geolocation js/navigator) showPosition)
but with checking for nulls

lsenjov03:09:16

Note that what you use in showPosition will be a js object, so js->clj may/may not be of use here

fsd03:09:04

wow thanks so much buddy, Appreciate it 🙂

👍 1
Nik07:09:46

I get 'electron is not defined' error when I try to require electron npm module in renderer process. It, however, works fine in main process. I'm using https://github.com/ahonn/shadow-electron-starter. I updated electron and shadow cljs to latest versions "electron": "^15.0.0",  "shadow-cljs": "^2.8.110" ShadowCLJS EDN - {:source-paths ["src"] :dependencies [[reagent "0.10.0"]] :dev-http {8080 "resources/public/"} :builds {:main {:target :node-script :output-to "resources/main.js" :main app.main.core/main} :renderer {:target :browser :output-dir "resources/public/js" :asset-path "js" :modules {:renderer {:init-fn app.renderer.core/start!}}}}}

thheller07:09:47

electron tend to break stuff often. make sure you follow the electron documentation for the version you are using. they might have removed the global and made it a require or something

1
thheller07:09:47

also really need to see your code accessing js/electron. build config looks fine.

Nik08:09:52

Looking at latest docs, ipc stuff needs to happen in preload script instead of renderer. I can get message on main sent from preload script now

danbunea08:09:21

Hi, I need some help with an advanced compilation issue. I have a very simple cljs app that uses DataScript:

(ns taia.core
  (:require
   [reagent.dom :as r]
   [datascript.core :as d]
   [re-posh.core :as re-posh]))


(def taia-schema
  {:app/id {:db.unique :db.unique/identity}
   :app/name {}
   :app/type {}})


(def initial-db
  [{:app/id :bunea.projects
    :app/name "Projects"}
   {:app/id :bunea.mudanza
    :app/name "Mudanza"}
   {:app/id :bunea.deuras-alex
    :app/name "Deuras Alex Verano 2021"}])


(def conn (d/create-conn taia-schema))
(d/transact! conn initial-db)
(re-posh/connect! conn)

(defn home-page []
  [:span "Hola"])

;; -------------------------
;; Initialize app
(defn mount-root []
  (r/render [home-page] (.getElementById js/document "app")))


 
(defn ^:export init! []
  (->>
   (d/q '[:find ?id ?name
           :where
           [?a :app/id ?id]
           [?a :app/name ?name]]
         @conn)
   (println :db))
  (mount-root))
  
When initializing on the console I'd have:
:db #{[:bunea.projects Projects] [:bunea.mudanza Mudanza] [:bunea.deuras-alex Deuras Alex Verano 2021]}
When I used the advanced compilation with shadow-cljs I get:
:db #{[nil nil]}
shadow-cljs.edn:
{:source-paths ["client/src"]
 :dependencies [[binaryage/devtools "1.0.3"]
                [nrepl "0.8.3"]
                [reagent "1.1.0"]
                [datascript "1.2.2"]
                [re-posh "0.3.3"]]
 :builds       {:app {:target     :browser
                      :output-dir "client/public/js"
                      :asset-path "/js"
                      :compiler-options {}
                      :modules    {:app {:entries [taia.core]}}
                      :devtools   {:after-load taia.core/mount-root}}}

 :dev-http     {3000 {:root    "client/public"
                       :handler user/app}}}
Thank you in advance for your help, The source code is here: https://gitlab.com/danbunea/cljs.advanced.compilation

danbunea08:09:57

Update: it seems to be related to Datascript. If I read the entire database:

(d/q '[:find ?e ?a ?v
           :where
           [?e ?a ?v]],@conn)
the results are:
:db #{[1 :app/id 1] [1 :app/name Projects] [2 :app/id 2] [2 :app/name Mudanza] [3 :app/id 3] [3 :app/name Deuras Alex Verano 2021]}
and on advanced:
:db #{[1 :app/id nil] [1 :app/name nil] [2 :app/id nil] [2 :app/name nil] [3 :app/id nil] [3 :app/name nil]}
having all the values nil. Still can't figure out why

danbunea15:09:42

I'll check it out, thank you :)

yiorgos09:09:00

Hi guys, how can I pretty print in the console? I am using (cljs.pprint/pprint (-> e .-dataTransfer)) and the result that I see is: #object[DataTransfer [object DataTransfer]] Thanks

ag20:09:53

Have you enabled custom formatters in dev tools?

borkdude10:09:53

@g3o for JS objects it's often easier to use (.log js/console obj)

👍 1
yiorgos10:09:16

Sweet! that worked! thanks

p-himik12:09:05

And you can save a whopping space by using (js/console.log obj). :)

👍 1
p-himik20:09:58

What's the right way or what would be a good way to log extra data when a test fails? I just wrote this macro as a replacement for is:

(defmacro logging-is [expr msg & vals]
  `(let [result# (t/is ~expr ~msg)]
     (when-not result#
       (js/console.group ~msg)
       (try
         ~@(map (fn [v]
                  `(js/console.log ~v))
                vals)
         (catch :default ~'e
           (js/console.error "Unable to log something" ~'e)))
       (js/console.groupEnd))
     result#))

john20:09:06

Doesn't seem obvious why you'd want to do that, but that seems like the right impl to do it, unless you want to do more with the vals... Looking at is though, maybe a dumb question, but could it be rewritten as a function? Is there any short-circuiting going on in the chain up to assert-expr?

p-himik20:09:50

> Doesn't seem obvious why you'd want to do that The tests are executed in the browser - I can't easily just automatically break on a test failure and inspect everything in my cozy IDE. > Looking at `is` though, maybe a dumb question, but could it be rewritten as a function? No, at least not without losing a bunch of its functionality.

john20:09:08

ah, it's got these "special forms"