@@ -15,22 +15,16 @@ module Strands.Context
1515 , Location(..)
1616 , getLocation
1717 , setLocation
18− , getData
19− , setData
20− , modifyData
21− , delData
2218 )
2319where
2420
2521import Control.Concurrent (ThreadId)
2622import Control.Concurrent.STM (TChan)
2723import Control.Exception (SomeException)
28−import Control.Monad.State (MonadState, gets, modify, StateT,
29− MonadPlus(..), MonadIO(..))
24+import Control.Monad.State (StateT, MonadPlus(..), MonadIO(..))
3025import qualified Control.Monad.Trans.State.Lazy as Lazy (get, gets, modify, put)
31−import Data.Dynamic (TypeRep, Typeable, typeOf)
26+import Data.Dynamic (Typeable)
3227import Data.IORef (IORef)
33−import qualified Data.Map as M
3428import Data.Maybe (fromJust)
3529import Unsafe.Coerce (unsafeCoerce)
3630import GHC.Prim (Any)
@@ -72,7 +66,7 @@ data Context = Context
7266 -- User state
7367 ---------------------------------------------------------------------------
7468
75− , mfData :: M.Map TypeRep Any -- untyped, type coerced
69+ -- , mfData :: M.Map TypeRep Any -- untyped, type coerced
7670 -- ^ State data accessed with get or put operations
7771
7872 ---------------------------------------------------------------------------
@@ -141,7 +135,7 @@ initContext x childChan pending credit finalizer results =
141135 , currentm = unsafeCoerce x
142136 , fstack = [unsafeCoerce finalizer]
143137 , location = Worker
144− , mfData = mempty
138+ -- , mfData = mempty
145139 , parentChannel = Nothing
146140 , childChannel = unsafeCoerce childChan
147141 , pendingThreads = pending
@@ -240,37 +234,3 @@ getCtxResultDest :: Context -> Either (TChan (ChildEvent a)) (IORef [a])
240234getCtxResultDest ctx =
241235 maybe (Right $ unsafeCoerce $ fromJust $ accumResults ctx)
242236 (Left . unsafeCoerce) (parentChannel ctx)
243−
244−------------------------------------------------------------------------------
245−-- * Extensible State: Session Data Management
246−------------------------------------------------------------------------------
247−
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)
251−getData = resp
252− where resp = do
253− list <- gets mfData
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
259−
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) }
263− where t = typeOf x
264−
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
273− Nothing -> Nothing
274−
275−delData :: (MonadState Context m, Typeable a) => a -> m ()
276−delData x = modify $ \st -> st { mfData = M.delete (typeOf x) (mfData st) }