Fork me on GitHub
#clojurescript
<
2024-03-19
>
grounded_sage11:03:32

Has anyone created a watchable reference in a web worker. I essentially want to be able to run some compute on a web worker which puts results in an atom. Which is watched from the main thread.

p-himik11:03:57

Assuming you already have a worker set up:

(def worker-data (atom nil))

(set! the-worker -onmessage #(reset! worker-data (.-data ^js %)))

thheller11:03:39

you probably want to serialize stuff back and forth. doubt you are just gonna send js data

grounded_sage12:03:00

I looked at this more the ideal solution is transferring ownership of an ArrayBuffer if I go in this direction. My thoughts was actually if someone had wrapped SharedArrayBuffer with IWatachable or something.

thheller13:03:15

IMHO that is nonsense unless you actually just sharing binary data. CLJS datastructures are not binary data, so either way you are going to serialize them. if you serialize them you might as well just transfer a string, eg. edn or transit.

dominicm17:03:52

How do I import/require an enum? Particularly goog.crypt.base64.Alphabet?

dominicm17:03:17

Hmm, that hasn't worked...

dominicm17:03:22

Says it can't be found

1
thheller17:03:33

FWIW JS doesn't have enums. its just a javascript object, so the same interop rules like any other JS object apply

dominicm17:03:08

The required namespace "goog.crypt.base64.Alphabet" is not available, it was required by

dominicm17:03:14

#?(:cljs (:import [goog.crypt.base64 Alphabet]))

thheller17:03:23

(:require [goog.crypt.base64 :as b64]) b64/Alphabet.WEBSAFE

p-himik17:03:36

The error is because the enum isn't defined in its own file.

dominicm17:03:37

@U05224H0W I was worried that might not work across compilation styles 😄

dominicm17:03:44

ah, OK 😂

p-himik17:03:12

goog is inconsistent, yeah. :) Gotta be somewhat creative.

thheller17:03:29

imho :import makes little sense in CLJS. :require does everything you need

👍 1
dnolen18:03:50

:import was added a long time ago to solve a specific problem - when the namespace is a constructor

thheller18:03:52

but since we added using :as aliases as "vars" this became redundant no? ["some.thing" :as x] (x.) works now, same for closure lib

dnolen19:03:24

all stuff that came way later - there’s too little value in changing this kinds of really old decisions

dnolen19:03:04

pretty sure treating aliases as invokeable things also came way later

thheller19:03:14

I'm not saying to change it at all. just saying that :require now covers everything :import previously did, so it is no longer "required" to use

dnolen19:03:55

right I just mean :import does exist for a good reason even if it is historical

dnolen19:03:05

and this largely about aligning Clojure / ClojureScript

dnolen19:03:02

of course not meaning to make a pointless argument

dnolen19:03:27

it might have a been a bit clearer w/ “:require now does everything you need”

dominicm19:03:23

Is it worth updating the documentation around how to use the closure library if :require can now do it in place of :import?

1
hifumi12300:03:17

agree with dominic. there are cases I have used :import where :require is sufficient. itd be nice to reflect "modern clojurescript" in official documentation.

thheller08:03:17

note that there is technically nothing wrong with :import and it is fine to use. the only problem is that the closure library is somewhat inconsistent in how things are structured. so some things are actual "namespaces" while others are just "vars" (as in property of the namespace) or even properties of other vars. so while :import might work for some it won't for others. :require is a bit more forgiving that way, but still not immune either.