This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-06-08
Channels
- # babashka (9)
- # beginners (43)
- # biff (4)
- # calva (11)
- # cider (6)
- # clerk (1)
- # clj-kondo (4)
- # cljs-dev (6)
- # clojure (82)
- # clojure-berlin (1)
- # clojure-europe (42)
- # clojure-nl (1)
- # clojure-norway (182)
- # clojure-quebec (1)
- # clojure-uk (19)
- # clojurescript (6)
- # datahike (1)
- # emacs (30)
- # fulcro (5)
- # honeysql (6)
- # hyperfiddle (12)
- # lambdaisland (8)
- # malli (11)
- # off-topic (36)
- # pathom (26)
- # pedestal (1)
- # portal (25)
- # practicalli (1)
- # rdf (29)
- # re-frame (17)
- # reitit (1)
- # releases (1)
- # sci (37)
- # shadow-cljs (15)
- # vim (10)
- # xtdb (13)
@borkdude or anyone — I am working on a portal viewer that’s cljs evaluated via sci.
this require fails with “could not find namespace: goog.object”
https://github.com/mentat-collective/Leva.cljs/blob/main/src/leva/schema.cljs#L6
is there a replacement I can use, for goog.object/set
with a string key (not known at compile time) and value?
I would use assoc
if I were in cljs, but I’m mutating a JS object here
okay nice, whenever I do that someone pops up and says “only use aset with arrays!!”
borkdude approved, done! and yes this is clearly the right way, thanks!
@borkdude any preferred substitute for this-as
in sci?
this is from portal, when I try to require a cljs file that uses this-as
inside of it cc @U1G869VNV
https://github.com/mentat-collective/JSXGraph.cljs/blob/main/src/jsxgraph/core.cljs#L50-L52
@borkdude oh whoops I assumed your link was a docstring for this-as, instead it’s a workaround -
yes, this-as is only possible in a compiled language, in an interpreter I haven't found a way
there must be some other way to get the “context” for the event, I’ll read up
so it’s calling the handler with https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
I think I can get around this
nice :)
user=> (def thisAs (new js/Function "return this"))
#'user/thisAs
user=> (def obj #js {:fn thisAs})
#'user/obj
user=> (.fn obj)
#js {:fn #object[anonymous]}
I guess I could use that in SCI and track where the this-as local is used and insert a call to this special thisAs function instead
another alternative would be to wrap every function body in SCI with this-as in the host, but not sure if there would be a performance impact
Although, I guess this-as is a macros
(defmacro this-as
"Defines a scope where JavaScript's implit this is bound to the name provided."
[name & body]
`(let [~name (~'js* "this")]
~@body))
I updated the issue here, I think the js/Function thing is interesting, I'll try it when I have some time after the bb conf. Or perhaps @U5H74UNSF feels like hacking on this with me tomorrow ;) https://github.com/babashka/sci/issues/564
I’ve got it worked out for now, turns out I could close over the object that was eventually passed to my callback as this
, since I had just created it when I registered the callback
I tried a couple of things, but I'm still getting the global object in SCI for "this":
cljs.user=> (def obj (sci/eval-form (sci/init {:bindings {'thisAs (js/Function. "return this") }}) (do (def obj (clj->js {:f (fn [] (thisAs))})) (.f obj))))
#'cljs.user/obj
cljs.user=> (identical? obj js/globalThis)
true
ah wait:
cljs.user=> (sci/eval-form (sci/init {:bindings {'thisAs (js/Function. "return this") }}) (do (def obj (clj->js {:fn thisAs})) (.fn obj)))
#js {:fn #object[anonymous]}
I wish I could make this work:
(sci/eval-form (sci/init {:bindings {'thisAs (js/Function. "return this") }}) (do (def obj (clj->js {:fn (fn [] (thisAs))})) (.fn obj)))
since then I could probably also make this-as
work, but it seems thisAs
needs to be directly on the object@U017QJZ9M7W unchecked-set/get
may be more "idiomatic" than aget/set, even though the docs say it's internal ;)