Fork me on GitHub
#clojurescript
<
2021-10-04
>
vemv15:10:38

ℹ️ If you use https://github.com/clojure-emacs/clj-suitable/ i.e. CIDER's clojurescript autocompletion, you can use its new bugfix release - first one in a while! You can grab it by simply using https://github.com/clojure-emacs/cider latest (per MELPA snapshots) - it will pull cider-nrepl latest, and clj-suitable latest with it. In particular, now completions won't choke on "string requires". Let us know how it works for you cider

👍 1
alexdavis15:10:49

I’m trying to translate this:

useMutation(
    delete, {
        onMutate: async (newTodo: string) => {
            await queryClient.cancelQueries('todos')
                ...
                return {
                    previousTodos
                }
        },
        // If the mutation fails, use the context returned from onMutate to roll back
        onError: (err, variables, context) => {
            if (context?.previousTodos) {
                queryClient.setQueryData < Todos > ('todos', context.previousTodos)
            }
        },
    }
)
where onMutate is an async function that returns an object, and that object can be accessed from a normal function (onError), but I have no idea how to do it in cljs. Heres what I have so far using this for help
(defn handle-delete
  [client key id]
  (go
    (<p! (.cancelQueries client key))
    (let ...
      {:previous previous-vals
       :new new-vals})))

(defn handle-error
  [err new-item context]
  (let [previous (:previous context)]
    (.setQueryData client previous)))

(useMutation
 delete
 #js {:onMutate #(handle-delete client %)
      :onError handle-error
      :onSettled #(.invalidateQueries client key)})
I’ve cut out a fair bit but hopefully the important stuff is all there

alexdavis15:10:05

The issue is that context is a #object[cljs.core.async.impl.channels.ManyToManyChannel]

p-himik16:10:32

Don't use core.async at all. Instead, use JS promises - they are the ones you want when you need to translate JS async/`await`.

alexdavis16:10:26

Thanks, I think I misinterpreted this https://clojurescript.org/guides/promise-interop#using-promises-with-core-async I am not familiar with either promises or core async and just assumed they were compatible

alexdavis16:10:46

so I basically have to use .then? There’s no way to have async/await syntax?

p-himik16:10:18

That is correct.

Ryan15:10:48

Does anyone have any clues why I can't use cljs.repl/source on the output of a higher order fn? code in thread.

Ryan16:10:15

(defn- gen-any-value-filter [attribute]
  (fn [indexed-entity]
    (let [entity (util/get-indexed-entity indexed-entity)]
      (some? (get attribute entity)))
    )
)

Ryan16:10:20

(cljs.repl/source (gen-any-value-filter :phone-number))

Ryan16:10:23

----- REPL Error while processing ---------------------------------------------
(cljs.repl/source (gen-any-value-filter :phone-number))
Encountered error when macroexpanding cljs.repl/source.
AssertionError: Assert failed: (symbol? sym)
	cljs.analyzer.api/resolve (api.cljc:201)
	cljs.analyzer.api/resolve (api.cljc:201)
	cljs.repl/source-fn (repl.cljc:1502)
	cljs.repl/source-fn (repl.cljc:1493)
... snip

p-himik16:10:15

Because source is a macro that expects a symbol. It can't deal with results of higher order functions at all.

Ryan16:10:34

aha, so is there a way to see what code a generation function produces?

p-himik16:10:08

Not directly. So if you have some third-party higher order function, you're out of luck. But if you're the author of such a function, you can attach metadata that would contain the code itself. But it still won't be supported by source - it would be something you would have to know how to use. But what's the problem you're trying to solve? Maybe there are other ways.

Ryan16:10:00

Was mostly my own curiosity to see if the function was doing what I thought it was

p-himik16:10:02

Ah, then it's much easier to just go to the source file and look there. :)

Ryan16:10:59

I mean I would think so but it eludes me a bit

Ryan16:10:36

Ok I think I've figured it out, I think I was having trouble using the (:key map) access method with a passed-in key? making the access call as (get :key map) worked

👍 1
p-himik16:10:25

You probably meant (get map :key).

Ryan16:10:25

yes 🙂

leif20:10:56

Is there any way I can change the tools.reader library I use with clojurescript?

leif20:10:12

As a test, when I add:

org.clojure/tools.reader {:local/root "/home/leif/src/tools.reader"}
to my deps.edn file, I start getting errors about not being able to find tools.reader in clojurescript:
No such namespace: cljs.tools.reader, could not locate cljs/tools/reader.cljs, cljs/tools/reader.cljc, or JavaScript source providing "cljs.tools.reader" in file /home/leif/.gitlibs/libs/org.clojure/clojurescript/a35a8e3927a53f69414ed684be5dfb3af27e80d8/src/main/cljs/cljs/reader.cljs