This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-03-30
Channels
- # babashka (1)
- # beginners (86)
- # boot (5)
- # calva (21)
- # cider (26)
- # clj-kondo (10)
- # cljs-dev (5)
- # cljsrn (3)
- # clojure (181)
- # clojure-europe (22)
- # clojure-germany (11)
- # clojure-italy (3)
- # clojure-nl (7)
- # clojure-spec (6)
- # clojure-sweden (6)
- # clojure-uk (65)
- # clojuredesign-podcast (1)
- # clojurescript (71)
- # core-logic (2)
- # cryogen (15)
- # data-science (9)
- # datomic (7)
- # duct (4)
- # emacs (4)
- # events (1)
- # exercism (1)
- # fulcro (136)
- # funcool (1)
- # joker (6)
- # kaocha (3)
- # lambdaisland (28)
- # lumo (3)
- # malli (5)
- # mount (1)
- # off-topic (13)
- # re-frame (14)
- # ring (10)
- # shadow-cljs (20)
- # sql (5)
- # tools-deps (5)
- # tree-sitter (1)
- # uncomplicate (9)
Anyone have any idea why I’m not able to access my namespaces in my CIDER nrepl session into my shadow-cljs project
defmethod
ends up as a deftype with a method table as an atom. Is it gross to (keys @(.-method-table my-method))
?
isn't that methods
?
has anyone used reitit routing library on the frontend
Hi guys, what's the best way to handle stripe payments in the client side in a re-agent app?
Like here, https://stripe.com/docs/payments/accept-a-payment-charges, there's a way to put a script tag in the html
and then do
var stripe = Stripe('pk_test_sadljfdslafjsdalfjsdljfkjls');
var elements = stripe.elements();
in js. How to replicate this in clojurescript?
When I do (.. Stripe 'pk_test_sadljfdslafjsdalfjsdljfkjls'), I get that Stripe isn't defined.You can try:
(Stripe. "pk_test_sadljfdslafjsdalfjsdljfkjls")
How do you require Stripe ?
You can try:
(Stripe. "pk_test_sadljfdslafjsdalfjsdljfkjls")
“Undeclared variable” sounds like it isn’t loading the js for Stripe at all, maybe you need to include a <script> tag in your enclosing HTML document? Or possibly use shadow-cljs.
there are many ways. you can try using cljsjs:
https://github.com/cljsjs/packages/tree/master/stripe
which involves adding a dependency in your deps.edn to cljsjs/stripe
and then requiring it like:
[stripe :refer [Stripe]]
(I think that’s the right require, not sure)I don't know how to add a dependency in a deps.edn. I don't have that file in my project
FWIW, referring to global JS variables requires you to add js/
at the beginning of the symbol.
js/window
, js/document
, etc.
but in this case, it’s better to have the externs since it will help prevent errors when doing advanced optimizations
@lilactown I required it like so in my project.clj [cljsjs/stripe "3.0-0"]. I get "No such namespace: stripe, could not locate stripe.cljs, stripe.cljc, or JavaScript source providing "stripe"", where I used it [stripe :refer [Stripe]]
I’m not sure about the require, it’s hard to tell from cljsjs what the correct namespace is for externs
I have a general question. How to import any js library in a clojurescript project (Figwheel) without using cljsjs? Like is there something as simple as doing npm install --save whatever?
i think this is where shadow-cljs shines and actually fulfill's Clojure's promise of easy interop with the host language ecosystem
how reliable is "externs inference" ? does it requires manual intervention occasionally?
I'm okay with using js/Stripe which works. However, I have a problem with js promises: I'm trying to convert this js code:
stripe.createToken(card).then(function(result) {
if (result.error) {
// Inform the customer that there was an error.
var errorElement = document.getElementById('card-errors');
errorElement.textContent = result.error.message;
} else {
// Send the token to your server.
stripeTokenHandler(result.token);
}
And I have the following:
(go
(let [result (<!
(.createToken stripe @(subscribe [:card-element]))
)]
(prn "result is" result)
;; (if (.-error result)
;; (.textContent (js/document.getElementById "card-errors") (.-message .-error result))
;; (prn "response is" (js/stripeTokenHandler (.-token result)))
;; )
)
)
But I get the error:you're calling <!
on a promise. .createToken
returns a promise not a channel that you can take from
Is it possible to make a ratio or bigdec type as a library in cljs that plays nice with the rest of the number system (ie all arithmetic and comparisons work with regular numbers)?
Only if you use some custom functions instead of the built-in ones like +
, -
, and so on, and use them everywhere. Which means that this approach doesn't really scale beyond your own application code.
Thought this would make for a convenient macro for this small side project. Howerver I can't get it to work. I get an Can't use qualified name as parameter
error due to resolve and reject being qualified in the ns.
(defmacro promise
"
Example:
(promise (resolve 5))
(promise (reject (js/Error. \"oops\")))
"
[body]
`(js/Promise. (fn [resolve reject] ~body)))
Are resolve
and reject
functions or just indicators that the promise is being resolved/rejected?
Pretty sure resolve and reject are functions
The equivalent JS of the first example:
new Promise(function (resolve, reject) {
resolve(5);
});
Wouldn’t you want (fn [resolve & reject] ~body)
since you wouldn’t be resolving and rejecting a the same time? (acknowledging this is unrelated to your original question)
The default JS Promise constructor always provides a resolve and reject args to the function.
(println (macroexpand '(p/promise
(resolve 5))))
;; =>
(new js/Promise
(cljs.core/fn [cljs.core/resolve project.promise/reject]
(resolve 5)))
https://stackoverflow.com/questions/40270625/when-to-use-some-symbol-in-clojure-macro
Thanks! Just found the same solution recommended in clojuredocs defmacro examples
(defmacro promise
"
Example:
(promise (resolve 5))
(promise (reject (js/Error. \"oops\")))
"
[body]
`(js/Promise. (fn [~'resolve ~'reject] ~body)))

what is the state-of-the-art idiomatic way to work with JS Promises?
(somehow) convert them into core.async Promise Channels?
[funcool/promesa "4.0.0"]
?
Plain ol' interop?
I like to convert them to core async channels using https://github.com/wilkerlucio/wsscode-async
thanks will look at that
I wouldn’t use core.async just for promises. most of my apps haven’t used core.async so I just use interop
I agree that interop is fine if you're not using core async elsewhere in the app 👍
here's a nice example I saved from awhile back https://gist.github.com/porkostomus/579414ee10e62817bfbbaeafc18f549c
ooh yeah that looks pretty fluent
thats very helpful
thanks guys
I am using core.async anyway, for websocket comms
Started drafting some macros over the weekend to smooth out interop a bit for a project using cljs + puppeteer. Came up with these, which may not be quite as helpful for your project but I'm interested in feedback if anything here is useful or if it only mirrors someone elese's work. https://gist.github.com/eccentric-j/1d84d332a8afc9dcc1dbb48309811407
I use a macro that converts promises to channels, e.g.:
(go (prn (js<! (some-promise))))
https://github.com/jacobobryant/trident/blob/master/src/trident/util.cljc#L111I quite like kitchen-async, https://github.com/athos/kitchen-async
Wow that's really slick! I was thinking about how to make a macro like p/let and p/try but athos clearly beat me to it 😄
I need to give certain input elements a datamask depending on the type of class it has. I am using Reagent and I am wondering how could I do this if I am using this https://github.com/RobinHerbots/Inputmask. Is there a way to apply this while creating the input component? Currently the way it is being done is calling
(-> (js/jQuery ".hasDatePicker") (.inputmask "99/99/9999" (clj->js {:clearIncomplete true})))
on :component-did-update
which is on a component that wraps a whole form element, that contains all the inputs. This doesn't seem to be the proper way of doing it.