Fork me on GitHub
#sci
<
2022-05-19
>
awb9905:05:15

async evals with the async-loader work great. but there are some situations where it does not work: The code below is a snippet which I eval in its entirity in sci in the browser. When I require [funny :as f] then it works, so (f/joke) in the code works. But when I just have [funny] and then I call (funny/joke), then it does NOT work. If my require is [funny :as funny] then (funny/joke) works also. A require with :refer does not work in an circumstance. (I didnt put an example for that into the snippet) this is a link to the async-eval in clojurescript: https://github.com/pink-gorilla/goldly/blob/master/goldly/src/goldly/sci/kernel_cljs.cljs

(ns demo.notebook.lazy
  (:require
   [r]
   [string]
   [user :refer [compile-sci-async println]]))

; this is a sci-cljs notebook!

(defn wrap-code [form]
  (string/join
   "\n"
   (map pr-str form)))

(defn show-atom [a]
  [:p (pr-str @a)])

(defn async-eval [form]
  (let [d (r/atom "async compiling..")
        p (compile-sci-async (wrap-code form))]
    (.then (:result p)
           (fn [result]
             (.log js/console "async result: " result)
             (reset! d result)))
    ^:R
    [show-atom d]))

^:R
[:div
 (async-eval
  '[(ns example1 (:require ["some_js_lib" :as my-lib]))
    (my-lib/libfn)])
 (async-eval
  '[(ns example2 (:require [adder :as x]))
    (x/add 7 13)])
 (async-eval
  '[(ns example3 (:require [funny :as f]))
    (f/joke)])
 #_(async-eval
    '[(ns example4 (:require [funny :as funny]))
      (funny/joke)])
 (async-eval
  '[(ns example5 (:require [funny]))
    (funny/joke)])]

borkdude10:05:17

@UCSJVFV35 I see. If you return {:handed true} then SCI does not process any :refer [x y z] and it assumes you have handled that yourself.

borkdude10:05:16

Let me work out a scenario where you would like to define a normal Clojure namespace in SCI using async load fn

awb9915:05:11

Perfect!!! Thank you so much!

mkvlr18:05:05

nice, we should give this a spin in nextjournal

borkdude19:05:14

could try next week - I assume you mean http://nextjournal.com, the product right?

👍 1
awb9920:05:51

(sci/add-namespace! ctx libname sci-ns))) ------------^------------------------------------------------------------------- Use of undeclared Var sci.core/add-namespace!

awb9920:05:00

I am getting this warning from shadow-cljs.

awb9920:05:06

its a normal function.

awb9920:05:53

does clojars babashka 0.3.5 contain this function?

awb9920:05:41

what is weird: clojars says 0.3.5 was published on may 1. but in github releases it says 0.3.5 was published on may 16.

awb9920:05:55

So I fixed it by using sci via git dependency.

awb9920:05:07

But the difference of github releases and clojars is still weird.

borkdude20:05:45

yes, it's not in 0.3.5, just on master

borkdude20:05:03

I forgot to make a release on github for 0.3.5 so I did that later

awb9920:05:40

thanks for the explaination!

awb9920:05:47

I have one question:

awb9920:05:57

So I am adding lots and lots of extensions,

awb9920:05:01

just with edn metadata.

awb9920:05:47

{:name "js-interop" ; build :lazy false :cljs-namespace [; clojure clojure.string clojure.walk applied-science.js-interop] :cljs-bindings {} :cljs-ns-bindings {'string {'split clojure.string/split 'join clojure.string/join 'escape clojure.string/escape 'blank? clojure.string/blank? 'replace clojure.string/replace 'lower-case clojure.string/lower-case} 'j {'get applied-science.js-interop/get 'call applied-science.js-interop/call 'get-in applied-science.js-interop/get-in 'call-in applied-science.js-interop/call-in 'obj applied-science.js-interop/obj} 'walk {'postwalk clojure.walk/postwalk 'prewalk clojure.walk/prewalk 'keywordize-keys clojure.walk/keywordize-keys 'walk clojure.walk/walk 'postwalk-replace clojure.walk/postwalk-replace 'prewalk-replace clojure.walk/prewalk-replace 'stringify-keys clojure.walk/stringify-keys}} }

awb9920:05:50

this is one example.

awb9920:05:07

lazy:false means this module put into the main js file of the bundle

awb9920:05:20

lazy:true means put this into a separate js module that gets lazy loaded.

awb9920:05:41

the reason why I go with this edn file is,

awb9920:05:02

so that I do not need any sci dependency in libraries that might be used without sci.

awb9920:05:15

I autogenerate the rest based on many of this definitions.

awb9920:05:31

Now I would like to build the sci environment in the best way possible

awb9920:05:52

I understand that macros from clojurescript will not work, and they need to be rewritten.

awb9920:05:58

but most of the other stuff should work.

awb9920:05:11

So this wrappers you use in your sci-namespace definitions,

awb9920:05:25

I guess they are all macros that supply meta data to the sci environment?

awb9920:05:13

Or is there any other reason why an export would better not be defined in an edn module?

borkdude20:05:14

clojure string and walk are already included in SCI so it doesn't make sense to make those lazy. you can see the config for js-interop here: https://github.com/babashka/sci.configs/blob/main/src/sci/configs/applied_science/js_interop.cljs in fact, you can use sci.configs as a library to get the config

borkdude20:05:40

you can check out nbb's shadow config: https://github.com/babashka/nbb/blob/main/shadow-cljs.edn everything except the core module is loaded lazily

borkdude20:05:21

the idea is to just compile separate modules and some are optional. the optional ones add something to the sci context when loaded

borkdude20:05:17

you can even make an optional module which first defines the sci context, so you can exclude SCI for a very thin build

borkdude20:05:29

and then the other ones add something to the context on load

awb9921:05:46

its an amazing idea! I will start using sci.configs !

awb9921:05:53

Do you plan to add more configs in the future?

awb9921:05:36

Or in other works: do you plan to make sci.configs to somethinig similar to the babashka pods registry?

borkdude21:05:38

yes, more configs will be there probably

awb9921:05:03

Great news!

awb9921:05:53

thanks a lot!

awb9922:05:59

It all works now @U04V15CAJ. But there is one edge case:

awb9922:05:01

(ns example3 (:require [funny])) (funny/joke)

awb9922:05:25

(ns example4 (:require [funny :refer [joke]])) (joke)

awb9922:05:38

(ns example5 (:require [funny :as f])) (f/joke)

awb9922:05:22

so all 3 examples work!

awb9922:05:29

when I start a new sci context,

awb9922:05:39

lazy loading works, sci eval works - perfect.

awb9922:05:49

the example5, whith :as f

awb9922:05:52

that does not work.

borkdude22:05:46

can you make a github issue with repro? then I'll take a look tomorrow

awb9922:05:07

ticket done. I kept it really short. But I verified all a couple of times.

borkdude14:05:59

ticket solved

awb9920:05:00

Great! Thankd a lot!

awb9920:05:22

Just some report on the async loader: in goldly I have 20 source code files that get evaled on startup. And I have a browser based sci clojurescript repl. I yesterday (before you fixed the issue) changed all requires so that I would avoid :refer and :as. All working perfect! This async loader really is the key issue to loading modules. I still have an async ui renderer loader that works with sync eval only. But I intend to rewrite it as soon as I did some more tests on the async eval.

awb9920:05:14

What I noticed is that even though it is just a very minor interface differene (promise instead of result) I had to change aa lot of code to make it work. So I will migrate 100% to the async loader. This is so that I can use modules. Because module loading does not work in sync eval.

👍 1