Fork me on GitHub
#clojurescript
<
2019-07-26
>
caleb.macdonaldblack02:07:23

When developing a library what's the standard way for implementing asynchronous code? Such as callback, promise, core.async.

Roman Liutikov08:07:28

I’d say go with Promises, they are supported everywhere these days and easier to use than callbacks. core.async is a bit too heavy weight, depending on what your goal is of course

leonoel12:07:34

callback pair + cancellation support, please.

caleb.macdonaldblack02:07:14

With callback, the developer could implement promise or core.async if they wanted to

caleb.macdonaldblack02:07:27

However I believe promise would be convenient to have.

caleb.macdonaldblack02:07:31

I find in general core.async has too much overhead compared to callback and promise and I usually defer using it unless I have a problem that core.async is good at solving

caleb.macdonaldblack02:07:19

So I'm thinking of overloading my async functions so that they can take a callback, but will return a promise if the callback is not provided. What are other peoples thoughts?

nenadalm04:07:17

I think this is pretty common way how people do it in js (e.g. https://www.npmjs.com/package/bcrypt#with-promises) so it's probably fine.

carkh02:07:09

i'd say don't force async stuff on your users, callback is fine, let them do it their way

carkh02:07:14

also if possible at all, hide asynchronicity

carkh02:07:28

there was a nice talk about it

caleb.macdonaldblack03:07:36

@carkh I plan on wrapping a library so the async methods would need to be exposed to support asynchronous

caleb.macdonaldblack03:07:02

When you say hide asynchronocity, what do you mean by that?

carkh03:07:12

i'm trying to find that talk but i can't ...

carkh03:07:31

they were providing examples like om/fulcro

carkh03:07:45

there is much asynchronicity in there, but you don't see it as a library user

caleb.macdonaldblack03:07:12

oh okay I see what you mean

carkh03:07:37

or pedestal interceptors, as a user you don't care or even need to know if it's using core.async

carkh03:07:51

and indeed it's only using it when necessary

carkh03:07:57

(if i recall correctly)

clslcl09:07:55

that's weird - I can't believe there is no way to find requires of a namespace in repl, yet find usages of variable. Am I just missing something?

Oliver George11:07:44

Hi All, I couldn't find a page on http://clojurescript.org which introduces javascript interop and the #js literal. Is there an authoritative reference?

Chris Swanson12:07:05

does anyone know of any libraries for generic plugin type behavior? Like if i wanted to write a cljs ui with features enabled / disabled by serverside config, and have a clean api for registering new features into the app without interfering with existing features, attach metadata about how they could expose metrics or potentially communicate with each other, hints about layout, etc. I'm not looking for something that actually does metrics or layout, just looking for a solid, extensible foundation for implementing highly modular behavior on top of. Maybe that's just super idealistic though

scknkkrer12:07:42

@chrisjswanson, idealism, yes. I love it! If there is no such a library. We have to act and getting together to create one.

Chris Swanson13:07:06

@scknkkrer maybe that library is named Clojure 😛 but seriously it seems like a common enough pattern, i'd expect it's been done in other languages and frameworks to various levels. Ironically, the most modular pluggable ui component and behavior type framework i can think of is Drupal. I'm not a big fan of php and don't know that it'd be wise to draw inspiration from their implementation, but no doubt they've thought about the problem to great length. A light weight clojure library with cms-like isolation of ui components and any functionality they add to the system would be a fun project

Chris Swanson13:07:59

I'm particularly interested in it for mobile apps, to turbocharge a remote config solution

thheller13:07:37

:advanced compilation makes building "highly modular" things kind of tough if not impossible so you'd have to make a lot of compromises if you want to build something that is truly modular

Chris Swanson14:07:21

hmmm, that is something i hadn't considered much

Chris Swanson14:07:51

tbh i don't know half as much about advanced compilation as I should; sounds like now's the time to learn a bit more

Chris Swanson14:07:53

thanks for the tip

lilactown15:07:29

as long as you have a single "project" that contains all of the modules, you can use code splitting to modularize your code base pretty efficiently

✔️ 4
lilactown15:07:44

the issue is when you want to allow external parties provide plugins or modules

eskemojoe00716:07:03

Going through some re-frame tutorials. I saw some folks using (rf/path) in their event handlers that confused me. They had written something simple like:

(rf/reg-event-db
  :form/set-field
  [(rf/path :form/fields)]
  (fn [fields [_ id value]]
    (assoc fields id value)))
Where I had written what I think is similar:
(rf/reg-event-db
 :form/set-field
 (fn [db [_ id value]]
   (assoc-in db [:form/fields id] value)))

eskemojoe00716:07:14

Wondering what best practice, or most normal is?

eskemojoe00716:07:33

Or if they truely are equivalent.

Lu23:07:39

@david.folkner By looking at the source code, they are not equivalent .. They simply yield the same output

Lu23:07:49

As you are adding an interceptor to the chain, there's a bit of more logic involved (which I assume is negligible)