is there async/await syntax in cljs? im trying to use promises but having trouble when using them with threadfirst macro
there is also official guide on promises here: https://clojurescript.org/guides/promise-interop + some libraries like promesa: https://funcool.github.io/promesa/latest/user-guide.html (I never tried any promise library)
What issues are you running into with the thread first macro?
CLJS does not have async/await natively, but there are some libraries that implement similar to it using macros
You can use cljs.core.async.interop/<p!
to turn a promise into a value within a go
block, at which point you can use the normal core.async
tools.
Can anyone point me in the direction of the best way to handle client side redirects using reframe with reitit front end with luminus? I've been trying this:
(rf/reg-event-fx
:redirect
(fn [_ [_ path]]
(set! (.-hash js/window.location) path)))
But i get the error:
No protocol method IMap.-dissoc defined for type string: /
Is there a more idiomatic way of handling this?
EDIT: For reference of anyone searching this slack history for a similar issue, i found this to be the solution when using the default luminus template for re-frame, where the :common/navigate
event comes from:
(rf/reg-event-fx
:redirect
(fn [_ [_ route-name]]
(let [route (reitit/match-by-name router route-name)]
{:dispatch [:common/navigate route]})))
With the event going on the same ns as the router to avoid circular dependenciesReitit should have a function for that.
Don't call side-effecting functions (like set!
) in re-frame event handlers. Use effects for that.
Your solution is not really a solution without the definition of the :common/navigate
event because it's the one that does all the work.
Sorry that's default in the luminus template im using, will update to mention that
I need to interop with a JS lib that makes heavy use of class
and extend
. Are there any libs/strategies that make this seamless in cljs? Anything I should watch out for?
The library’s primary means of extension is by subclassing the major classes. I should be able to get away with using Object.assign
from cljs perhaps?
This is a bit tricky, actually. There are some ways to “fake” it but “class ... extends ...” actually does some special things at runtime in modern browsers
Here’s what I did in a lib I maintain https://github.com/lilactown/helix/blob/master/src/helix/impl/classes.js
Basically wrote some JS that I could include in my CLJS lib which handles the class extension
The downside is that this seems to require emitting ES6+ code, at least at development time
if you arte using shadow-cljs you can have real class with real extends
example use here https://github.com/thheller/shadow-experiments/blob/master/src/main/shadow/experiments/grove/components.cljs#L151
extends is is just (extends FooBar)
same level of (field ...)
this emits real JS class
. got tired of working around that myself 😉
besides field, constructor, extends it acts like deftype
with protocols
inside constructor
you can call (super "foo")
. must be before any field is accessed
So, is there any good way to change the *target*
value in your code?
When I try doing:
(binding [*target* "default"]
...)
In my code, it works fine unless I do :optimizations :advanced
, in which case I get the error:
SEVERE: /home/leif/src/interactive-syntax/target/public/cljs-out/prod/interactive_syntax/core.js:66: ERROR - [JSC_INVALID_DEFINE_INIT_ERROR] illegal initialization of @define variable cljs.core._STAR_target_STAR_
}finally {(cljs.core._STAR_target_STAR_ = _STAR_target_STAR__orig_val__25665);
Have you considered upstreaming this?
well technically it could just be a library since it doesn't use anything shadow-cljs specific. just to lazy to make a lib for one ns/macro
@leif *target*
is a compile time constant. you are not supposed to dynamically adjust it which is why you get that error.
it is controlled by the :target
compiler setting
I see. so you can't change the target when you're evaling some code?
What are you hoping to accomplish?
I’d love to use it in helix, but don’t want the lib to depend on shadow-cljs
Suppose I could copy paste
feel free to copy for now. I have a bunch of other stuff I eventually want to make standalone and not part of shadow-cljs so one day I'll definitely do it. just low priority for now.
@dpsutton I'm trying to use eval
in a webpage built with :target :bundle
Which worked in clojurescript 1.10.741, but not in 1.10.773
(Note that I don't want the eval itself to be the bundle target, I would like that to be default
.)
The defclass
macro looks really nice, thank you for posting that