Fork me on GitHub
#clojurescript
<
2017-07-10
>
souenzzo00:07:45

Also no support to #js {5 55}... Is it intentional?

tjscollins00:07:34

Is there a way to get response status out of cljs-ajax? I can't figure out how to get anything more than [[ok response]] which is just a true/false flag. The response object is just the body, not the whole response. Anyway to get the whole response instead?

souenzzo01:07:58

tjscollins:

(-> (ajax/json-response-format)
      (update :read (fn [original-handler]
                      (fn [response-obj]
                        {:headers #?(:cljs (js->clj (.getResponseHeaders x))
                                     :clj  (reduce (fn [headers header]
                                                     (assoc headers (.getName header) (.getValue header))) {} (-> x :response .getAllHeaders)))
                         :body    (original-handler response-obj)
                         :status  #?(:cljs (.getStatus response-obj)
                                     :clj  (-> response-obj :response .getStatusLine .getStatusCode))}))))

souenzzo01:07:15

Not sure about clj part, but cljs is working šŸ˜‰

souenzzo01:07:38

I just about to propose this option to ajax-cljs

tjscollins01:07:31

Thanks. This is a better option than the other one I just found, which was :response-format {:read identity :description "raw"}, which works, but then you still have to use .getStatus, etc. on the result.

Lone Ranger01:07:34

anyone here use sente for websockets? ()

Lone Ranger01:07:57

I have a security concern but I'm too much of a clojure noob to entirely understand this aspect of it

Lone Ranger01:07:23

;; server
(defmethod -event-msg-handler
  :default ; Default/fallback case (no other matching handler)
  [{:as ev-msg :keys [event id ?data ring-req ?reply-fn send-fn]}]
  (let [session (:session ring-req)
        uid     (:uid     session)]
    (debugf "Unhandled event0: %s" event)
    (when ?reply-fn
      (?reply-fn {:umatched-event-as-echoed-from-from-server event}))))  ;; <---- this is the part that concerns me (see below)

;; client
(when-let [target-el (.getElementById js/document "btn2")]
  (.addEventListener target-el "click"
    (fn [ev]
      (->output! "Button 2 was clicked (will receive reply from server)")
      (chsk-send!
        [:example/button2 {:had-a-callback? "indeed"}]
        5000
        (fn [cb-reply]   ;; <---- it looks like this function is being sent to server to be executed as ?reply-fn
          (->output! "Callback reply: %s" cb-reply))))))

Lone Ranger01:07:04

I'm sure there's something I don't understand but it looks as if arbitrary code is being sent to be executed on the server...

euccastro01:07:06

@goomba: the code is not sent for execution on the server. all the ?reply-fn will do when called is send its arguments to the client for execution there

Lone Ranger01:07:58

:thinking_face:

euccastro01:07:47

you can try writing a callback that is not even valid clojure (only valid clojurescript)

Lone Ranger01:07:01

ok so in this case

euccastro01:07:23

or writing one that modifies the DOM, prints to the browser console, or does anything else that doesn't make sense in the server side

euccastro01:07:36

that should crash if the server tried to execute it

Lone Ranger01:07:12

?reply-fn is sending back to the client {:umatched-event-as-echoed-from-from-server event} which is executed by the anonymous function

(fn [cb-reply]
          (->output! "Callback reply: %s" cb-reply))
?

Lone Ranger01:07:31

okay... that makes sense with the behavior I'm seeing, thank you

euccastro01:07:37

exactly, and cb-reply is the dict that the server has sent

euccastro01:07:03

(the mapping even)

Lone Ranger01:07:14

I see... THANK you

euccastro01:07:49

you're welcome. sente is awesome!

Lone Ranger01:07:59

I was gonna šŸ˜­ if I couldn't use this! Yes, sente is awesome!!!

anmonteiro04:07:41

@madvas just looks like the module is written in a format that Closure canā€™t understand

madvas04:07:13

yeah, thought itā€™ll something like that šŸ˜• nevermind, thanks for reply šŸ˜‰

qqq04:07:34

what is the cljs way to have a function that is: 1. called once every 100 ms 2. across different reloads (via figwheel / boot-reload), don't have the function end up being called too much

qqq04:07:52

i.e. avoid "every reload = additional timer callikng the function once every 100 ms"

anmonteiro04:07:42

@qqq

(defonce interval
  (js/setInterval (fn [] ...) 100))

qqq04:07:15

okay, so I have an XY problem

qqq04:07:29

suppose we have: (defn reactive-timer-new [] ... )

qqq04:07:49

it'll look something like:

(defn r-timer-new []
  (let [r (r-atom (js/Date.))
        update (fn []
                 (reset! r (js/Date.)))]
    (js/setInterval update 100)))

qqq04:07:11

now, when I no loger need this timer reactive atom

qqq04:07:16

(r-atom is reagent/atom)

qqq04:07:29

the problem is: (1) this can't get gc-ed since there is a js/setInterval referencing it)

qqq04:07:39

is there a way to get around this problem?

anmonteiro04:07:56

js/clearInterval?

qqq04:07:01

