2-- Module : Streamly.Internal.Data.Stream.Exception
3-- Copyright : (c) 2019 Composewell Technologies
4-- License : BSD-3-Clause
5-- Maintainer : streamly@composewell.com
6-- Stability : experimental
9module Streamly.Internal.Data.Stream.Exception
25import Control.Exception (Exception)
26import Control.Monad.Catch (MonadCatch)
27import Control.Monad.IO.Class (MonadIO)
28import Streamly.Internal.Data.Stream.Type (Stream, fromStreamD, toStreamD)
30import qualified Streamly.Internal.Data.Stream.StreamD as D
34-- >>> import qualified Streamly.Internal.Data.Stream as Stream
36------------------------------------------------------------------------------
38------------------------------------------------------------------------------
40-- | Run the action @m b@ before the stream yields its first element.
42-- Same as the following but more efficient due to fusion:
44-- >>> before action xs = Stream.nilM action <> xs
45-- >>> before action xs = Stream.concatMap (const xs) (Stream.fromEffect action)
48before :: Monad m => m b -> Stream m a -> Stream m a
49before action xs = fromStreamD $ D.before action $ toStreamD xs
51-- | Like 'after', with following differences:
53-- * action @m b@ won't run if the stream is garbage collected
54-- after partial evaluation.
55-- * Monad @m@ does not require any other constraints.
56-- * has slightly better performance than 'after'.
58-- Same as the following, but with stream fusion:
60-- >>> afterUnsafe action xs = xs <> Stream.nilM action
64{-# INLINE afterUnsafe #-}
65afterUnsafe :: Monad m => m b -> Stream m a -> Stream m a
66afterUnsafe action xs = fromStreamD $ D.after_ action $ toStreamD xs
68-- | Run the action @IO b@ whenever the stream stream stops normally, or
69-- if it is garbage collected after a partial lazy evaluation.
71-- The semantics of the action @IO b@ are similar to the semantics of cleanup
72-- action in 'bracketIO'.
74-- /See also 'afterUnsafe'/
77afterIO :: MonadIO m => IO b -> Stream m a -> Stream m a
78afterIO action xs = fromStreamD $ D.after action $ toStreamD xs
80-- | Run the action @m b@ if the stream aborts due to an exception. The
81-- exception is not caught, simply rethrown.
83-- /Inhibits stream fusion/
85{-# INLINE onException #-}
86onException :: MonadCatch m => m b -> Stream m a -> Stream m a
87onException action xs = fromStreamD $ D.onException action $ toStreamD xs
89-- | Like 'finally' with following differences:
91-- * action @m b@ won't run if the stream is garbage collected
92-- after partial evaluation.
93-- * has slightly better performance than 'finallyIO'.
95-- /Inhibits stream fusion/
99{-# INLINE finallyUnsafe #-}
100finallyUnsafe :: MonadCatch m => m b -> Stream m a -> Stream m a
101finallyUnsafe action xs = fromStreamD $ D.finally_ action $ toStreamD xs
103-- | Run the action @IO b@ whenever the stream stream stops normally, aborts
104-- due to an exception or if it is garbage collected after a partial lazy
107-- The semantics of running the action @IO b@ are similar to the cleanup action
108-- semantics described in 'bracketIO'.
110-- >>> finallyIO release = Stream.bracketIO (return ()) (const release)
112-- /See also 'finallyUnsafe'/
114-- /Inhibits stream fusion/
116{-# INLINE finallyIO #-}
117finallyIO :: (MonadIO m, MonadCatch m) =>
118 IO b -> Stream m a -> Stream m a
119finallyIO action xs = fromStreamD $ D.finally action $ toStreamD xs
121-- | Like 'bracket' but with following differences:
123-- * alloc action @m b@ runs with async exceptions enabled
124-- * cleanup action @b -> m c@ won't run if the stream is garbage collected
125-- after partial evaluation.
126-- * has slightly better performance than 'bracketIO'.
128-- /Inhibits stream fusion/
132{-# INLINE bracketUnsafe #-}
133bracketUnsafe :: MonadCatch m
134 => m b -> (b -> m c) -> (b -> Stream m a) -> Stream m a
135bracketUnsafe bef aft bet = fromStreamD $ D.bracket_ bef aft (toStreamD . bet)
137-- | Run the alloc action @IO b@ with async exceptions disabled but keeping
138-- blocking operations interruptible (see 'Control.Exception.mask'). Use the
139-- output @b@ as input to @b -> Stream m a@ to generate an output stream.
141-- @b@ is usually a resource under the IO monad, e.g. a file handle, that
142-- requires a cleanup after use. The cleanup action @b -> IO c@, runs whenever
143-- the stream ends normally, due to a sync or async exception or if it gets
144-- garbage collected after a partial lazy evaluation.
146-- 'bracketIO' only guarantees that the cleanup action runs, and it runs with
147-- async exceptions enabled. The action must ensure that it can successfully
148-- cleanup the resource in the face of sync or async exceptions.
150-- When the stream ends normally or on a sync exception, cleanup action runs
151-- immediately in the current thread context, whereas in other cases it runs in
152-- the GC context, therefore, cleanup may be delayed until the GC gets to run.
154-- /See also: 'bracketUnsafe'/
156-- /Inhibits stream fusion/
158{-# INLINE bracketIO #-}
159bracketIO :: (MonadIO m, MonadCatch m)
160 => IO b -> (b -> IO c) -> (b -> Stream m a) -> Stream m a
161bracketIO bef aft = bracket3IO bef aft aft aft
163-- For a use case of this see the "streamly-process" package. It needs to kill
164-- the process in case of exception or garbage collection, but waits for the
165-- process to terminate in normal cases.
167-- | Like 'bracketIO' but can use 3 separate cleanup actions depending on the
168-- mode of termination:
170-- 1. When the stream stops normally
171-- 2. When the stream is garbage collected
172-- 3. When the stream encounters an exception
174-- @bracket3IO before onStop onGC onException action@ runs @action@ using the
175-- result of @before@. If the stream stops, @onStop@ action is executed, if the
176-- stream is abandoned @onGC@ is executed, if the stream encounters an
177-- exception @onException@ is executed.
180{-# INLINE bracket3IO #-}
181bracket3IO :: (MonadIO m, MonadCatch m)
188bracket3IO bef aft gc exc bet = fromStreamD $
189 D.bracket' bef aft exc gc (toStreamD . bet)
191-- | Like 'handle' but the exception handler is also provided with the stream
192-- that generated the exception as input. The exception handler can thus
193-- re-evaluate the stream to retry the action that failed. The exception
194-- handler can again call 'ghandle' on it to retry the action multiple times.
196-- This is highly experimental. In a stream of actions we can map the stream
197-- with a retry combinator to retry each action on failure.
199-- /Inhibits stream fusion/
203{-# INLINE ghandle #-}
204ghandle :: (MonadCatch m, Exception e)
205 => (e -> Stream m a -> Stream m a) -> Stream m a -> Stream m a
208 . D.ghandle (\e xs -> toStreamD $ handler e (fromStreamD xs))
211-- | When evaluating a stream if an exception occurs, stream evaluation aborts
212-- and the specified exception handler is run with the exception as argument.
214-- /Inhibits stream fusion/
217handle :: (MonadCatch m, Exception e)
218 => (e -> Stream m a) -> Stream m a -> Stream m a
220 fromStreamD $ D.handle (toStreamD . handler) $ toStreamD xs
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list