Fork me on GitHub
#clojurescript
<
2020-08-17
>
Joe12:08:28

I have a npm dependency, CodeMirror, which has a css file in it. The js guides say to do @import 'codemirror/lib/codemirror.css'; but trying various combinations of require and import, I couldn't get that working in cljs. In the end I just copied the css file into my public dir and linked it from my index.html. Which is fine, but what would the proper way to do this?

polymeris15:08:06

I don't think you can import .css into cljs... it's probably meant to be imported into your SASS/LESS/PostCSS bundle, to be merged with the rest of your css.

Joe15:08:12

Ah gotcha, thanks. I don't know how to do any of that, so will have to read up.

polymeris15:08:22

Linking it directly from index.html should be fine, anyways. The only advantage of merging it with the rest of the css is that you save one request.

đź‘Ť 3
dabrazhe12:08:48

Hi. What is the difference between async/await in JS and the way promises are handled in Clojurescript? I am not really familiar with JS nor the whole front-end domain for that matter. I need to use a JS library that supports async and wondering if I can use promises, or need to do smth more complex.

đź‘Ť 3
athomasoriginal18:08:58

Besides the point I suppose, what what are you trying to do (which library?) In general though, you can 100% use promises in ClojureScript. To handle them, you just use .then. Having said this, if you asking if you can use async/await (which is a layer ontop of promises) in ClojureScript, I believe the answer is no. BUT you can use <p! from core.async which gives the same “feel” as async/await. More here: https://clojurescript.org/guides/promise-interop

athomasoriginal18:08:47

So my original question about the library is just about whether the library requires the ability to use async/await, or if the library is built on promises. If it’s the latter, you should still be able to use the library in ClojureScript and then if you want you can use something like <p!

Jimmy Miller18:08:45

> So my original question about the library is just about whether the library requires the ability to use async/await, or if the library is built on promises. Async/await is more or less syntactic sugar over promises. No library can require you use async/await. Async functions are just functions that return promises.

đź‘Ť 3
dabrazhe12:08:15

@U5K8NTHEZ thanks! @U6GNVEWQG great, <p! is new to me, although I've read that people tend to stay way from core.async in CLJS because it's not so easy to understand/complex. I will try to look for code snippets on how to use the library in question

athomasoriginal14:08:18

<p! is pretty new in general 🙂 It was partially added because there was a bit of conversation around the need for async/await so David Nolen, and a few others who were suggesting that core.async could handle it, threw in this little helper.

athomasoriginal14:08:13

> I’ve read that people tend to stay way from core.async in CLJS because it’s not so easy to understand/complex This depends on how you use it 🙂 I wouldn’t reach for it always, rarely do I need to on the front end, but <p! is pretty straightforward and I would argue not difference in “complexity” than async/await

schaueho16:08:50

I have a type hinting / inferencing issue I'm not clear how to resolve, could somebody give me a hint? I'm calling a javascript function on some js object that is returning a Promise (that would in turn resolve to a Buffer), using the <p! operator. If I compile this, then I get a Cannot infer target type in expression message. I tried type hinting on either the Promise or the Buffer, but to no avail.

Jimmy Miller18:08:01

It would probably be useful to share a portion of your code and what you tried that didn’t work. Would make it easier for people to help.

schaueho08:08:17

Here's a gist with some sample code showing my type inference issues: https://gist.github.com/schaueho/99882da7e8130b2d93e0d8247741d20e The JS code I'm trying to use is SodiumPlus, an encryption library.

thheller08:08:22

try (let [^js sodium (.-sodium js/window) ...

thheller08:08:33

or (<p! (.crypto_secretbox ^js sodium plaintext nonce key))

thheller08:08:02

(<p! ^CryptographyKey (.crypto_secretbox sodium plaintext nonce key)) is incorrect

schaueho08:08:28

This does not fix the issue, unfortunately. I get the exact same :infer-warnings like before.

thheller08:08:52

hmm I suspect that the typehints are lost due to go rewriting everything.

thheller08:08:42

(def ^js sodium js/window.sodium)

(defn encrypt-test [plaintext]
  (go
    (let [nonce (<p! (.randombytes_buf sodium 24))
          key (<p! (.crypto_secretbox_keygen sodium))
          ciphertext (<p! (.crypto_secretbox sodium plaintext nonce key))]
      (println "key: " (.toString key "hex"))
      (println "cipher:" (.toString ciphertext "hex"))

      (println "decrypted:" (.toString (<p! (.crypto_secretbox_open sodium ciphertext nonce key)))))))

🎉 3
thheller08:08:44

maybe that helps?

schaueho08:08:08

Yes, it does! Great idea that go will re-write things, I would never have thought of that in this context. Thanks a ton!

Sam Ritchie19:08:16

This is outrageous enough that I have to share... Sicmutils now works in Clojurescript! https://github.com/littleredcomputer/sicmutils

đź‘Ť 15
Sam Ritchie19:08:02

I haven't made a release yet, and I want this settle... but this gives us automatic differentiation, lagrangian + hamiltonian mechanics, and a generic, differentiable algebra package in clojurescript

dnolen19:08:25

cool stuff

🎉 3