Fork me on GitHub
#clojurescript
<
2018-04-25
>
alice00:04:02

Ah so it's like... an internal subsciber?

sova-soars-the-sora00:04:57

yep. what inputs are you about to use

sova-soars-the-sora00:04:52

it kinda looks like a little ear šŸ‘‚ :<- for listening

alice00:04:04

That's.. one way to remember that

eoliphant07:04:03

Hi, I have a cljs macro question/issue. I have some macro code that generates re-frame subscriptions based on a mini-DSL. I think Iā€™ve followed all the guidance for them. The macro is in a separate namespace, and in a .cljc, etc. I need to have the macro execute when the file loads so I have an inline call to it in the file where itā€™s included. It seemed to be working at one point but now it doesnā€™t seem to get called at all, and I canā€™t figure out why

;;; macros.cljc  
(defn gen-calc-sub
  [calcs [field [calc args]]]
  (print "creating sub for: " field)
  `(rf/reg-sub
     ~field
     (fn [_# _#]
       ~(mapv (fn [arg] (if (calcs arg)
                          `(rf/subscribe [~arg])
                          `(rf/subscribe [:form/cur-field ~arg]))) args))
     (fn [args#]
       (calc/run-calc ~calc args#))))

(defmacro gen-calc-subs
  [calcs]
  (print "creating subs for: " calcs)
  (let [calc-fields (set (map first calcs))                 ;for quick membership test
        ]
    `(do ~@(map #(gen-calc-sub calc-fields %) calcs))))  

;;; file where it's used
(ns f.....events-subs
 ...
  (:require-macros [....macros :as fm]))
...
;;; invocation
(fm/gen-calc-subs {:sf425/cash-on-hand
                [:calc/subtract [:sf425/cash-received :sf425/cash-disbursed]]  })
Iā€™ve done something similar in Clojure with no problems, so I guess Iā€™m missing some other CLJS specific thing. Iā€™ve checked the expansion in the CLJS and CLJ repls, and Iā€™m getting the expected output in both places

ingesol08:04:15

Just the obvious question first: are you 100% sure this needs to be a macro? šŸ™‚

eoliphant11:04:44

Lol hmm good point

eoliphant11:04:34

Gonna be pretty annoyed with myself if it doesn't lol

ingesol12:04:41

The answer was no 10 out of the 10 last times I asked myself that šŸ™‚

eoliphant12:04:56

yeah, ok, hmm, i need to call reg-sub with functions that i need to create programmattically so i kind of think i do

eoliphant12:04:57

now the base sub is just a vector, so might work, maybe, but env then itā€™s a vectory of function calls that shouldnā€™t be evalā€™ed at that point

eoliphant12:04:11

it seems macroish

eoliphant12:04:19

but will play around with it

ingesol16:04:03

Yeah, if you need to postpone evaluation, then you're stuck. Don't know much about your problem, unfortunately

eoliphant20:04:40

yeah itā€™s frustrating, I was so close to a nice generic solution lol Iā€™d written this crazy chunk of code that would fire off new events if fields that computed fields depended on changed, etc. Then, I just happened to re-read the re-frame docs, Iā€™d kind of blasted through them when I first started playing with it, and realized I pretty much totally missed level 3 subs lol. So replaced like 50 lines of code with that macro

eoliphant20:04:48

now just need it to work lol

ingesol20:04:40

Well, at least you're learning :)

eoliphant20:04:31

yeah iā€™d been doing more server side clojure

eoliphant20:04:58

itā€™s weird how you get used to all of the stuff yo udonā€™t have to deal with

eoliphant20:04:30

Iā€™m trying to push a full stack clojure(script), datomic, kafka, microservice solution for one of my projects

eoliphant20:04:43

so iā€™m building out prototypes for a slice of functionality

eoliphant20:04:38

in 3 different stacks, pure java/react/lagom, then isomorphic kotlin with react on the client and both lagom and axon, vs the clojure stack

eoliphant20:04:02

the amount of code iā€™m having to write, even with frameworks that provide a lot of plumbing.. is pretty annoying lol

ingesol20:04:08

Wow, that's impressive. I love doing full stack clojure, have also tried java/react.

amarjeet06:04:54

Hey @eoliphant, Did you find the solution to your problem? I have faced a similar issue when I tried Macros in cljs a few days back. I am wondering if its some kind of config (clj->cljs) issue.

eoliphant14:04:56

No I didnā€™t šŸ˜ž Itā€™s annoying the hell out of me lol. Although, thanks to good oā€™ homoiconicity, I just ran the macro in the REPL, and pasted the code in for now. But I want to get that working as I want to pull this stuff out into a general library

eoliphant14:04:45

oh and there was another bizarre thing I didnā€™t mention. There was a weird lapse in referential transparency lol The macro took a map that was basically a little calculation DSL for form fields {:my-field [:sum [:field :field2]) so I had

(def calcs {:my-field [:sum [:field :field2]))
(re-frame-sub-generator-macro calcs)
was blowing up and saying calcs wasnā€™t defined šŸ˜ž I didnā€™t even dig into that and just put the map directly into the call

amarjeet15:04:08

haha...yeah, in fact I also just pasted the results (for now) to test the system I was building.

eoliphant15:04:43

yeah, itā€™s frustrating, I really need this to work lol

eoliphant15:04:10

tried upgrading to 1.10.x just for giggles. That didnā€™t help

sova-soars-the-sora07:04:20

@eoliphant ... works in CLJS repl ? o.O

wombawomba09:04:57

Iā€™m trying to use a NPM module (https://github.com/conorhastings/react-syntax-highlighter) in my project, but I canā€™t quite figure out how to get it working. Initially, I just added :npm-deps {"react-syntax-highlighter" "7.0.2"} to my cljsbuild :compiler config. This caused figwheel to fail with Error: Cannot find module '@cljs-oss/module-deps'. I eventually figure out that I had to run npm install @cljs-oss/module-deps, which fixed the error. Is there a better way to do this? Anyway, Iā€™ve since been trying to import the code, but none seem to work. AFAICT Iā€™m supposed to be able to just do (:require [react-syntax-highlighter]), but that gives me a ā€œNo such namespaceā€ error. So does pretty much everything else Iā€™ve tried. (FWIW I have verified that node_modules/react-syntax-highlighter exists). What am I missing here?

mfikes10:04:05

@wombawomba Iā€™m curious, did you have :install-deps true set as a compiler option?

wombawomba11:04:59

I did notā€¦ will try with that enabled and see how it goes šŸ™‚ I guess I did npm install react-syntax-highlighter to get it in place

wombawomba11:04:00

I still seem to get No such namespace; react-syntax-highlighter, even with :install-deps true enabled

justinlee15:04:18

@wombawomba Note: the npm-deps feature will run the npm module through advanced compilation, which may or may not work with the library you want to use. If you are going to use a lot of npm libraries, you will probably be happier with shadow-cljs or using webpack. https://gist.github.com/jmlsf/f41b46c43a31224f46a41b361356f04d

wombawomba15:04:58

Thatā€™s good to know, thanks! For now, I only want this specific package, so Iā€™d like to see if I can get npm-deps working. Otherwise Iā€™ll definitely try one of those approaches.

wombawomba15:04:51

From my description above, do you think the advanced compilation is likely to be the culprit, or is it possible that I missed something?

justinlee16:04:19

I donā€™t think thatā€™s the issue. Advanced compilation issues are usually something like ā€œb.c is undefinedā€ or some kind of reference to a name-mangled symbol

wombawomba12:04:33

Got it. Thanks

eoliphant11:04:46

@soya yes it seems to work in the repl

jonrob12:04:34

ive got some code to generate a largeish (a few mbs) data structure from a text file; id like to pregenerate this data structure and then distribute it with the rest of the cljs code to live in memory on the client. whats the best way of going about this?

gklijs12:04:21

Whatā€™s the purpose of the data structure, is it a javascript object, or is it used as clojure data?

grav13:04:23

I have a silly little app that uses hiccup for generating html. Iā€™d like to do this in ClojureScript, since itā€™s going to be in AWS lambda. Iā€™m looking at a few forks of hiccup that promise to work with Cljs but theyā€™re pretty old. Another strategy would be to do it in React, but it seems like a detour to first generate dom elements and then render them to a string. Any thoughts?

gklijs13:04:37

I use https://github.com/teropa/hiccups for now, but itā€™s old and some things from hiccup are not supported. from https://github.com/weavejester/hiccup/issues/127 itā€™s not clear if cljs will be supported by hiccup

jonrob14:04:31

the data structure is for use as clojure data... eg its representing a tree and so is queried by clojurescript functions.

gklijs14:04:02

Yes, mine is the same, I have cljc function generating the data structures and with either hiccup or hiccups I turn them intro a string

mkarp14:04:17

Hey everyone! I have a question about stacktraces within nodejs targets. With :optimizations set to :simple I canā€™t make cljs files appear in them. My assumption is that sourcemaps arenā€™t used for some reason. Iā€™ve made a simple demo here https://github.com/mkarp/cljs-sourcemap-demo. Any ideas and feedback are welcome!

j-po16:04:20

It looks like clojurescript defines the cljs.spec.alpha namespace across two files: alpha.cljs and alpha.cljc. Is there a defined order those files are read in? Can they not reference each other at all? I made a toy project with a .cljc, .cljs, and .clj all with the same name and namespace which, on requireing it in a clojure repl, gave me access to stuff from the .clj file but not the .cljc file

orestis17:04:23

.cljc will be searched only if .clj or .cljs are not found (depending on the env, of course). (AFAIK)

j-po17:04:50

How does it work in cljs.spec.alpha, then?

orestis17:04:53

My guess is that .cljc only contains specs for macros? Since macros are expanded in Clojure, not CLJS.

orestis17:04:51

(Note, I donā€™t have first-hand experience here, just Iā€™ve seen this pattern a lot in the channel)

j-po17:04:42

Aha! It's the reverse, in factā€”the .cljc file (mostly) only contains macros for specs, and the .cljs file's ns form :refer-macross "itself" to "get" them

j-po17:04:19

Thanks for the help! I'm almost entirely a clj dev but I'm trying to make a library cross-platform

orestis17:04:21

Hah, Iā€™m not even a clj dev (ā€¦yet) ā€” just been reading a lot šŸ™‚ ā€” note, I think recent CLJS added some :require sugar so you might not need to do :refer-macros any more ā€” need to search for this.

pesterhazy17:04:30

It looks like require('source-map-support').install(); isn't emitted with :target nodejs and simple optimizations with clojurescript 1.10.238 (it is emitted with "optimzations none"). You can enable sourcemaps by adding (.install (js/require "source-map-support")) to your code. Is this the expected behavior?

mkarp07:04:37

Iā€™ll try it now, thank you!

mkarp08:04:46

Works very well in the project too. Thank you!

pesterhazy09:04:01

Awesomesauce

pesterhazy17:04:19

This is with cljsbuild

richiardiandrea17:04:49

oh, that is suprising a bit, I have noticed the same but I thought "it was just me"

pesterhazy17:04:54

@richiardiandrea yeah not having used the node target before I'm not sure if my expectation ā€” that the "source-map-support" shim should be included automatically ā€” is correct

richiardiandrea17:04:12

not sure either, would wait for someone like David to answer, I now do what you did above in my lumo code

pesterhazy18:04:56

Just tried it with cljs.main and it seems to work there. Maybe a lein cljsbuild issue?

alex-dixon23:04:16

Soo Iā€™d like to do this

(let [{:user/keys [id :as user-id]} user
       {:company/keys [id]} company]
[user-id id])

jimberlage23:04:27

What do user and company look like in this scenario?

jimberlage23:04:50

Assuming I have your use case right, you could do

(let [{{user-id :id} :user/keys} user
      {{id :id} :company/keys} company]
  [user-id id])

jimberlage23:04:17

[jim@goliath ~]$ lumo
Lumo 1.8.0
ClojureScript 1.9.946
Node.js v9.2.0
 Docs: (doc function-name-here)
       (find-doc "part-of-name-here")
 Source: (source function-name-here)
 Exit: Control+D or :cljs/quit or exit

cljs.user=> (let [user {:user/keys {:id 56}}
       #_=>       company {:company/keys {:id 28}}
       #_=>       {{user-id :id} :user/keys} user
       #_=> {{id :id} :company/keys} company]
       #_=>   [user-id id])
[56 28]

eoliphant14:04:45
replied to a thread:Hi, I have a cljs macro question/issue. I have some macro code that generates re-frame subscriptions based on a mini-DSL. I think Iā€™ve followed all the guidance for them. The macro is in a separate namespace, and in a .cljc, etc. I need to have the macro execute when the file loads so I have an inline call to it in the file where itā€™s included. It seemed to be working at one point but now it doesnā€™t seem to get called at all, and I canā€™t figure out why ;;; macros.cljc (defn gen-calc-sub [calcs [field [calc args]]] (print "creating sub for: " field) `(rf/reg-sub ~field (fn [_# _#] ~(mapv (fn [arg] (if (calcs arg) `(rf/subscribe [~arg]) `(rf/subscribe [:form/cur-field ~arg]))) args)) (fn [args#] (calc/run-calc ~calc args#)))) (defmacro gen-calc-subs [calcs] (print "creating subs for: " calcs) (let [calc-fields (set (map first calcs)) ;for quick membership test ] `(do ~@(map #(gen-calc-sub calc-fields %) calcs)))) ;;; file where it's used (ns f.....events-subs ... (:require-macros [....macros :as fm])) ... ;;; invocation (fm/gen-calc-subs {:sf425/cash-on-hand [:calc/subtract [:sf425/cash-received :sf425/cash-disbursed]] }) Iā€™ve done something similar in Clojure with no problems, so I guess Iā€™m missing some other CLJS specific thing. Iā€™ve checked the expansion in the CLJS and CLJ repls, and Iā€™m getting the expected output in both places

oh and there was another bizarre thing I didnā€™t mention. There was a weird lapse in referential transparency lol The macro took a map that was basically a little calculation DSL for form fields {:my-field [:sum [:field :field2]) so I had

(def calcs {:my-field [:sum [:field :field2]))
(re-frame-sub-generator-macro calcs)
was blowing up and saying calcs wasnā€™t defined šŸ˜ž I didnā€™t even dig into that and just put the map directly into the call