Fork me on GitHub
#clojurescript
<
2020-05-04
>
sergey.shvets00:05:36

you're right. I checked it on console.log and it works. My problem is not with apply but that javascript's function this isn't bound properly when I call it this way.

sergey.shvets00:05:10

I solved the problem with bind like this, but this feels very ugly. Is there a simpler way?

(let [command-fn (-> (gobj/get ref (name clause))
                     (.bind ref))]                           
    (apply command-fn args))

jeroenvandijk13:05:18

Hmm looks much better than my solution 🙂 https://github.com/borkdude/sci/blob/master/src/sci/impl/interop.cljc#L49-L60 Not sure if it is a similar problem, but apply didn’t work and I didn’t know about bind

sergey.shvets17:05:14

The code I shared above works for my example. But it seems to me that you're trying to invoke constructor for a new object, which shouldn't have this at all? .bind will only set "this" inside called function to the object you pass to .bind.

jeroenvandijk18:05:20

yeah true. I mostly wanted to show how ugly things can be become 🙈

sergey.shvets22:05:44

Haha, true as well. Especially with interop. Btw, I think that if you get a constructor function and bind it to cls that should make your implementation simpler. And help you get rid of 7 items limit for those psychos who do something like that. You don't want to mess around with those people 😄

😂 4
sergey.shvets22:05:25

(let [ctor (-> (.-apply constructor)
               (.bind constructor))
     args (js-object-array args)]
       (apply ctor args))

sergey.shvets01:05:12

That doesn't work. I spent an hour trying to find a solution to calling constructor with variable args and no luck

sergey.shvets01:05:36

function newCall(Cls) {
    return new (Function.prototype.bind.apply(Cls, arguments));
    // or even
    // return new (Cls.bind.apply(Cls, arguments));
    // if you know that Cls.bind has not been overwritten
}

sergey.shvets01:05:58

This works in js, but I can't figure out how to translate it to cljs due to arguments

jeroenvandijk08:05:11

Thanks for your suggestions @U4EFBUCUE. At the very least it sounds like there is room for improvement. I’ll try to look at it later and fix it in Sci. Would be nice to remove all these edge cases from Sci

lilactown01:05:11

you can also use (.apply fn ref args)

Roman Liutikov08:05:34

important note in case of JS .apply, args should be an array (iterables won't work) (.apply f context (into-array args))

☝️ 4
👍 4
jjttjj12:05:01

I created the channel #bootstrapped-cljs for bootstrapped/self-hosted cljs discussion if anyone wants to join

nick16:05:13

#mount Trying to understand why the old timer is not cleared using mount. Just tried to change the print format from print-stats-ver-n2.1 to print-stats-ver-n2.2 (ns fooapp.core (:require [fooapp.config :refer [env]] [fooapp.middleware :refer [wrap-defaults]] [fooapp.routes :refer [router]] [macchiato.server :as http] [macchiato.middleware.session.memory :as mem] [mount.core :as mount :refer [defstate]] [taoensso.timbre :refer-macros [log trace debug info warn error fatal]])) (defn print-stats-ver-2 [] (js/console.log "print-stats-ver-n2.2 called")) (defn start-timers [] (js/setInterval print-stats-ver-2 5000)) (defstate timer-watcher :start (do (println "Starting timer") (start-timers)) :stop (do (println "Stopping timer") (js/clearInterval timer-watcher))) (defn server [] (mount/start) (let [host (or (:host @env) "127.0.0.1") port (or (some-> @env :port js/parseInt) 3000)] (http/start {:handler (wrap-defaults router) :host host :port port :on-success #(info "fooapp started on" host ":" port)}))) Console log: > ("../95E9ED1.js") > Figwheel: loaded these files > ("../fooapp/core.js" "../oneclickpoll/app.js") > print-stats-ver-n2.1 called <= old one is still printing > print-stats-ver-n2.2 called > print-stats-ver-n2.1 called <= old one is still printing Anything that I missed?

lilactown17:05:50

do you need to deref timer-watcher inside the :stop expr?

nick17:05:47

well this is embarrassing but I'm still learning 🙂 Thank you @lilactown

lilactown17:05:34

sure thing! that’s not obvious, would be lovely if clearInterval threw an error if it was passed something other than a timer 😉

curlyfry19:05:42

Hi! Is there any way to tell cljs.test to dynamically include or exclude a test (given for example a string) when running cljs.test/run-tests, aside from using test-vars with explicit vars? I'm writing a test runner GUI that presents the tests in a web page, and I'm trying to add a filter input that would give you all the tests that have names that partially match the input.

noisesmith20:05:30

I hope there's a friendly way to do this, but if not: clojure.test finds tests by looking at all vars in a given ns (or all namespaces if none specified) that have a function under their :testmetadata. You could potentially do the same sort of search using all-ns and ns-publics then use the filter across the resulting vars

noisesmith20:05:59

this is all assuming that cljs is mostly compatible, so disregard if it's substantially different...

curlyfry20:05:06

Thanks! Pretty sure those aren't available in cljs at runtime, though. And I have no idea how to work this into a macro...

noisesmith20:05:10

@curlyfry your real answer: run-all-tests takes an optional regular expression argument

curlyfry20:05:30

Sadly only for the namespace!

curlyfry20:05:07

No var filtering from what I can see.

curlyfry20:05:44

To clarify: I need filtering on vars, not namespaces

phronmophobic20:05:42

it’s really good practice to look under the hood of the libraries and platforms we use. clojure itself is really interesting. to solve this problem, I would look out https://github.com/clojure/clojurescript/blob/master/src/main/cljs/cljs/test.cljc#L331 and use that a basis for doing what you need

curlyfry20:05:20

Sorry, I should have clarified that this is what I'm doing currently! I just struggle with applying all this macro magic to my problem (let alone get any of these ns listings to compile). But thank you for the nudge in the right direction 🙂

phronmophobic20:05:27

no problem. I realize that digging through a library you’re using isn’t a really great answer, but it looks like that might be the best option here

👍 4
curlyfry19:05:32

With cljs.test/report I can choose to implement my own reporting method given the test result, but I can't find a way to hook in to the actual running of the test