Fork me on GitHub
Danny Almeida00:07:52

Can someone please explain what set! does in this example ? It's from this blog :

(let [socket (js/WebSocket. url)]
  (set! (.-onmessage socket)
        (fn [event]
          (let [tx-data (read-transit (.-data event))]
            (d/transact! conn tx-data)))))

Ruy Valle01:07:10

The equivalent of socket.onmessage = function(event) { … } in JS.

Danny Almeida01:07:36

Ah! thank you. I'm not well versed with was confused. So the function is set to socket.onmessage event ..right ?

Ruy Valle01:07:30

I would phrase it a little differently. When that socket receives a message, it will trigger the fn

Danny Almeida01:07:19

Yep..that's what i meant..but phrased it poorly 🙂 .. thank you


It's also nice paste code here and see what it's done in cljs


Hey everyone. Quick question: I initialised a new figwheel-main project with reagent. Then, I tried following to add an npm dependency. I get this error, however:

Configuration error in CLJS compile options: dev.cljs.edn
-- Spec failed --------------------

  {:main ..., :target :bundle, :bundle-cmd ...}

should be one of: :nodejs, :webworker

-- Doc for :target -----

  If targeting nodejs add this line. Takes no other options at the
  moment. The default (no :target specified) implies browsers are being
  targeted. Have a look here for more information on how to run your
  code in nodejs.

    :target :nodejs
I searched online for a while but couldn't find any solutions to this problem. Does anyone know what might be the problem?


My project.clj:

(defproject weather-app "0.1.0-SNAPSHOT"
  :description "FIXME: write this!"
  :url ""
  :license {:name "Eclipse Public License"
            :url ""}

  :min-lein-version "2.7.1"

  :dependencies [[org.clojure/clojure "1.9.0"]
                 [org.clojure/clojurescript "1.10.520"]
                 [reagent "0.8.1"]]

  :source-paths ["src"]

  :aliases {"fig"       ["trampoline" "run" "-m" "figwheel.main"]
            "fig:build" ["trampoline" "run" "-m" "figwheel.main" "-b" "dev" "-r"]
            "fig:min"   ["run" "-m" "figwheel.main" "-O" "advanced" "-bo" "dev"]
            "fig:test"  ["run" "-m" "figwheel.main" "-co" "test.cljs.edn" "-m" "weather-app.test-runner"]}

  :profiles {:dev {:dependencies [[com.bhauman/figwheel-main "0.2.3"]
                                  [com.bhauman/rebel-readline-cljs "0.1.4"]]


And dev.cljs.edn

^{:watch-dirs ["test" "src"]
  :css-dirs ["resources/public/css"]
  :auto-testing true}
{:main weather-app.core
 :target :bundle
 :bundle-cmd {:none ["npx" "webpack" "--mode=development" :output-to "-o" :final-output-to]}}


Your ClojureScript version is too old. Bump it up to at least the versions in the article


And the figwheel-main version too


I see, thanks. I was hoping the template would setup everything with working versions 😅 I'll bump up the versions and try again


Well, I can build without errors but now, lein fig:build cannot with to browser, so no repl propmt shows up. Also, the browser page is empty and console shows a 404 error


I'm trying to get the ASCII value of a character in a cljc file. How would I code the reader conditional for cljs?

(defn get-ascii
  "Gets the ASCII code of a char"
  #?(:clj (int c)
     :cljs (. c charCodeAt 0)))
Right now I have this but it doesn't seem/look right


changed it to

(.charCodeAt c 0)
and it looks pretty good, but can someone look over that?

Jacob Emcken13:07:24

Looks good to me 👍

👍 3

(. c (charCodeAt 0)) also works

👍 3
Timofey Sitnikov14:07:20

Good morning Clojurians, I am new to frontend and I am pulling my hair out trying to access a function. Following example on this, and looking at this code:

(ns hello-world.core)

(println "Hello world!")

(defn average [a b]
  (/ (+ a b) 2.0))
Is it possible to render the output of the average function to html? Firstly, I tried it like this:
<script src="out/main.js" type="text/javascript"></script>
    <script type="text/javascript">hello_world.core.average(1,2);</script>
but it complains that it is not defined. I tried both hello-world and hello_world but it keeps complaining that it is not defined. Nevertheless, I can type in the google chrome console hello_world.core.average(1,2) and it does return 1.5.


Try adding ^:export after defn.

Timofey Sitnikov16:07:21

Tried it, no luck. The error on the Chrome console is:

(index):10 Uncaught ReferenceError: hello_world is not defined
    at (index):10

Timofey Sitnikov16:07:20

My full html listing:

    <meta charset="UTF-8">
    <script src="out/main.js" type="text/javascript"></script>

    <script type="text/javascript">hello_world.core.test_fn();</script>
            Welcome Home</div>
Something tells me that it is not picking up the hello_world namespace.


Ah, so it's hello_world that's undefined. Then it seems like your out/main.js doesn't contain the required namespace.

Timofey Sitnikov16:07:24

I followed the example very closely

Timofey Sitnikov16:07:51

so looking at the main.js listing:

var CLOSURE_NO_DEPS = true;
if(typeof goog == "undefined") document.write('<script src="out/goog/base.js"></script>');
document.write('<script src="out/goog/deps.js"></script>');
document.write('<script src="out/cljs_deps.js"></script>');
document.write('<script>if (typeof goog == "undefined") console.warn("ClojureScript could not load :main, did you forget to specify :asset-path?");</script>');
looks like it is trying to source it.


you can see that it’s loading things asynchronously - adding script tags. it’s not defined at the time where you call it but subsequently becomes defined once the script loads. i think.


how do i get :`ignoreLogs` from this?

=> react/LogBox
#js{:ignoreLogs #object[ignoreLogs],
    :ignoreAllLogs #object[ignoreAllLogs],
    :uninstall #object[uninstall],
    :install #object[install]}




or (.-ignoreLogs react/LogBox)


not sure why i can’t get a hang of this type of js interactions but thanks again @thheller!


is there any documentation for the special form js* I see it used internally in cljs.core and i know we're not supposed to use it, but i'm just curious how it works


Should I have *warn-on-infer* at the top of every file or just the entry?


if you use set! you only need to change it once, it's not a per-file variable


you could use binding or set! to override the value wherever needed, as with any dynamic var


hmm actually the doc explicitly mentions "for the remainder of the file", but that's not how any vars that I know of actually work(?) > Once set the compiler will warn for the remainder of the file anytime it cannot determine the types involved in a dot form, whether property access or method invocation.


I guess it is important to note that if you set it after the ns form, it won't effect the files required by that ns - you need it before ns to do that


shadow-cljs has a setting that will turn on inference warnings for just your files


:compiler-options {:infer-externs :auto} in your shadow-cljs build config

👍 3

Got it, thanks