Fork me on GitHub
#clojurescript
<
2021-09-02
>
Steve Pegoraro04:09:16

Hi all, I think I’m misunderstanding something trivial here, can I get some help? I’m using cljs to work with the Playwright browser automation library and am not seeing the result I expect when working with go blocks. Here’s what I have so far:

(ns lede.google
  (:require ["playwright" :as pw]
            [cljs.core.async :refer [go <!]]
            [cljs.core.async.interop :refer-macros [<p!]]))

(def chromium (.-chromium pw))

(defn get-title
  [url]
  (go
    (let [browser (<p! (.launch chromium #js{"headless" false}))
          page (<p! (.newPage browser))]
      (try
        (<p! (.goto page url {"waitUntil" "networkidle"}))
        (<p! (.title page))
        (catch js/Error err (js/console.log (ex-cause err)))
        (finally (.close browser))))))

(go (. js/console log (<! (get-title ""))))  ; Doesn't print anything to the repl

; If I store the result in an atom it works correctly
(def -r (atom nil))
(go (reset! -r (<! (get-title ""))))
-r ; Evaluates to "Google" after the browser operation completes
Why would it not be printing to the REPL? Sorry if this is such a beginner question, any help is greatly appreciated! For context I’m using CIDER

p-himik05:09:20

> Doesn't print anything to the repl The most probable cause: the REPL sets its printing machinery per expression, and go returns before anything its printed - so by the time that (. js/console log ...) function is called, the REPL printing is out of the scope.

p-himik05:09:34

It should print to the JS browser console instead.

thheller05:09:11

you are only using js/console.log which never ends up in any REPL output? only print does? (.goto page url {"waitUntil" "networkidle"}) thats also missing a #js

hbprince09:09:26

anyone here used clojurescript server side (nodejs)?

borkdude09:09:52

There is now a scripting tool for CLJS on Node.js called nbb: https://github.com/borkdude/nbb Does that count as server side?

borkdude09:09:06

There is also a web framework for node.js: https://github.com/macchiato-framework Personally I've never used it.

hbprince09:09:54

Anyone using on prod? would like to know the experience using it on prod.

borkdude09:09:15

I think @U050CBXUZ is using it in production

hbprince09:09:37

@U04V15CAJ not specifically macchiato, i have a small webapp which is using clojurescript on frontend, would like to use it serverside 🙂

borkdude09:09:08

yeah, I think there are examples, you could just start porting. CLJS just compiles to JS so it should be as stable as using Node.js directly really

yogthos13:09:22

yeah I’ve got a couple of services at work using it, but nothing that’s under heavy load 🙂

👍 2
hbprince16:09:07

@U050CBXUZ I just want to know if there is any "cljs runtime" overhead 🙂?

hbprince16:09:37

^considering performance/resource usage

borkdude16:09:59

if performance is a concern, you shouldn't be using JS at all ;)

hbprince17:09:59

@U04V15CAJ 😄 i am not doing that much compute operations IO perf works 🙂 was thinking about resource usage actually.. Want to deploy a medium app with the power of grayskull ( CLJS) but less resource usage 🙂

borkdude17:09:30

If you’re interested in resource usage you could also consider GraalVM native-image + Clojure

yogthos17:09:53

here’s an example for that incidentally https://github.com/yogthos/graal-web-app-example

yogthos17:09:24

and I haven’t done any serious performance testing for Macchiato, so really can’t say how well it holds up under load

👍 2
dnh119913:09:35

how do I create separate clojurescript packages? I have a large clojurescript project that I want to break up into multiple packages, but don't quite understand how to do this. Basically I want to create a local package and then import the code into another package

dnolen13:09:41

You can do this w/ deps.edn, multiple repos and git shas

borkdude13:09:42

if you mean packages as in libraries, you could also use :local/root to refer to libraries in the same repo

dnh119913:09:58

are there any examples of how to do this? I haven't used :local before for imports

borkdude13:09:11

Basically:

lib1/deps.edn
{:paths ["src"]}
app/deps.edn
{:paths ["src"]
 :deps { {:local/root "../lib1"}}}

borkdude13:09:26

we used to rely on publishing our libs to a private mvn repo but using local/root took care of that complexity

dnh119913:09:49

thanks, will try this out and take a look at guide

dnolen13:09:48

we don't use :local/root unless we need it - GH private repos and git deps works for us

dnolen13:09:57

So far to me seems superior to artifacts - artifacts are for other people outside your team

borkdude13:09:17

we have a mono-repo, I guess that's where local/root becomes more useful, or for local dev of libs (for us there is no difference)

dnh119915:09:29

I look at the local repos and tried using this. I'm a bit confused how this will integrate with node_modules. If a make a library that requires node modules, and use that library in a different project, how will the required in the imported library be included?

borkdude15:09:56

that's an interesting question.

borkdude15:09:21

I guess the same goes for libraries like reagent: in their case you should bring your own react

41ph4_Ph4un15:09:44

A quick clarification on specs and instrumentation. Am I correct if I'm under the assumption that instrumentation means that all used functions get their inputs validated and no other checks are generated? Sorry to bump in guys!

borkdude15:09:54

yes, the output is not validated

41ph4_Ph4un15:09:07

okay.. so here's an interesting situation

41ph4_Ph4un15:09:31

I have codebase where I run instrumentation in dev-mode.. and it's suppose to just validate the used functions

41ph4_Ph4un15:09:56

somehow.. it seems to.. begin generating arguments for the functions and fail those checks..

41ph4_Ph4un15:09:04

I literally only call (st/instrument)

borkdude15:09:21

did you perhaps spec a higher order function?

41ph4_Ph4un15:09:33

is that what could cause it? 😄

41ph4_Ph4un15:09:41

I have some yes

borkdude15:09:44

there is some weirdness in spec around this I think

41ph4_Ph4un15:09:10

yeah.. i mean.. I separated some modules in my code and some namespaces worked correctly

41ph4_Ph4un15:09:32

then on some cases it started to whine that it would need to require clojure.test.check and it's submodules

41ph4_Ph4un15:09:47

which I think was super odd because.. well.. I'm not generating anything or want to 😄

borkdude15:09:48

This is probably an issue for #clojure-spec

41ph4_Ph4un15:09:27

thanks! will take there! 🙂

borkdude15:09:18

perhaps declare a package.json in your lib on which you depend in a package.json in your app? (this is my guess)

dnolen15:09:47

@zuwadihi this is what :npm-deps is for if you're using ClojureScript directly

dnolen15:09:42

if the other project has deps.cljs in the root of its classpath, and this contains :npm-deps - then when you install deps those will get installed

borkdude15:09:03

oh, that's good to know!

dnolen15:09:44

most typical cases are solved in ClojureScript directly now

dnh119915:09:03

will this work with webpack, will webpack be able to see the libraries in the other node modules folder?

dnolen15:09:07

if they aren't solved, it's a medium amount of effort to solve them yourself - Krell continue to be a good example of how to do this the right way

dnolen15:09:20

@zuwadihi that's not how it works

dnolen15:09:42

those other projects are declaring what the leaf project needs to install

dnolen15:09:23

and yes webpack will see it

dnh119915:09:25

@dnolen do you have any links to github repos with an example of this? sorry im a bit newer and its a bit tough to fully grasp, if there is an example repo available that would help me a lot

dnolen15:09:56

but it is trivial

dnolen16:09:29

src/deps.cljs - the contents of that file will be {:npm-deps {name-of-node-module-package "version.string"}}

dnolen16:09:48

from the leaf project - clj -M -m cljs.main --install-deps

borkdude19:09:09

this isn't clojurescript, but just regular clojure

zhuxun219:09:06

Oh, you are right. Sorry I was confused.

Ben Hammond20:09:47

what library would you choose for crypto random strings? I see there are a few in npmjs, but I have no idea how to choose between them to be run inside a browser

Ben Hammond20:09:47

or should I have a REST endpoint to generate one at the server using [crypto-random "1.2.1"]

Ben Hammond21:09:04

I didn't realise that was a built-in