Fork me on GitHub
#clojurescript
<
2021-07-07
>
clj839401:07:48

how do I require an npm module that has a slash in it? Im trying to import and put quotes around the imported module name but it isn't working properly

clj839401:07:31

for example, if the nodejs require is:

require("hello/world")
im trying
(:require ["hello/world" :as hello-world])\

p-himik10:07:44

It might depend on the build tool that you're using. Shadow-cljs should handle it just fine. Also check what that module actually says in the "main" key in its package.json. If that doesn't provide any clues, you can always use a full path relative to node_modules.

clj839401:07:38

but its not working

zendevil.eth05:07:01

I have the following issue that I submitted on the reagent github, but haven’t heard back and don’t know the solution of! https://github.com/reagent-project/reagent/issues/543

thheller05:07:16

@ps probably [:> Auth0Provider ...] not [Auth0Provider ...]

zendevil.eth13:07:46

I’m implementing the following: https://github.com/arunoda/use-magic-link And so far I have this:

(:require ["use-magic-link" :default useMagicLink])

(defn login []
  (let [!email (atom nil)
        auth (useMagicLink "pk_live_23456787878579")]
    [:div
     (if (. auth -loggedIn)
       [:div {:on-click #(. auth logout)} "Logout"]
       [:div
        [:input {:on-change #(reset! !email (-> % .-target .-value))}]
        [:div {:on-click (fn [] (. auth login @!email))} "Login/Sign Up with email"]])]))
And I’m using it as [:f> login] in a parent component. But I’m getting this error:
core.cljs:36 Uncaught TypeError: module$node_modules$use_magic_link$dist$use_magic_link.default is not a function
    at cmp.humboi$core$login [as reagentRender] (core.cljs:36)
    at eval (component.cljs:86)
    at Object.reagent$impl$component$wrap_render [as wrap_render] (component.cljs:91)
    at Object.reagent$impl$component$do_render [as do_render] (component.cljs:117)
    at eval (component.cljs:73)
    at Object.reagent$ratom$in_context [as in_context] (ratom.cljs:44)
    at Object.reagent$ratom$deref_capture [as deref_capture] (ratom.cljs:57)
    at Object.reagent$ratom$run_in_reaction [as run_in_reaction] (ratom.cljs:539)
    at cmp.re_frisk$reagent$impl$component$wrap_funs_$_render [as render] (component.cljs:73)
    at finishClassComponent (react-dom.development.js:17486)

p-himik13:07:45

Check what useMagicLink is with js/console.log. Sometimes it's not what it's supposed to be.

zendevil.eth13:07:01

ha it’s undefined

p-himik13:07:54

Try using :as instead and see what it outputs then.

zendevil.eth13:07:08

yeah changing to as works, which seems like a #shadow-cljs bug

zendevil.eth15:07:54

I’m writing the first code block from here in cljs: https://www.npmjs.com/package/@magic-ext/oauth?activeTab=readme And I have the following:

(Magic. "pk_live_ldshfsjdfsfasdf"
                      #js {:extensions [(OAuthExtension.)]})
but this is giving me the following cryptic error:
Uncaught TypeError: Cannot read property 'compat' of null
    at checkExtensionCompat (sdk.js:21)
    at eval (sdk.js:68)
    at Array.forEach (<anonymous>)
    at SDKBase.prepareExtensions (sdk.js:67)
    at new SDKBase (sdk.js:107)
    at Function.humboi$core$login (core.cljs:37)
    at Function.eval [as cljs$core$IFn$_invoke$arity$2] (core.cljs:3925)
    at Function.eval [as cljs$core$IFn$_invoke$arity$2] (core.cljs:3961)
    at Object.reagent$impl$component$functional_wrap_render [as functional_wrap_render] (component.cljs:380)
    at Object.reagent$impl$component$functional_do_render [as functional_do_render] (component.cljs:393)
How to fix this?

henryw37415:07:32

the cljs looks correct I think. maybe you need to init thing first? e.g. const res = await magic.oauth.getRedirectResult();

p-himik15:07:38

#js reader tag is not recursive.

👍 2
zendevil.eth18:07:11

I have this piece of code, in which I’m using reset! to set the value of an atom:

(defn login []
  (let [
        magic (new-magic)
        !social-logged-in (atom nil)
        _ (go (js/console.log "is logged in " (<p! (.. magic -user (isLoggedIn))
                                                   )))
        _ (go (reset! !social-logged-in
                      (<p! (.. magic -user (isLoggedIn))
                           )))
        _ (js/console.log "social logged in " @!social-logged-in)
        
        ]
   ))
As you can see, there are two logs one that prints A
_ (go (js/console.log "is logged in " (<p! (.. magic -user (isLoggedIn))
                                                   )))
and the other that prints B:
_ (js/console.log "social logged in " @!social-logged-in)
Even though I’m setting the value of !social-logged-in, the values that A and B are printing aren’t the same. Why is that? I’m getting “is logged in true” “social logged in null”

phronmophobic19:07:41

is it being printed in that order? I would expect it to print in the opposite order either way there's no explicit order dependency between the reset! and the _ (js/console.log "social logged in " @!social-logged-in).

phronmophobic19:07:33

maybe try something like:

(defn login []
  (let [
        magic (new-magic)
        !social-logged-in (atom nil)
        _ (go (js/console.log "is logged in " (<p! (.. magic -user (isLoggedIn))
                                                   )))
        reset-ch (go (reset! !social-logged-in
                      (<p! (.. magic -user (isLoggedIn))
                           )))
        _ (go
            (<! reset-ch)
            (js/console.log "social logged in " @!social-logged-in))
        
        ]
   ))

zendevil.eth19:07:32

is <! valid in cljs?

zendevil.eth19:07:54

yes they are printed in that order

phronmophobic19:07:22

yes, <! is valid in cljs. it's <!! that doesn't work

zendevil.eth19:07:13

yeah that does print true for both, but why is <! required?

phronmophobic19:07:02

you want to print the result after the reset! has been run

zendevil.eth19:07:24

but how will this fit into the rest of the code? this is what follows the let bindings:

[:div
     (if @!social-logged-in
       [:div {:style {:cursor :pointer}
              :on-click #(.. magic -user (logout))} "Logout"]
       [:div {:style {:display :flex :flex-direction :row}}
        [:img
         {:on-click (fn [event]
                      (go
                        (<p! (.. magic -oauth
                                (loginWithRedirect
                                 #js {:provider "facebook"
                                      :redirectURI
                                      (.. js/window -location -origin)})))))
          :src ""
          :style {:cursor :pointer :height 50}}]])]
there’s a conditional rendering with if that depends on !social-logged-in

phronmophobic19:07:43

what UI library are you using?

phronmophobic19:07:21

is the atom an r/atom or the built in one?

phronmophobic19:07:35

it should be an r/atom so that it will rerender when the value is updated

ChillPillzKillzBillz21:07:53

I am trying to create a simple logging thread in clojurescript. I am following the examples in the learn-clojurescript ebook. My code is simple.. (ns tstcljs.core (:require [clojure.core.async :as async :refer [<! >! chan go-loop])) (def echo-chan (chan)) (go (prn (<! echo-chan))) (>! echo-chan "hello") At the last line I get the error: #object[Error Error: >! used not in (go ...) block] Why?

p-himik23:07:57

Because you used >! not in a go block?

p-himik23:07:02

The last line in your code.

ChillPillzKillzBillz00:07:37

heh ok. total newbee here... thanks man!!

leif21:07:23

Is there something like IPrintWithWriter in clojurescript, but can emit non-string values?

quoll04:07:12

I’m not sure what you mean here. Emit to where? If it’s the output stream then non strings can be converted, can’t they? Or am I missing something important here?

quoll04:07:38

For instance, I have code in a defrecord that emits the object as tagged data (specifically to emit in edn):

IPrintWithWriter 
  (-pr-writer [this writer _] (-write writer (str this)))

quoll04:07:43

Or, are you wanting to emit some kind of byte serialization?

leif20:07:43

@U051N6TTC I have my own *print-fn*, that can handle non-string values. I'd just like to send them through the println function if possible.

quoll20:07:46

So, what am I missing with using IPrintWithWriter?

leif21:07:28

Doesn't str emit a string?

leif21:07:05

If so, I don't want non-strings to be converted to strings. I want my custom *print-fn* function to get non-strings and embed them in the DOM.

leif21:07:17

Although I could be missing something. There seems to be a lack of documentation for the -pr-writer function. 😞

quoll21:07:07

str emits a string, yes. That’s what I sent to the -write function in my version of -pr-writer, but what else do you want to send to -write? Do you want to serialize something else into bytes?

quoll21:07:33

In my case, I have an object which wraps a number, and the string form is #a/n[1234]. By extending the object to IPrintWithWriter and having it call -write with the string form, then I get it printing properly at the repl, etc. I’m still trying to understand what you want to do, sorry

leif21:07:02

No worries. Ultimately, I would like to write out a DOM node.

leif21:07:30

For context, I'm making this for clojurescript: https://www.youtube.com/watch?v=8htgAxJuK5c

leif21:07:19

And by that, I don't mean a serialization of the dom node. I'd like to print out the dom node itself. (If I have to pass through a serialize/deserialize layer than oh well. I'll get around to it later.)

quoll21:07:53

But what do you mean by “print out the dom node”? Because if you’re printing it, then it’s a string, right?

leif21:07:56

Nope. Consider a web browser's console. When you print a DOM node to the console using console.log, you get a live object of that DOM node.

leif21:07:20

Its not just a string, its the data structure itself.

quoll21:07:46

actually, I did not know that. I thought that once it hit the console that it was a static snapshot rendered as a string

quoll21:07:40

Though, with immutable objects then it does this

leif21:07:56

Ah, nope. It actually is a thing that initially throws off a lot of devs, because javascript objects are mutable.

leif21:07:12

And if the object gets mutated after its printed out to the console, the object in the console changes.

leif21:07:39

So if you want a static render, you need to stringify and reparse it. (Per the MDN page anyway)

quoll21:07:32

Yes, I got that. So you want to be sending DOM objects through -write?

leif21:07:41

Ideally yes.

leif21:07:09

If that's not possible I'll eventually set up a mechanism to do the serialize/deserialize trick.

quoll21:07:26

I don’t know the implementation of the writer, so I don’t know if it can take objects?

leif21:07:51

It doesn't seem to be able to. 😞 (Hence my original question.)

leif21:07:44

Anyway though, thanks for the suggestion. 🙂

quoll21:07:53

I don’t know… maybe it does? It lets you send objects to it

quoll21:07:20

I don’t know if it’s only converting them to strings as it renders to my repl though 🙂

quoll21:07:32

I don’t have a repl in a browser to test it

quoll21:07:07

(deftype MyData [data]
  IPrintWithWriter
  (-pr-writer [this writer _] (-write writer {:data data})))
Then:
=> (MyData. 5)
{:data 5}

quoll21:07:55

But my repl is based on Node, since I don’t usually work in a browser

leif22:07:16

OOHH, this might be exactly what I want. I'll take a look at it. Thanks.

leif15:07:47

@U051N6TTC Ah, sadly it looks like it does cast it to a string. Oh well. Thanks again. 🙂

quoll16:07:13

Where are you wanting your data to go? To the console?

quoll16:07:03

If not, then does it need to be a standard protocol? Would it be possible to create your own, and then extend desired types to it? (Extending object for the default case)

leif18:07:20

@U051N6TTC Ultimately,, it's going into a <div> in the DOM that's meant to look like a console.

leif18:07:43

But ya, it doesn't have to use a standard protocol. I had just hoped to be able to, that way users don't have to add :refer-clojure :eexclude [println ...]).

quoll18:07:32

But that seems like it would only be necessary if you’re trying to insert a printed version into the DOM? Couldn’t you allow the standard printing to take place elsewhere, and then use your protocol for DOM insertion? I’m sorry. I keep feeling like I’m missing obvious things when we’re talking on this.

leif18:07:09

No worries, let me try to back up:

leif18:07:22

I am building an IDE in the browser using cljs.js. In this IDE, users both write and run their code directly in the browser.

leif19:07:12

In addition to supporting vanilla clojurescript, the IDE needs to support VISRs, which are mix of a macro, combined with a renderer, and some state (probably using deftype).

leif19:07:04

Users can print a VISr like any other object, but when they do, I want it to show up in the repl as a rich object, and not just its textual representation.

leif19:07:43

And I would ideally let users do the printing with standard print functions (`print`, println,`pprint`,etc.)

leif19:07:55

Does that make any more sense @U051N6TTC?