Skip to content

Commit

Permalink
Add 'resource' combinator
Browse files Browse the repository at this point in the history
Sometimes its necessary to lift pure values into the `Resource` monad
within the `RequestT` monad transformer, e.g. in `whenDone` actions.
  • Loading branch information
Stefan Kersten committed Jun 6, 2012
1 parent 3d01b0e commit 076cfcd
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions Sound/SC3/Server/Monad/Request.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Sound.SC3.Server.Monad.Request
, AllocT , AllocT
-- * Resources -- * Resources
, Resource , Resource
, resource
, extract , extract
, after , after
, after_ , after_
Expand Down Expand Up @@ -156,6 +157,10 @@ newtype Resource m a = Resource { extract :: ServerT m a -- ^ Extract result fro
} }
deriving (Applicative, Functor, Monad) deriving (Applicative, Functor, Monad)


-- | Return a pure value as a 'Resource' in the 'RequestT' monad transformer.
resource :: Monad m => a -> RequestT m (Resource m a)
resource = return . return

-- | Register a cleanup action that is executed after the notification has been -- | Register a cleanup action that is executed after the notification has been
-- received and return the deferred notification result. -- received and return the deferred notification result.
after :: MonadIO m => Notification a -> AllocT m () -> RequestT m (Resource m a) after :: MonadIO m => Notification a -> AllocT m () -> RequestT m (Resource m a)
Expand Down
4 changes: 2 additions & 2 deletions examples/hello.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Sound.SC3.Server.Monad.Command
-- You need the hsc3-server-internal package in order to use the internal server -- You need the hsc3-server-internal package in order to use the internal server
--import Sound.SC3.Server.Monad.Process.Internal (withDefaultInternal) --import Sound.SC3.Server.Monad.Process.Internal (withDefaultInternal)
import Sound.SC3.Server.Monad.Process (withDefaultSynth) import Sound.SC3.Server.Monad.Process (withDefaultSynth)
import Sound.SC3.Server.Monad.Request ((!>), async, extract, whenDone) import Sound.SC3.Server.Monad.Request ((!>), async, extract, resource, whenDone)
import Sound.SC3.Server.Notification import Sound.SC3.Server.Notification
import Sound.OpenSoundControl (immediately) import Sound.OpenSoundControl (immediately)
import qualified Sound.OpenSoundControl as OSC import qualified Sound.OpenSoundControl as OSC
Expand Down Expand Up @@ -33,7 +33,7 @@ main = run $ do
r <- rootNode r <- rootNode
synth <- extract =<< immediately !> do synth <- extract =<< immediately !> do
d_recv "hsc3-server:sine" sine `whenDone` d_recv "hsc3-server:sine" sine `whenDone`
\sd -> s_new sd AddToTail r [("freq", 440), ("amp", 0.2)] \sd -> resource =<< s_new sd AddToTail r [("freq", 440), ("amp", 0.2)]
fork statusLoop fork statusLoop
pauseThread 10 pauseThread 10
immediately !> s_release 0 synth immediately !> s_release 0 synth
Expand Down

0 comments on commit 076cfcd

Please sign in to comment.