Permalink
Browse files

Add 'resource' combinator

Sometimes its necessary to lift pure values into the `Resource` monad
within the `RequestT` monad transformer, e.g. in `whenDone` actions.
  • Loading branch information...
1 parent 3d01b0e commit 076cfcdfdfa6acc1828e9176f39403ebfc00d878 Stefan Kersten committed Jun 6, 2012
Showing with 7 additions and 2 deletions.
  1. +5 −0 Sound/SC3/Server/Monad/Request.hs
  2. +2 −2 examples/hello.hs
View
5 Sound/SC3/Server/Monad/Request.hs
@@ -21,6 +21,7 @@ module Sound.SC3.Server.Monad.Request
, AllocT
-- * Resources
, Resource
+ , resource
, extract
, after
, after_
@@ -156,6 +157,10 @@ newtype Resource m a = Resource { extract :: ServerT m a -- ^ Extract result fro
}
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
-- received and return the deferred notification result.
after :: MonadIO m => Notification a -> AllocT m () -> RequestT m (Resource m a)
View
4 examples/hello.hs
@@ -5,7 +5,7 @@ import Sound.SC3.Server.Monad.Command
-- 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 (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.OpenSoundControl (immediately)
import qualified Sound.OpenSoundControl as OSC
@@ -33,7 +33,7 @@ main = run $ do
r <- rootNode
synth <- extract =<< immediately !> do
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
pauseThread 10
immediately !> s_release 0 synth

0 comments on commit 076cfcd

Please sign in to comment.