This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-04-02
Channels
- # asami (5)
- # aws (16)
- # babashka (41)
- # babashka-sci-dev (44)
- # beginners (157)
- # biff (3)
- # cider (1)
- # clj-commons (1)
- # cljdoc (22)
- # clojure (7)
- # clojure-dev (5)
- # clojure-europe (13)
- # clojure-nl (1)
- # clojure-uk (1)
- # clojurescript (17)
- # core-typed (13)
- # cursive (14)
- # datascript (10)
- # events (1)
- # fulcro (2)
- # graalvm (2)
- # gratitude (1)
- # jobs (3)
- # lsp (229)
- # pathom (2)
- # pedestal (3)
- # portal (53)
- # re-frame (7)
- # remote-jobs (1)
- # spacemacs (14)
- # xtdb (6)
I'm experimenting with web workers in Clojurescript (through shadow-cljs). Is there a simple way to send cljs data structures back and forth from the web worker?
I've tried cljs-bean
but that doesn't preserve :val
in {:key :val}
(turned into "val"
)
Got it working using com.cognitect/transit-cljs
Next question: Is it possible to instruct a web worker to call a specific function? I've tried to send this map to the web worker {:fn 'my.ns.my-fn}
.
The web worker receives the map as m
. I check (:fn m)
and it returns my-ns.my-fn
But nothing happens when I call ((:fn m))
Do I need to resolve the symbol some way?
Instead of dynamically resolving arbitrary functions, if the set of functions that you'd want to call in a web worker is known then just create a registry of them.
Yes, that was my first idea. But it felt nicer to send the symbol name. But i guess it won’t survive advanced compilation anyway?
You can still send the symbol name, and make that registry index functions by the symbols. :) If there are a lot of such functions, you can replace defn
with your own macro that automatically populate the registry.
> But i guess it won’t survive advanced compilation anyway?
That's my guess but I haven't checked. I.e. the symbols themselves will definitely survive but resolve
might not work - because the original namespace and function name were renamed.
> You can still send the symbol name, and make that registry index functions by the symbols. :) If there are a lot of such functions, you can replace defn
with your own macro that automatically populate the registry.
Oh yeah, I didn't think of that. Great solution, thanks!
I think you should use https://clojuredocs.org/clojure.core/intern