@@ -15,22 +15,16 @@ module Strands.Context
15 , Location(..)
15 , Location(..)
16 , getLocation
16 , getLocation
17 , setLocation
17 , setLocation
25import Control.Concurrent (ThreadId)
21import Control.Concurrent (ThreadId)
26import Control.Concurrent.STM (TChan)
22import Control.Concurrent.STM (TChan)
27import Control.Exception (SomeException)
23import Control.Exception (SomeException)
28−import Control.Monad.State (MonadState, gets, modify, StateT,
24+import Control.Monad.State (StateT, MonadPlus(..), MonadIO(..))
29− MonadPlus(..), MonadIO(..))
30import qualified Control.Monad.Trans.State.Lazy as Lazy (get, gets, modify, put)
25import qualified Control.Monad.Trans.State.Lazy as Lazy (get, gets, modify, put)
31−import Data.Dynamic (TypeRep, Typeable, typeOf)
26+import Data.Dynamic (Typeable)
32import Data.IORef (IORef)
27import Data.IORef (IORef)
33−import qualified Data.Map as M
34import Data.Maybe (fromJust)
28import Data.Maybe (fromJust)
35import Unsafe.Coerce (unsafeCoerce)
29import Unsafe.Coerce (unsafeCoerce)
36import GHC.Prim (Any)
30import GHC.Prim (Any)
@@ -72,7 +66,7 @@ data Context = Context
72 -- User state
66 -- User state
73 ---------------------------------------------------------------------------
67 ---------------------------------------------------------------------------
75− , mfData :: M.Map TypeRep Any -- untyped, type coerced
69+ -- , mfData :: M.Map TypeRep Any -- untyped, type coerced
76 -- ^ State data accessed with get or put operations
70 -- ^ State data accessed with get or put operations
78 ---------------------------------------------------------------------------
72 ---------------------------------------------------------------------------
@@ -141,7 +135,7 @@ initContext x childChan pending credit finalizer results =
141 , currentm = unsafeCoerce x
135 , currentm = unsafeCoerce x
142 , fstack = [unsafeCoerce finalizer]
136 , fstack = [unsafeCoerce finalizer]
143 , location = Worker
137 , location = Worker
144− , mfData = mempty
138+ -- , mfData = mempty
145 , parentChannel = Nothing
139 , parentChannel = Nothing
146 , childChannel = unsafeCoerce childChan
140 , childChannel = unsafeCoerce childChan
147 , pendingThreads = pending
141 , pendingThreads = pending
@@ -240,37 +234,3 @@ getCtxResultDest :: Context -> Either (TChan (ChildEvent a)) (IORef [a])
240getCtxResultDest ctx =
234getCtxResultDest ctx =
241 maybe (Right $ unsafeCoerce $ fromJust $ accumResults ctx)
235 maybe (Right $ unsafeCoerce $ fromJust $ accumResults ctx)
242 (Left . unsafeCoerce) (parentChannel ctx)
236 (Left . unsafeCoerce) (parentChannel ctx)
244−------------------------------------------------------------------------------
245−-- * Extensible State: Session Data Management
246−------------------------------------------------------------------------------
248−-- | Same as 'getSData' but with a more general type. If the data is found, a
249−-- 'Just' value is returned. Otherwise, a 'Nothing' value is returned.
250−getData :: (MonadState Context m, Typeable a) => m (Maybe a)
254− case M.lookup (typeOf $ typeResp resp) list of
255− Just x -> return . Just $ unsafeCoerce x
256− Nothing -> return Nothing
257− typeResp :: m (Maybe x) -> x
258− typeResp = undefined
260−-- | Same as 'setSData' but with a more general type.
261−setData :: (MonadState Context m, Typeable a) => a -> m ()
262−setData x = modify $ \st -> st { mfData = M.insert t (unsafeCoerce x) (mfData st) }
265−modifyData :: (MonadState Context m, Typeable a) => (Maybe a -> Maybe a) -> m ()
266−modifyData f = modify $ \st -> st { mfData = M.alter alterf t (mfData st) }
267− where typeResp :: (Maybe a -> b) -> a
268− typeResp = undefined
269− t = typeOf (typeResp f)
270− alterf mx = unsafeCoerce $ f x'
271− where x' = case mx of
272− Just x -> Just $ unsafeCoerce x
275−delData :: (MonadState Context m, Typeable a) => a -> m ()
276−delData x = modify $ \st -> st { mfData = M.delete (typeOf x) (mfData st) }