Fork me on GitHub
#clojurescript
<
2020-07-15
>
Danny Almeida00:07:52

Can someone please explain what set! does in this example ? It's from this blog : https://tonsky.me/blog/acha-acha/

(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.

3
Danny Almeida01:07:36

Ah! thank you. I'm not well versed with javascript...so 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

3
Danny Almeida01:07:19

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

souenzzo12:07:31

It's also nice paste code here and see what it's done in cljs http://app.klipse.tech/

Prasoon10:07:47

Hey everyone. Quick question: I initialised a new figwheel-main project with reagent. Then, I tried following https://figwheel.org/docs/npm.html 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?

Prasoon10:07:16

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"]]
                   }})

Prasoon10:07:51

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]}}

danieroux10:07:18

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

danieroux10:07:32

And the figwheel-main version too

Prasoon11:07:40

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

Prasoon11:07:21

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

Sung13:07:17

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"
  [c]
  #?(:clj (int c)
     :cljs (. c charCodeAt 0)))
Right now I have this but it doesn't seem/look right

Sung13:07:03

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
dnolen13:07:16

(. 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 https://clojurescript.org/guides/quick-start, and looking at this code:

(ns hello-world.core)

(println "Hello world!")

;; ADDED
(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.

p-himik15:07:05

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:


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

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

p-himik16:07:29

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_UNCOMPILED_DEFINES = {};
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>');
document.write('<script>goog.require("process.env");</script>');
document.write('<script>goog.require("clojure.browser.repl.preload");</script>');
document.write('<script>goog.require("hello_world.core");</script>');
looks like it is trying to source it.

dazld20:07:57

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.

benny17:07:22

how do i get :`ignoreLogs` from this?

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

thheller17:07:47

react/LogBox.ignoreLogs

thheller17:07:22

or (.-ignoreLogs react/LogBox)

benny17:07:07

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

currentoor23:07:52

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

adam23:07:36

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

noisesmith23:07:55

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

noisesmith23:07:27

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

noisesmith23:07:42

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. https://clojurescript.org/guides/externs#externs-inference

noisesmith23:07:45

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

lilactown23:07:37

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

lilactown23:07:01

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

👍 3
adam23:07:49

Got it, thanks