[it's a bit different from the original qu3estin . asked; but across reloads, I may end up creating ew timers ]

qqq05:07:48

actually, you win, I'm going to defonce one global "now" reagent/atom

skammer11:07:03

Is there a way to do native await in clojurescript?

Roman Liutikov11:07:58

@rauh @skapadia afaik they use core.async under the hood, one should be aware that it produces a lot of code

skammer11:07:48

promesa requires you to do p/await inside p/alet, which, in some sense, defeats itā€™s purpose šŸ˜ž

Roman Liutikov11:07:58

@skammer you could use js* macro to implement something like this i cljs

(defmacro async [body]
  `(js* "(async function() {~{}})();" ~body))

(defmacro await [expr]
  `(js* "await ~{}" ~expr))

(async
  (let [a (await 1)
        b (await 2)]
    (+ a b)))

Roman Liutikov11:07:17

but Iā€™m not sure if this is safe

Roman Liutikov12:07:22

@skammer yeah, this wouldnā€™t JUST work. for example let bindings is being wrapped into IIFE which also have to by async function in order to be able to use await inside of it

Roman Liutikov12:07:24

in any case, unless you are targeting browsers that have native support for async/await, thereā€™s not much benefit since both core.async and transpiled async/await produce a lot of code

skammer12:07:08

Iā€™m targeting nodejs, so even large amount of generated code isnā€™t really an issue here

rauh12:07:22

@skammer In that case I'd def take another look at promesa, bluebird is faster than native async/await.

shidima_13:07:23

I'm trying to flip the state of an item in an atom, but I cant get it to work

(defn todo-item [{:keys [title done]}]
  [:li (done? done) " - " title
   [:a {:on-click #(reset! not :done)} done]])

shidima_13:07:59

I think I'm not using the reset function correctly

ruben13:07:18

@shidima_ Use

swap!
instead of
reset!
to change one key of the map. Assuming that the atom is called state use
(swap! state #(assoc % :done (not (:done %))))

shidima_13:07:25

thanks @ruben will check it out

pesterhazy13:07:16

or more concisely (swap! state update :done not)

yvon14:07:04

Hello developers. I'm new to the ClojureScript language, and it's a pleasure to be yours.

dzita15:07:57

I am currently learning to create mobile apps using clojurescript, react-native and I would add a toobar in my app. help please

yvon15:07:17

335/5000 I would like to create a registration form with

ClojureScript
using
React-Native
(and
Reagent
thereafter). And I would like to include the different components with MaterialDesign, and I do not know how to do it. Do you have any suggestions? I have already found on the Internet, but I did not have much.

yvon15:07:30

I just want to create a simple registration form, using ClojureScript MaterialDesign ReactNative.

yvon15:07:36

Thanks in advance for your feedback.

yvon15:07:55

Name

Your Name
Email
Your Email
Password
Enter your password
Send Cancel

dnolen15:07:14

@dzita there is a #cljsrn channel for React Native questions

dnolen15:07:38

@yvon you also probably want to checkout the React Native channel

fedreg15:07:20

Hi all, How can I pass a function as an argument as follows: I need a button component with the ability to add or subtract from a value so would like to have (defn MyBtn [operator] [:button {on-click (operator valA valB)} str opertor]) and call it as [MyBtn +] or [MyBtn -] etc. Hope that makes sense! Thx!

dnolen16:07:20

@fedreg {:on-click (fn [event] (operator valA valB)}} is what you want for the handler

fedreg16:07:52

@dnolen Thx! My example wasnā€™t the best. I had the fn [e] part but was calling the operator incorrectly within my swap! function. Got it though. Thx again!

dnolen17:07:02

re: async/await support, Iā€™m curious as to why people want this ā€¦ why not just patch Promises to be channels?

pesterhazy17:07:19

one reason may be to be able to translate async/await library examples to clojurescript: https://github.com/zeit/micro#async--await

devth17:07:20

is there a js to cljs translation tool somewhere?

pesterhazy17:07:38

@devth I wanted that many times, even a stupid one

devth17:07:44

yeah. šŸ˜•

pesterhazy17:07:04

i guess all the mutability in js makes that hard

anmonteiro17:07:52

shouldnā€™t be hard to print a JavaScript AST to ClojureScript

anmonteiro17:07:58

but.. ^ famous last words, right?

devth17:07:45

often need to grab a snippet from somewhere and translate. e.g. https://github.com/GoogleCloudPlatform/stackdriver-errors-js#initialization

mccraigmccraig17:07:16

this might help with translating js async/await examples - http://funcool.github.io/promesa/latest/#async-await-syntax

pesterhazy17:07:08

would be a great web service, like http://htmltohiccup.herokuapp.com/

jhorwitz18:07:06

Any reviews for this book? Half off for DRM day and just wondering if anyone has experience with it https://www.packtpub.com/web-development/learning-clojurescript

benbot19:07:00

Hey everyone, Iā€™m trying to follow along with the modern-cljs tutorials but Iā€™m running into issues with boot-cljs-repl

boot.user=> (start-repl)
<< started Weasel server on ws://127.0.0.1:50549 >>
<< waiting for client to connect ... Connection is 
Writing boot_cljs_repl.cljs...
Iā€™m trying to connect to the nrepl server but the boot repl is just hanging here. Any ideas as to whatā€™s going on?

pesterhazy20:07:25

did you open the page in the browser?

pesterhazy20:07:35

do you see any errors in the dev console?

benbot20:07:52

none, it prints out ā€œHello, World!ā€ just like it should

benbot20:07:37

It also shows that Connection is ws://... instantly even if i donā€™t have localhost:3000 open

pesterhazy20:07:11

can you verify if the browser connects via websocket?

benbot20:07:52

I donā€™t think it isā€¦ i see no indication that it is other than the repl output

pesterhazy20:07:12

you may need to add code to that effect, maybe you skipped over that part of the tutorial?

pesterhazy20:07:21

ah wait boot-cljs ads that

pesterhazy20:07:28

try shift-reloading the browser

benbot20:07:45

that did it

benbot20:07:21

I guess it just cached the old version?

benbot20:07:33

anyway thanks a bunch @pesterhazy

pesterhazy20:07:09

@benbot you probably use Chrome? Then you ran into this recent change in how Chrome caches javascript files when you click Reload: https://blog.chromium.org/2017/01/reload-reloaded-faster-and-leaner-page_26.html

benbot20:07:59

> relatively minor change

benbot20:07:08

good to know šŸ™‚

pesterhazy20:07:52

Chrome essentially caches js files served without Cache headers forever, even if you click reload.

pesterhazy20:07:49

Reloading is faster if you don't actually reload anything.

jcthalys20:07:37

Hi everyone, I have a question and I would like to know if anyone here can help me. What is the best way to via spec using date and time fields.. Iā€™m using this way:

(s/def :model.shift/initial-date #(instance? goog.date.UtcDateTime %))
(s/def :model.shift/start-time #(instance? goog.date.DateTime %))
(s/def :model.shift/end-time #(instance? goog.date.DateTime %))
And how could I use exercise with this fields?

pcj20:07:44

So I am new to macros and found a nice use case for one, however, I don't think it's quite right. My goal is to be able thread js/requestAnimationFrame through a list of functions. This is my macro:

(defmacro do-raf
[& functions]
  (list (reduce (fn [val# item#]
                  (list `fn `[] item# (list `js/requestAnimationFrame val#)))
                `(fn [])
                (reverse functions))))
This is what a macroexpand looks like
(macroexpand '(do-raf
                                       (aset _element "style" "opacity" 0)
                                       (add-class! _element "set-html-transition")
                                       (aset _element "style" "opacity" 1)))
((clojure.core/fn []
            (aset _element "style" "opacity" 0) 
            (js/requestAnimationFrame 
             (clojure.core/fn [] 
               (add-class! _element "set-html-transition") 
               (js/requestAnimationFrame 
                (clojure.core/fn [] 
                  (aset _element "style" "opacity" 1) 
                  (js/requestAnimationFrame (clojure.core/fn []))))))))
When using the macro itself, it doesn't transition correctly. But, when I copy and paste the code generated by macroexpand it works as intended.

roklenarcic20:07:47

isn't there an extra parenthesis around the whole thing?

pcj20:07:24

No. The reduce produces a fn so I have to call it.

pcj20:07:53

Oh so I figured it out... Had to use the correct syntax for macros in a namespace desclaration

roklenarcic20:07:54

man I have problems and I've tried slack and IRC and nobody even answers ever

roklenarcic21:07:47

I have an om.next app going and I am trying to introduce some ready-made components, namely bootstrap by am using racehub/om-bootstrap

roklenarcic21:07:02

and as soon as I introduce that, it breaks the whole project

roklenarcic21:07:30

I've tried #clojurescript IRC, #om.next slack, #om slack and nothing

roklenarcic21:07:35

nobody speaks for hours

roklenarcic21:07:13

when I go to #rust IRC I get at least an attempt at answer in 5 minutes

pcj21:07:08

If I had some experience in om.next I'd help you ;c

roklenarcic21:07:07

Well I tried adding dependecy [racehub/om-bootstrap "0.6.1"]

roklenarcic21:07:33

and then I replaced om.dom/button with om-bootstrap.button/button

roklenarcic21:07:25

and I get the following console errors: Uncaught TypeError: Cannot read property 'prototype' of undefined

roklenarcic21:07:32

at core.cljs and at schema.cljs and at mixins.js and at util.cljs and at button.js

roklenarcic21:07:42

5 files, same error

roklenarcic21:07:17

just wondering if anyone has any om.next project using om-bootstrap on github to demonstrate proper use

dnolen21:07:41

First sneak preview post on the next major release - https://clojurescript.org/news/2017-07-10-code-splitting

roklenarcic21:07:04

is there some sort of schemata + cljs problem going on that I'm not aware of

petterik21:07:20

@roklenarcic I have no experience with om-bootstrap myself, but I find github search to be quite good to find examples on how to use something. See: https://github.com/search?l=Clojure&amp;q=racehub%2Fom-bootstrap&amp;type=Code&amp;utf8=āœ“

roklenarcic21:07:05

0 hits on code?