Repository navigation
Spike on chrysalis idea - #6
stevenproctor wants to merge 4 commits into
Conversation
|
Targeting the I will ultimately target |
| (catch #?(:clj Throwable :cljs :default) err | ||
| (transition ctx err)))))) | ||
| ctx) | ||
| f (or (stage ix) identity)] |
There was a problem hiding this comment.
Removed redundant let.
| (ns lambda-toolshed.papillon.async) | ||
|
|
||
| (defprotocol Chrysalis | ||
| (eclose [this handler])) |
There was a problem hiding this comment.
eclose
intransitive verb
ēˈklōz
-ed/-ing/-s
of an insect
: to emerge from the eggshell or pupal case
There was a problem hiding this comment.
linguistically it works, though does have me wondering if sticking too close to the entomological theme within the library, might cause/increase friction for non-native English speakers.
or cross pollination with finance industry terms like "e-close"
There was a problem hiding this comment.
"or cross pollination with ..."
I see what you did there.
There was a problem hiding this comment.
But isn't it the Clojure way to find an underutilized word, give a definition, and get it into more mainstream usage in the Clojure community? 😜
There was a problem hiding this comment.
yeah, a lot of lib/repos do a similar thing. To be clear, Chrysalis is probably fine, my concern is more around eclose and how it might lead to human parsing errors. On a quick read can be read as close instead, leading them to thinking it's about closing or dismissing a thing, instead of realizing or completion of a thing.
There was a problem hiding this comment.
I am okay with a different/better name. I am even open to renaming the protocol.
I went with these to avoid conceptual collisions with other libraries' ideas of having an "Async Context" to let it stand independently.
I wanted to ensure they were distinct enough that someone didn't confuse this protocol with something else. 😉
There was a problem hiding this comment.
Any objections to emerge? Or is that too close to other functions like eclose is?
There was a problem hiding this comment.
emerge can work, it's a clear enough term that it'd be a lot less likely to cause language parsing errors.
if emerge doesn't quite click, the synonyms surface or materialize might be options.
yeah, finding the best term here is tough, when time allows I've been digging through CS literature to try and find the best answer (most things just use "async"). the problem of, how do we describe the context of asynchronous operations in a unique but also still related way.
something like allochronic (https://www.merriam-webster.com/dictionary/allochronic) might work in place of async, though from the definition is more about geologic time. possible that allochronic it's esoteric enough in this context that it would also cause confusion.
I'll keep looking just in case.
There was a problem hiding this comment.
AsyncSystem might also work for the protocol
https://en.wikipedia.org/wiki/Asynchronous_system
| ([ixs] | ||
| (execute ixs {})) | ||
| ([ixs ctx] | ||
| #?(:org.babashka/nbb nil |
There was a problem hiding this comment.
Don't give the 2-arity to nbb as nbb doesn't support core.async
CLJ and CLJS get the nice behavior we see in the tests about being able to get async results on a channel.
| result (leave (enter ctx))] | ||
| (if (satisfies? ReadPort result) | ||
| (present-async result) | ||
| (if (satisfies? Chrysalis result) |
There was a problem hiding this comment.
I don't think I like this...
Part of me says:
If using callback functions, we should always present via callback functions, even if it ends up being all synchronous.
The other part:
It is kinda nice to piggyback off the callback version, as that means we test that version when using the 2-arity version in our tests. Also 🤢🤮 of callback functions when something is completely synchronous.
There was a problem hiding this comment.
Still trying to think through the best paths for all this. Like, is there a way to reasonably represent these concepts as part of the context map through reserved/predefined namespaced keys? nbb reader conditional can still happen, could be two separate inside of present-async.
Taking the path of "sane defaults, overwritable in user land" is a thing, always triggering callback functions if present does make sense. Don't want that functionality, don't set them.
Also, if they're represented as keys on the context, it allows for dynamically removing or adding them as needed during execution.
With the base problem being how best to represent an agnostic interface for async mechanisms, it has me wondering what path reduces the most conceptual complexity.
There was a problem hiding this comment.
The imposition of callback fns for completely synchronous work is a lot of incidental complexity that is not present in alternative interceptor libs.
Regarding the desire to support alternate implementations of deferred computations, has there been any investigation of an approach using requiring-resolve and multiple alternative impl namespaces (all implementing the Chrysalis protocol) that could be optionally added as an additional artifact? So require lambda-toolshed/papillon and lambda-toolshed/papillon-manifold. In l-t/execute you could provide an optional parameter to override the default (core.async) and bring in an alternate implementation.
Apologies if this has already been considered...
There was a problem hiding this comment.
Interesting 🤔
Pretty sure we chatted about something similar, but weren't really sure about how to represent it. one example @stevenproctor was looking at was AsyncContext from sieppari.
There was a problem hiding this comment.
Wow... that AsyncContext in sieppari is exactly what I had in mind, with the addition of separate artifacts to complement add-ons like manifold.
There was a problem hiding this comment.
I think the term is "Same Same, but Different." 😉
I looked at it, and it was part of the reason I finally went with a distinct protocol.
I opted not to do catch and continue because of our error handling with core.async channels, and I went with only having eclose instead.
I put the core in async, although maybe if we decide to return it, we can move it to the main namespace. For extending ReadPort with the protocol, I put that in the lambda-toolshed.papillon.async.core_async namespace, which the tests require to load that functionality to get channels.
I left out manifold as I didn't want to put a dependency on manifold in the project, as those who want it could extend it themselves.
Given how we are currently handling the pure sync version, I was also unsure if we would want a blocking take similar to the AsyncContext of sieppari.
There was a problem hiding this comment.
My endorsement of sieppari's approach was limited to the protocol + extension to "built-in" types + (presumably) external artifacts/customization to extend to other defereds. I did not mean to endorse catch/continue and TBH I have not dug deep enough into either sieppari's implementation or ours to render a useful opinion on the shape of the protocol.
(I will try to dig deeper into the Chrysalis implementation this weekend).
I strongly agree with leaving out any third party dependencies.
I agree that a blocking take "behind the scenes" in the sync path is to be avoided.
There was a problem hiding this comment.
The imposition of callback fns for completely synchronous work is a lot of incidental complexity that is not present in alternative interceptor libs.
Yeah, that is what I was thinking, but it did feel off that the result could be given via callback or return value, and the person would have to know which case they are in.
The other options that came to mind were:
- don't piggyback the 2-arity and the 4-arity; if you use the 4-arity version, you always live in callback land. 😕
- to return the result and check if Chrysalis protocol, and have them call
eclosewith their handler (this was made cleaner by thetransitionfrom theerror-handlingbranch, as before it took both the initial-context and the candidate context. - always return a Chrysalis, and make the raw context object (either the current
mapor a new record type) be shared so thateclosereturns itself - return a Chrysalis as above, and give a blocking take that does identify if it was in synchronous execution.
- some other option that you all might think of
There was a problem hiding this comment.
IIUC, numbers 3/4 involve wrapping sync behavior in Chrysalis. That's a reasonable solution as long as we stick to plain maps for context (with magic type metadata) not some custom type and we don't transition to async and then "fake it" with a blocking take -that will present an unexpected debugging nightmare.
There was a problem hiding this comment.
I pushed some changes that refined some of this thinking.
I now think of present-async as an abstraction, and we can present-promise, present-channel, or some other function if given in the context under the key :lambda-toolshed.papillon/present-async.
present-promise is only available in ClojureScript.
present-channel is not available in nbb.
The present-async default, if not given, is:
present-promiseif in nbb,present-channelotherwise.
| (is (empty? (::ix/queue res))) | ||
| (is (empty? (::ix/stack res))) | ||
| (is (= (ex-message the-exception) (ex-message (res ::error)))) | ||
| (is (= expected-log (::ix/trace res)))))))) |
There was a problem hiding this comment.
After some feedback on how it should behave, I will add some tests for the 4-arity version.
|
This is for #2 to think about supporting a Clojure runtime where core.async doesn't exist. Also, it helps support the world where people might choose Manifold, Promesa, or something else they already have with their project. |
Give ability to specify how to present an async result as the return value of the function in async world
27c3dee to
1c04dd6
Compare
|
I think this should be ready for a more "final review" |
cch1
left a comment
There was a problem hiding this comment.
Is there a reasonable way to break this PR into pieces? I'm thinking one piece to generalize async support through Chrysalis and a PR to support babashka.
| (update ctx :number #(* % 2)))}) | ||
|
|
||
| ;; Run an interceptor chain with one interceptor in it that is synchronous | ||
| ;; Run an interceptor chain with two interceptor in it that is synchronous |
| goog.string.format)))) | ||
| (:require #?@(:cljs ([goog.string :as gstring] | ||
| goog.string.format)) | ||
| #?(:org.babashka/nbb [] |
There was a problem hiding this comment.
I thought we concluded bb does support core async...
There was a problem hiding this comment.
This is nbb not bb. 😉
| (emerge result #(present-async % presenter)) | ||
| (presenter result))) | ||
|
|
||
| #?(:cljs |
There was a problem hiding this comment.
I'm bothered by the lack of generalization of the "present" concept that results in two diametric fn names. Can't present-channel and present-promise be unified in one fn whose implementation differs across platforms?
There was a problem hiding this comment.
My idea was that it would allow someone else to give a different presenter (e.g. manifold or promesa), or pick if they want promises vs channels if in CLJS.
There was a problem hiding this comment.
Would present-as-promise and present-as-channel be better names?
There was a problem hiding this comment.
is there a way to achieve both? unification of terms, while maintaining optionality/composability? possibly protocol driven?
There was a problem hiding this comment.
Yep, my belief/hope is that the terms can be unified. It's not a question of naming -it's the generalization (or lack thereof).
| ([ixs ctx] | ||
| (let [ixs (map-indexed namer ixs) | ||
| ctx (init-ctx ctx ixs) | ||
| present-async (::present-async ctx #?(:org.babashka/nbb present-promise :default present-channel)) |
There was a problem hiding this comment.
See previous comment about unification of present.
|
I can look into breaking it into two. The async generalization came from trying to support |
PR #7 opened. Also, the presentation is for nbb (not babashka? node babashka?), not babashka itself per se. The presentation also would help some with the tests that were updated in #7 on the CLJS side, which had to do a |
No description provided.