clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
tvaughan 2020-12-04T12:23:08.124700Z

Let's say, hypothetically, I have to ping a REST API endpoint, and then wait for a message over a websocket. What's the preferred method for wrapping these up into one synchronous function? For example, do-thing should ping the endpoint and not return until the websocket message is received. I could wait on a semaphore or use a deferred promise, I think. Any recommendations?

p-himik 2020-12-04T12:25:40.124800Z

I don't think you can do that - WebSocket API is async, you cannot make it sync.

tvaughan 2020-12-04T12:28:45.125Z

I'm not trying to make it synchronous. I'm trying to wrap a couple of asynchronous operations into one function that doesn't return until everything has completed.

tvaughan 2020-12-04T12:31:36.125300Z

Unfortunately <!! and >!! aren't available in clojurescript

p-himik 2020-12-04T12:32:10.125500Z

If we restrict ourselves to the JS world, then that function: - has to be marked as async - has to await on the relevant promise It will not "return" if it's used with another await (quotes because await and async are just a sugar anyway). But it will return a promise just fine if used without await. With that being said, CLJS has no support for async and await keywords. You cannot block in JS. There's just no way.

p-himik 2020-12-04T12:32:47.125700Z

If your code absolutely must rely on some blocking function then that code has to be restructured to support the scenario with WebSocket.

tvaughan 2020-12-04T12:38:07.125900Z

Locking won't block? https://cljs.github.io/api/cljs.core/locking

p-himik 2020-12-04T12:39:36.126100Z

As you can see from its source code, it does absolutely nothing but ignores x. I don't know why. My bet is that it's there simply to make it easier to write code that works with both CLJ and CJLS.

tvaughan 2020-12-04T12:40:56.126300Z

:man-facepalming:

p-himik 2020-12-04T12:41:54.126500Z

Imagine you have a language where you have just a single thread and an event queue. Nothing more. How would you block an execution there to wait for some event in the queue? The answer is, you cannot do that. And that's basically what JS is - a single-threaded VM with a queue.

tvaughan 2020-12-04T12:53:31.126900Z

I understand. Thanks

2020-12-04T15:33:14.134600Z

Hi, I have a weird behavior with javascript interop that I don’t understand I’m working with https://www.highcharts.com/ and https://github.com/highcharts/highcharts-react. I’m trying to get the property processedYData from inside one of the chart objects called Series. If I print in the console the series object the property appears

(.log js/console "series=" series)
This is part of what is shown in the chrome console:
series= 
x {hcEvents: {…}, axisTypes: Array(2), coll: "series", colorCounter: 0, remove: ƒ, …}
addPoint: λ[]
afterAnimate: λ[]
alignDataLabe
……
processedYData: (1052) [0, 0.95, 0.22, -1.36, 0.14, 0.53, 0.64, 0.97, 0.62, -0.91, -2.18, -1.67, -1.68, -0.77,
……
but when I log only the processedYData property it is empty
(.log js/console "processedYData=" (. series -processedYData))
chrome log
processedYData= []
What I’m doing wrong? What I’m missing? thanks in advance

p-himik 2020-12-04T15:37:13.134900Z

That's strange. Have you tried debugging it in Chrome? You can add (js-debugger) in your code - it will trigger a breakpoint if that code is executed while the DevTools panel is open.

2020-12-04T15:40:05.135100Z

I didn’t know about that fn (js-debugger) I will try, thanks

2020-12-04T16:17:31.136100Z

some times it appears some time it doesn’t or it’s outdated. I think I’m going to change my point of view

2020-12-04T16:18:33.136500Z

thanks @p-himik, you’re always helpful

👍 1
benny 2020-12-04T23:09:48.141Z

is there an equivalent of an uberjar that targets node? In the optimal case I hand a virgin node a single file and it runs my cljs script/daemon for me on the target device

benny 2020-12-05T10:47:25.144600Z

shadow-cljs with :target :node-script works for me in release mode