Last active
August 29, 2015 14:18
-
-
Save mithrandi/fdeeb25cb05c846eceab to your computer and use it in GitHub Desktop.
Version of map handling deferreds/errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns clj-amp.core | |
(:require [manifold.deferred :as d] | |
[manifold.stream :as s])) | |
(defn- map' | |
"Like manifold.stream/map, except handles deferreds and closes the stream if an error occurs." | |
([f s] | |
(let [s' (s/stream)] | |
(s/connect-via | |
s | |
(fn [msg] | |
(-> msg | |
(d/chain | |
f | |
#(s/put! s' %)) | |
(d/catch | |
(fn [e] | |
(s/close! s') | |
e)))) | |
s' | |
{:description {:op "map"}}) | |
(s/source-only s'))) | |
([f s & rest] | |
(map' #(apply f %) | |
(apply s/zip s rest)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pasted an old buggy version of the code, updated with the proper error-close logic.