@@ -1,4 +1,4 @@
1−{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, RankNTypes, GADTs, ScopedTypeVariables, FunctionalDependencies, RecursiveDo, UndecidableInstances, GeneralizedNewtypeDeriving, StandaloneDeriving, EmptyDataDecls, NoMonomorphismRestriction, TemplateHaskell, PolyKinds, TypeOperators, DeriveFunctor, LambdaCase, CPP, ForeignFunctionInterface, DeriveDataTypeable, ConstraintKinds #-}
1+{-# LANGUAGE TypeFamilies, FlexibleContexts, FlexibleInstances, MultiParamTypeClasses, RankNTypes, GADTs, ScopedTypeVariables, FunctionalDependencies, RecursiveDo, UndecidableInstances, GeneralizedNewtypeDeriving, StandaloneDeriving, EmptyDataDecls, NoMonomorphismRestriction, TemplateHaskell, PolyKinds, TypeOperators, DeriveFunctor, LambdaCase, CPP, ForeignFunctionInterface, DeriveDataTypeable, ConstraintKinds, DefaultSignatures #-}
22module Reflex.Dom.Class where
33
44import Prelude hiding (mapM, mapM_, sequence, concat)
@@ -13,6 +13,7 @@ import Data.Foldable
1313import Control.Monad.Ref
1414import Control.Monad.Reader hiding (mapM, mapM_, forM, forM_, sequence)
1515import Control.Monad.State hiding (mapM, mapM_, forM, forM_, sequence)
16+import qualified Control.Monad.State.Strict as Strict hiding (mapM, mapM_, forM, forM_, sequence)
1617import Data.Dependent.Sum (DSum (..))
1718import GHCJS.DOM.Types hiding (Event)
1819import GHCJS.DOM (WebView)
@@ -38,13 +39,18 @@ class ( Reflex t, MonadHold t m, MonadIO m, MonadAsyncException m, Functor m, Mo
3839 ) => MonadWidget t m | m -> t where
3940 type WidgetHost m :: * -> *
4041 type GuiAction m :: * -> *
42+ type WidgetOutput t m :: *
43+ type WidgetOutput t m = Event t (WidgetHost m ())
4144 askParent :: m Node
4245 subWidget :: Node -> m a -> m a
43− subWidgetWithVoidActions :: Node -> m a -> m (a, Event t (WidgetHost m ()))
46+ subWidgetWithVoidActions :: Node -> m a -> m (a, WidgetOutput t m)
4447 liftWidgetHost :: WidgetHost m a -> m a --TODO: Is this a good idea?
4548 schedulePostBuild :: WidgetHost m () -> m ()
4649 addVoidAction :: Event t (WidgetHost m ()) -> m ()
47− getRunWidget :: IsNode n => m (n -> m a -> WidgetHost m (a, WidgetHost m (), Event t (WidgetHost m ())))
50+ getRunWidget :: IsNode n => m (n -> m a -> WidgetHost m (a, WidgetHost m (), WidgetOutput t m))
51+ tellWidgetOutput :: Ord k => Dynamic t (Map k (WidgetOutput t m)) -> m ()
52+ default tellWidgetOutput :: (WidgetOutput t m ~ Event t (WidgetHost m ()), Ord k) => Dynamic t (Map k (WidgetOutput t m)) -> m ()
53+ tellWidgetOutput = addVoidAction . switch . fmap (mergeWith (>>) . toList) . current
4854
4955class Monad m => HasDocument m where
5056 askDocument :: m HTMLDocument
@@ -55,6 +61,9 @@ instance HasDocument m => HasDocument (ReaderT r m) where
5561instance HasDocument m => HasDocument (StateT r m) where
5662 askDocument = lift askDocument
5763
64+instance HasDocument m => HasDocument (Strict.StateT r m) where
65+ askDocument = lift askDocument
66+
5867class Monad m => HasWebView m where
5968 askWebView :: m WebView
6069
@@ -64,6 +73,9 @@ instance HasWebView m => HasWebView (ReaderT r m) where
6473instance HasWebView m => HasWebView (StateT r m) where
6574 askWebView = lift askWebView
6675
76+instance HasWebView m => HasWebView (Strict.StateT r m) where
77+ askWebView = lift askWebView
78+
6779newtype Restore m = Restore { restore :: forall a. m a -> IO a }
6880
6981class Monad m => MonadIORestore m where
@@ -89,9 +101,18 @@ instance HasPostGui t h m => HasPostGui t h (ReaderT r m) where
89101 askPostGui = lift askPostGui
90102 askRunWithActions = lift askRunWithActions
91103
104+instance HasPostGui t h m => HasPostGui t h (StateT r m) where
105+ askPostGui = lift askPostGui
106+ askRunWithActions = lift askRunWithActions
107+
108+instance HasPostGui t h m => HasPostGui t h (Strict.StateT r m) where
109+ askPostGui = lift askPostGui
110+ askRunWithActions = lift askRunWithActions
111+
92112instance MonadWidget t m => MonadWidget t (ReaderT r m) where
93113 type WidgetHost (ReaderT r m) = WidgetHost m
94114 type GuiAction (ReaderT r m) = GuiAction m
115+ type WidgetOutput t (ReaderT r m) = WidgetOutput t m
95116 askParent = lift askParent
96117 subWidget n w = do
97118 r <- ask
@@ -108,6 +129,7 @@ instance MonadWidget t m => MonadWidget t (ReaderT r m) where
108129 return $ \rootElement w -> do
109130 (a, postBuild, voidActions) <- runWidget rootElement $ runReaderT w r
110131 return (a, postBuild, voidActions)
132+ tellWidgetOutput = lift . tellWidgetOutput
111133
112134performEvent_ :: MonadWidget t m => Event t (WidgetHost m ()) -> m ()
113135performEvent_ = addVoidAction