Comments (3)
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.
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.
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)
- Thread names don't include numbers HOT 3
- Feature request: Allow adding new times to a chimes-ch channel HOT 2
- Dependency to clj-time unnecessary HOT 1
- Expose :thread-factory option HOT 1
- Dynamic bindings not conveyed HOT 5
- periodic-seq NullPointerException HOT 4
- Error running scheduled fn java.lang.NullPointerException: Cannot invoke "String.length()" because "s" is null HOT 3
- Examples assume Dates instead of Instants? HOT 2
- Drop times that have already passed HOT 1
- Long running tasks HOT 1
- chime-at API changes HOT 1
- On-finished weird behaviours HOT 1
- Bump library to use log4j 2.16.0 in clojure/tools.logging HOT 1
- multiple chime-at calls HOT 1
- periodic-seq example and DST changes HOT 1
- Error in the doc-string? HOT 1
- Readme uses deprecated chime/ instead of chime.core/ HOT 1
- parameterize the executor HOT 1
- Don't understand how to run chime each day at a specific hour. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from chime.