Fork me on GitHub
#clojurescript
<
2019-02-15
>
currentoor05:02:06

I found this old thread on the google group discussing binding in CLJS https://groups.google.com/forum/#!topic/clojure/6cmnkHmHBNw The main point is this

user=> (def ^:dynamic x "root")
#'user/x
user=> x
"root"
user=> #'x
#'user/x
user=> ((binding [x "dynamic"] (fn [] x)))
"root"

currentoor05:02:13

Is there a work around to make binded vars respect local bindings when used within nested functions?

lilactown05:02:49

well, you can lexically bind the value

lilactown05:02:01

cljs.user=> ((binding [x "dynamic"]
       #_=>    (let [x' x]
       #_=>      (fn [] x'))))
"dynamic"

dpsutton05:02:15

i think the nature of dynamic vars is that their value is set by the current binding. your fn is constructed in the binding but is evaluated after the binding

dpsutton05:02:01

if you move the binding out of the invocation it should work, or if you capture the current binding into a non dynamic var

dpsutton05:02:59

@currentoor your example is equivalent to this form:

(let [f (binding [x "dynamic"]
          (fn [] x))]
  (f))
where it should be clear that the binding is no longer in effect

currentoor05:02:47

@dpsutton thanks, this this seems to work

(def ^:dynamic x "root")

(defn foo []
  x)

(binding [x "dynamic"]
  (foo))

currentoor05:02:18

but for some reason when i do this across an iframe element in react it doesn’t get the dynamic valuue

currentoor05:02:25

perhaps i’m doing something wrong

currentoor05:02:32

i’ll play with my code more

currentoor05:02:48

i have a modal library i’m using, and i want to bind a *mount-node* var to an iframe’s document.body whenever a component is used inside an iframe

currentoor05:02:36

and default to the outermost document.body whenever the component is used in a regular context

currentoor05:02:47

is there any reason that shouldn’t work?

lilactown05:02:55

i’m not sure how the iframe interacts with the dynamic var

lilactown05:02:33

is there a reason you’re using dynamic vars and not React context?

currentoor17:02:31

@lilactown great idea!, thanks

artrmrno06:02:43

Hi, I have the following code to create a Firebase Cloud Function that reacts when an entry is added to the database:

(ns gcf.core
  (:require ["firebase-functions" :as fb-fn]))

(defn db-ref []
      (.ref (.-database fb-fn) "/trollbox/{messageId}"))

(def translate-fn
  (.onCreate (db-ref)
             (fn [snapshot context]
                 (println "Executing function...")
                 (println (str "Value: " (.val snapshot))))))

(def exports #js {:translateHTTP translate-fn})
It's working fine when I test locally using the firebase shell, but when I deploy to firebase I get the following error when the function is invoked:
TypeError: a.D is not a function
    at /user_code/gcf.js:247:1067
    at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
    at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
    at /var/tmp/worker/worker.js:779:24
    at process._tickDomainCallback (internal/process/next_tick.js:135:7)
Looks like the code for (.val snapshot) is the one causing the issue because the function works when I remove it. Any ideas on how I can debug this? I'm using shadow-cljs with the following configuration: shadow-cljs.edn
{:source-paths ["src"]
 :builds       {:gcf {:target :node-library
                      :main gcf.core/main
                      :output-to "gcf.js"
                      :exports-var gcf.core/exports}}}
packages.json
{
  "name": "functions",
  "description": "CLJS Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "firebase-admin": "^7.0.0",
    "firebase-functions": "^2.1.0"
  },
  "private": true
}

lilactown07:02:16

you can try building with shadow-cljs release --debug and see if that gives any more info

lilactown07:02:01

it sounds like (.val snapshot) is being optimized into something. changing it to (.val ^js snapshot) might fix it

thheller08:02:41

@artrmrno add :compiler-options {:infer-externs :auto} to your build config and you'll get a warning that you need to typehint the .val invoke. Also :main does not apply to :node-library configs, you can remove that.

TobiH16:02:42

Hi guys!I have a seemingly big issue with figwheel. I cannot for the life of it get the figwheel repl to work not even thinking about getting it to work with cider. I have now tried it for days and with all the famous linux distros (arch, debian, ubuntu, fedora,...). I just wanted to get a simple project to work to get it eventually to work with cider, because that introduces another set of possible issues and I haven't got it to work. So I cloned the example flappy-bird-demo-new project and ran

lein fig:build
. After that the website with the game opens and I see the repl prompt in my terminal (cljs.user ==>). I can do that several times and it works. BUT: as soon as I reboot and try again to build it with
lein fig:build
I only see the message
Opening url http//:localhost:9500
After that it just stays there and nothing happens.

TobiH16:02:41

I also completely deleted the project, cloned it again and ran

lein fig:build
...Nothing. I also tried it with the clj tools version. Same thing. Also any other project and create from a leiningen figwheel template does not give me a cljs prompt.

TobiH16:02:49

I'm getting really frustrated, because I cannot figure out what the problem is. I also did not find anything online about it.

TobiH16:02:59

I'm using Leiningen 2.9.0 with Java 1.8.0_202b26.

Jan K16:02:34

So your browser doesn't open with the localhost url? What happens if you open it manually? Btw there's a dedicated #figwheel-main channel

TobiH16:02:04

No, sorry. The webpage opens

TobiH16:02:08

everytime

TobiH16:02:35

but I never get a cljs prompt in my terminal

Jan K16:02:49

Have you checked the browser console? Any errors?

TobiH16:02:47

I checked that once and did not see anything relating to that, but let me check again.

Jan K16:02:22

The CLJS prompt opens once the browser successfully connects to figwheel. It should report to the console.

TobiH16:02:48

log:Opening the WebSocket on  logger.js:367:5
 [goog.net.WebSocket] Opening the WebSocket on  console.js:203:20
log:WebSocket opened on  logger.js:367:5
 [goog.net.WebSocket] WebSocket opened on  console.js:203:20
log:Session ID: e69315e5-95e0-4101-90c7-9908d271cec0 logger.js:367:5
 [Figwheel REPL] Session ID: e69315e5-95e0-4101-90c7-9908d271cec0 console.js:203:20
log:Session Name: Rosie logger.js:367:5
 [Figwheel REPL] Session Name: Rosie

TobiH16:02:03

and some warning about react

TobiH16:02:13

The thing I don't get is. Why does it happen after a reboot? I created some snapshots on my virtual machine before and after I built the project the first time and after the restart. If I go back to the snapshot where I have not yet built the project it always works. As soon as I reboot I don't get the prompt anymore.

Jan K16:02:47

Seems like the browser is connecting fine. Sounds weird, I'm not sure what could be the problem.

Ian Terrell16:02:18

Can anyone offer a pointer on how to create a command line vanilla (non-lein, figwheel) REPL for a Clojurescript project that can access installed node_modules? All in the same directory I have a src folder, a deps.edn that includes src in :paths, package.json and node_modules with installed modules. I can boot a REPL with clj -m cljs.main -re node, and it can find my code. But it can't find the installed Node modules.

ClojureScript 1.10.439
cljs.user=> (require '[db.etl.staging])
module.js:545
    throw err;
    ^

Error: Cannot find module 'aws-sdk'
    at Function.Module._resolveFilename (module.
    ...
Any pointers?

TobiH17:02:08

I'm suprised that nobody else seems to have the same problem. As I said, I tried it with several different Linux distros and with jdk 11. On my Virtualbox and directly on my laptop. I always have the same issue.

TobiH17:02:30

What are you using as a system?

dpsutton17:02:10

@dev632 just staying in your base system, what is your host OS?

TobiH17:02:41

I'm using windows 10

TobiH17:02:08

But as I said I have the same issue if I install a clean system on my thinkpad

dpsutton17:02:16

thinkpad = linux?

TobiH17:02:00

yes. and for testing puposes I have another asus laptop on which I run Windows 10 and Virtualbox.

dpsutton17:02:41

ok. let's stay in linux on the thinkpad. it's lein 2.9.0, figwheel-main 0.2.0, correct?

TobiH17:02:43

A moment please. I have to boot it up

TobiH17:02:28

Ok. Leining is 2.9.0, Java is 1.8.0_202 and the figwheel-main version from the sample project is 0.1.5

TobiH17:02:51

rebel-readline-cljs is at 0.1.4 if it's of any use

dpsutton17:02:44

bump figwheel main to 0.2.0 so you can follow the setup guide

lilactown17:02:10

FWIW I had to bump figwheel main yesterday to 0.2.0 when I tried to use it with Clojure 1.10. Dunno if that helps

dpsutton17:02:50

^ good data point. Hopefully it just works ™️ now

TobiH17:02:13

OK. So I changed it in the project.clj of the flappy-bird-demo-new project but I still have the same problem

dpsutton17:02:04

can you paste the alias in your project.clj that fig:build is supposed to be invoking?

TobiH17:02:25

"fig:build" ["trampoline" "run" "-m" "figwheel.main" "-b" "flappy" "-r"]

TobiH17:02:13

thing is. even if i try to create to most simple project with only a folder and a deps.edn with the following line:

{:deps {com.bhauman/figwheel-main {:mvn/version "0.2.0"}}}
Even then I don't get to the prompt.

TobiH17:02:02

Of course I then run

clj -m figwheel.main

dpsutton17:02:08

ok. so i cloned that project, ran lein fig:build and immediately it opened up my browser and i started playing flappybird

dpsutton17:02:29

i didn't bump figwheel main or anything. so there's something up with your install it seems

TobiH17:02:35

yes. that works for me too

dpsutton17:02:37

and there are no vms, no docker, anything funky going on?

dpsutton17:02:43

oh. i thought it didn't work

dpsutton17:02:47

what's the issue you are seeing then

TobiH17:02:02

Can I post pictures here?

TobiH17:02:11

maybe a screenshot is more clear

dpsutton17:02:25

what is "the same problem" i'm not sure what we are trying to fix

TobiH17:02:22

The problem is, that the last thing I see in my terminal after I started the project is:

Opening URL 

TobiH17:02:32

and I don't get to the prompt

TobiH17:02:47

cljs.user==>

TobiH17:02:59

where I could directly interact with the browser

dpsutton17:02:38

what are you greeted with when you open that url manually?

TobiH17:02:04

same as when it's opened automatically

TobiH17:02:43

in the console I see that it says: [Figwheel REPL] Session Name: Vanita

dpsutton17:02:45

i'm still not quite following. when you open it manually, it behaves the same as when it works automatically. and in neither case do you get a connected repl?

dpsutton17:02:49

what is visible in the browser tab?

dpsutton17:02:05

i got the game visible. is your screen just plain white? does it say something like "keep on figwheelin'"

TobiH17:02:05

I can play the game in the broswer tab

dpsutton17:02:20

so the only issue is that the browser doesn't open automatically?

dpsutton17:02:29

but when you open it manually everything works?

TobiH17:02:48

that is what I get.

TobiH17:02:34

if I run the project on a freshly installed system then after the last line I get a prompt like:

cljs.user==>

dpsutton17:02:47

ok. this is what you get when the browser seems to be working? it never connects back? And again there are no vms involved, no docker, no shenanigans?

TobiH17:02:13

the browser is always workin

dpsutton17:02:25

ok. sorry i was so dense. just wanted to make sure i understood exactly what was going on

TobiH17:02:33

no problem

dpsutton17:02:49

look in the sources tab in the browser console. we can check if figwheel main is putting its required things to start figwheel

dpsutton17:02:56

look for any 404's or anything like that

TobiH17:02:22

ther is a figwheel folder inside the "cljs-out -> flappy" folder

TobiH17:02:33

the only 404 I see is for the favicon

dpsutton17:02:00

see something like this? requiring figwheel core?

TobiH17:02:15

what do you mean by "requiring figwheel core"

TobiH17:02:48

otherwise it seems the same to me

dpsutton17:02:50

do you see that script right there "goog.require("figwheel.core");

dpsutton17:02:13

ok. then i don't know

TobiH17:02:36

are you using linux yourself?

dpsutton17:02:08

i'm doing this on my mac. was running a job on my fedora machine

dpsutton17:02:11

i can do it there though

dpsutton17:02:15

it cranked right up immediately

dpsutton17:02:22

i'm sorry. i don't know what's going on with your stuff

dpsutton17:02:43

are you willing to try something really weird and almost nonsensical?

TobiH17:02:50

there seems nothing to loose here

dpsutton17:02:37

nevermind, it doesn't make any sense

lilactown17:02:01

what version of lein are you using dpsutton?

dpsutton17:02:03

there are some slow code loading issues related to the 202 version of java you are using. I was gonna see if you could go back to an older java version

TobiH17:02:08

^^ that thing itself doesn't make any sense 🙂

dpsutton17:02:11

i'm 2.9.0 on all boxes

dpsutton17:02:22

latest CIDER requires it due to nrepl 0.6.0

TobiH18:02:37

thanks for your help

TobiH18:02:27

like I said it I'm surprised that nobody else on linux seems to have this problem

dpsutton18:02:16

well, from the legions of people who don't, we're surprised you have the problem 🙂

dpsutton18:02:21

i hope you can resolve it.

dpsutton18:02:28

the worst thing to hear is "works on my machine"

TobiH18:02:40

if I can I'll you all know 🙂

artrmrno18:02:33

Thanks @lilactown & @thheller, your recommendations solved my problem.

👍 5