Giter VIP home page Giter VIP logo

Comments (3)

jimpil avatar jimpil commented on August 18, 2024

Here is an implementation of chime-ch that honors the doc-string (and if you let the user provide the channel, you pretty much have to honor it):

(let [ret (promise)
        sched (chime/chime-at times
                              (fn [time]
                                (when-not (a/>!! ch time)
                                  (a/close! @ret)))
                              {:on-finished (fn [] (a/close! @ret))})]
    (->> (reify
           p/ReadPort
           (take! [_ handler]
             (p/take! ch handler))

           p/Channel
           (close! [_] (.close sched) (a/close! ch))
           (closed? [_] (p/closed? ch)))

         (deliver ret)
         deref))

from chime.

jimpil avatar jimpil commented on August 18, 2024

There is still the question of what happens in case of an error. The schedule will stop, but both the returned channel, and the ch param will remain open when relying on the default error-handler, and there is no option to provide a custom one.

(defn chime-ch
  "..."
  [times & [{:keys [ch error-handler on-finished], :or {ch (a/chan)}}]]

  (let [ret (promise)
        sched (chime/chime-at times
                              (fn [time]
                                (when-not (a/>!! ch time)
                                  (a/close! @ret)))
                              {:on-finished (fn [] 
                                              (a/close! @ret) 
                                              (and on-finished (on-finished)))
                               :error-handler (fn [e] 
                                                (if error-handler 
                                                  (error-handler e) ;; user has the option to close `ch` here
                                                  true))})]
    (->> (reify
           p/ReadPort
           (take! [_ handler]
             (p/take! ch handler))

           p/Channel
           (close! [_] (.close sched) (a/close! ch))
           (closed? [_] (p/closed? ch)))

         (deliver ret)
         deref)))

from chime.

jimpil avatar jimpil commented on August 18, 2024

First of all happy new year!!! All the best wishes to you and your family :)

Secondly, I took a step back and wondered...

what is the point of letting the user provide the underlying channel?

After all, chime-at returns a readable channel - why would we let the user provide the underlying write channel too? The only reason I can think of is buffering (i.e. perhaps the user wants to supply a buffered channel). In light of this consider the following alternative implementation:

(defn chime-ch
  "..."
  [times & [{:keys [buffer error-handler on-finished]}]]

  (let [ch (a/chan buffer) ;; build the channel internally
        ret-ch (promise)
        error-handler (if error-handler
                        (fn [e]
                          (or (error-handler e)    ;; user's error-handler says to carry on with the schedule
                              (a/close! @ret-ch))) ;; user's error-handler says to stop the schedule
                        (constantly true)) ;; user didn't provided an error-handler - carry on with the schedule
        sched (chime/chime-at times
                              (fn [time] (a/>!! ch time)) ;; nothing special to do here
                              {:on-finished (fn []
                                              (a/close! ch) ;; only this needs closing at this point
                                              (and on-finished (on-finished)))
                               :error-handler error-handler})]
    (->> (reify
           p/ReadPort
           (take! [_ handler]
             (p/take! ch handler))

           p/Channel
           (close! [_] (.close sched) (a/close! ch)) ;; close both!
           (closed? [_] (p/closed? ch)))

         (deliver ret-ch)
         deref)))

I fully understand you don't like breaking changes, but do you see how simpler/clearer this becomes? Basically you only need to worry about the thing that you give back to the user. Anyway, enough for tonight ;)

from chime.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.