Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Plain Context API (no request/response) #30

Closed
wants to merge 6 commits into from

Conversation

martinklepsch
Copy link

This tries to address some of the requests (#15 & #28) for a more context focused API (i.e. without the request/response concepts).

execute-ctx still returns only the :response value but that's probably something we can figure out.

The main goal here is that we can move forward with a Context-based API while maintaining the previous request/response style for backwards compatibility.

cc @dspiteself @jjttjj @den1k

@den1k
Copy link
Contributor

den1k commented Apr 1, 2020

@martinklepsch looks good, thanks for doing this!

About execute-ctx returning :response. It looks like only await-result and deliver-result care about the :response. Could they maybe be passed a result-getter function instead that is identity for execute-ctx and :response for execute? It's less DRY but avoids leaking :response into the execute-ctx API.

@den1k
Copy link
Contributor

den1k commented Apr 1, 2020

Here it is sketched out (untested)

#?(:clj
   (defn- await-result [ctx]
     (if (a/async? ctx)
       (recur (a/await ctx))
       (if-let [error (:error ctx)]
         (throw error)
         ctx))))  ; <---

(defn- deliver-result [ctx get-result]  ; <---
  (if (a/async? ctx)
    (a/continue ctx deliver-result)
    (let [error    (:error ctx)
          result   (or error (get-result ctx))  ; <---
          callback (if error :on-error :on-complete)
          f        (get ctx callback identity)]
      (f result))))


(defn execute-ctx
  {:arglists '([interceptors initial-context]
               [interceptors initial-context on-complete on-error])}
  ([interceptors initial-context on-complete on-error]
   (execute-ctx interceptors initial-context on-complete on-error identity))  ; <---
  ([interceptors initial-context on-complete on-error get-result]
   (if-let [queue (q/into-queue interceptors)]
     (-> (context initial-context queue on-complete on-error)
         (enter)
         (leave)
         (deliver-result get-result))  ; <---
     ;; It is always necessary to call on-complete or the computation would not
     ;; keep going.
     (on-complete nil))
   nil)
  #?(:clj
     ([interceptors initial-context]
      (when-let [queue (q/into-queue interceptors)]
        (-> (context initial-context queue)
            (enter)
            (leave)
            (await-result))))))

(defn execute
  ([interceptors request on-complete on-error]
   (execute-ctx interceptors {:request request} on-complete on-error :response)) ; <---
  #?(:clj
     ([interceptors request]
      (some-> (execute-ctx interceptors {:request request})
              :response)))) ; <---

@martinklepsch martinklepsch changed the title [WIP] Context API [WIP] Plain Context API (no request/response) Apr 2, 2020
@martinklepsch
Copy link
Author

I ended up just adding execute-ctx which duplicates most of the code of execute.

@den1k I've added you as a collaborator to my fork, please feel free to help with updating/expanding the test suite and testing in general. I'd love to get this into a place where it could be considered for inclusion in Sieppari itself but also only have limited time to hack on this...

@den1k
Copy link
Contributor

den1k commented Apr 2, 2020

@martinklepsch I'm in the same boat time-wise at least until the end of this week. However, it looks like you already made changes to src + tests. Is this ready for review?

Copy link
Member

@ikitommi ikitommi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking great! merge is kinda illegal for any performance critical code, instead, we could use two different Record impls, one for Request/Response things, another for the raw impl.

([initial-context queue]
(merge (new Context nil queue nil nil nil) initial-context))
([initial-context queue on-complete on-error]
(merge (new Context nil queue nil on-complete on-error) initial-context)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merge will sadly kill all performance.

Copy link
Member

@ikitommi ikitommi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests don't pass.

@den1k den1k mentioned this pull request Apr 27, 2020
@martinklepsch
Copy link
Author

closing in favor of #31

@martinklepsch martinklepsch deleted the context-api branch April 30, 2020 15:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants