@@ -156,6 +156,7 @@ module Reflex.Class
156156 -- * Deprecated functions
157157 , switchPromptly
158158 , switchPromptOnly
159+ , buildDynamic
159160 -- * "Cheap" functions
160161 , fmapMaybeCheap
161162 , mapMaybeCheap
@@ -388,6 +389,11 @@ instance (Reflex t, Default a) => Default (Dynamic t a) where
388389class (Applicative m , Monad m ) => MonadSample t m | m -> t where
389390 -- | Get the current value in the 'Behavior'
390391 sample :: Behavior t a -> m a
392+ {-# INLINABLE sample #-}
393+ default sample :: (m ~ f m' , MonadTrans f , MonadSample t m' ) => Behavior t a -> m a
394+ sample = lift . sample
395+
396+ {-# DEPRECATED buildDynamic "Sample directly and use 'liftPusmM'" #-}
391397
392398-- | 'MonadHold' designates monads that can create new 'Behavior's based on
393399-- 'Event's; usually this will be 'PushM' or a monad based on it. 'MonadHold'
@@ -400,33 +406,50 @@ class MonadSample t m => MonadHold t m where
400406 -- the 'Behavior', it will see the __old__ value of the 'Behavior', not the new
401407 -- one.
402408 hold :: a -> Event t a -> m (Behavior t a )
409+ {-# INLINABLE hold #-}
403410 default hold :: (m ~ f m' , MonadTrans f , MonadHold t m' ) => a -> Event t a -> m (Behavior t a )
404411 hold v0 = lift . hold v0
405412 -- | Create a 'Dynamic' value using the given initial value that changes every
406413 -- time the 'Event' occurs.
407414 holdDyn :: a -> Event t a -> m (Dynamic t a )
415+ {-# INLINABLE holdDyn #-}
408416 default holdDyn :: (m ~ f m' , MonadTrans f , MonadHold t m' ) => a -> Event t a -> m (Dynamic t a )
409417 holdDyn v0 = lift . holdDyn v0
410418 -- | Create an 'Incremental' value using the given initial value that changes
411419 -- every time the 'Event' occurs.
412420 holdIncremental :: Patch p => PatchTarget p -> Event t p -> m (Incremental t p )
421+ {-# INLINABLE holdIncremental #-}
413422 default holdIncremental :: (Patch p , m ~ f m' , MonadTrans f , MonadHold t m' ) => PatchTarget p -> Event t p -> m (Incremental t p )
414423 holdIncremental v0 = lift . holdIncremental v0
424+ -- | DEPRECATED. This function allowed for extra laziness when sampling
425+ -- behaviors, but is no longer needed. When implementing MonadHold use the new
426+ -- 'liftPushM' instead.
415427 buildDynamic :: PushM t a -> Event t a -> m (Dynamic t a )
416- {-
417- default buildDynamic :: (m ~ f m', MonadTrans f, MonadHold t m') => PullM t a -> Event t a -> m (Dynamic t a)
418- buildDynamic getV0 = lift . buildDynamic getV0
419- -}
428+ buildDynamic initialValue e = do
429+ iv <- liftPushM initialValue
430+ holdDyn iv e
420431 -- | Create a new 'Event' that only occurs only once, on the first occurrence of
421432 -- the supplied 'Event'.
422433 headE :: Event t a -> m (Event t a )
434+ {-# INLINABLE headE #-}
435+ default headE :: (m ~ f m' , MonadTrans f , MonadHold t m' ) => Event t a -> m (Event t a )
436+ headE = lift . headE
423437 -- | An event which only occurs at the current moment in time, such that:
424438 --
425439 -- > coincidence (pushAlways (\a -> (a <$) <$> now) e) = e
426440 --
427441 now :: m (Event t () )
442+ {-# INLINABLE now #-}
428443 default now :: (m ~ f m' , MonadTrans f , MonadHold t m' ) => m (Event t () )
429444 now = lift now
445+ {-# INLINABLE liftPushM #-}
446+ liftPushM :: PushM t a -> m a
447+ {- -- When buildDynamic has been removed this can be the new default:
448+ default liftPushM :: (m ~ f m', MonadTrans f, MonadHold t m') => PushM t a -> m a
449+ liftPushM = lift . liftPushM
450+ -}
451+ default liftPushM :: (Reflex t ) => PushM t a -> m a
452+ liftPushM m = sample . current =<< buildDynamic m never
430453
431454-- | Accumulate an 'Incremental' with the supplied initial value and the firings of the provided 'Event',
432455-- using the combining function to produce a patch.
@@ -570,9 +593,9 @@ instance MonadHold t m => MonadHold t (ReaderT r m) where
570593 hold a0 = lift . hold a0
571594 holdDyn a0 = lift . holdDyn a0
572595 holdIncremental a0 = lift . holdIncremental a0
573- buildDynamic a0 = lift . buildDynamic a0
574596 headE = lift . headE
575597 now = lift now
598+ liftPushM = lift . liftPushM
576599
577600instance (MonadSample t m , Monoid r ) => MonadSample t (WriterT r m ) where
578601 sample = lift . sample
@@ -581,9 +604,9 @@ instance (MonadHold t m, Monoid r) => MonadHold t (WriterT r m) where
581604 hold a0 = lift . hold a0
582605 holdDyn a0 = lift . holdDyn a0
583606 holdIncremental a0 = lift . holdIncremental a0
584- buildDynamic a0 = lift . buildDynamic a0
585607 headE = lift . headE
586608 now = lift now
609+ liftPushM = lift . liftPushM
587610
588611instance MonadSample t m => MonadSample t (StateT s m ) where
589612 sample = lift . sample
@@ -592,9 +615,9 @@ instance MonadHold t m => MonadHold t (StateT s m) where
592615 hold a0 = lift . hold a0
593616 holdDyn a0 = lift . holdDyn a0
594617 holdIncremental a0 = lift . holdIncremental a0
595- buildDynamic a0 = lift . buildDynamic a0
596618 headE = lift . headE
597619 now = lift now
620+ liftPushM = lift . liftPushM
598621
599622instance MonadSample t m => MonadSample t (ExceptT e m ) where
600623 sample = lift . sample
@@ -603,9 +626,9 @@ instance MonadHold t m => MonadHold t (ExceptT e m) where
603626 hold a0 = lift . hold a0
604627 holdDyn a0 = lift . holdDyn a0
605628 holdIncremental a0 = lift . holdIncremental a0
606- buildDynamic a0 = lift . buildDynamic a0
607629 headE = lift . headE
608630 now = lift now
631+ liftPushM = lift . liftPushM
609632
610633instance (MonadSample t m , Monoid w ) => MonadSample t (RWST r w s m ) where
611634 sample = lift . sample
@@ -614,9 +637,9 @@ instance (MonadHold t m, Monoid w) => MonadHold t (RWST r w s m) where
614637 hold a0 = lift . hold a0
615638 holdDyn a0 = lift . holdDyn a0
616639 holdIncremental a0 = lift . holdIncremental a0
617- buildDynamic a0 = lift . buildDynamic a0
618640 headE = lift . headE
619641 now = lift now
642+ liftPushM = lift . liftPushM
620643
621644instance MonadSample t m => MonadSample t (ContT r m ) where
622645 sample = lift . sample
@@ -625,9 +648,9 @@ instance MonadHold t m => MonadHold t (ContT r m) where
625648 hold a0 = lift . hold a0
626649 holdDyn a0 = lift . holdDyn a0
627650 holdIncremental a0 = lift . holdIncremental a0
628- buildDynamic a0 = lift . buildDynamic a0
629651 headE = lift . headE
630652 now = lift now
653+ liftPushM = lift . liftPushM
631654
632655--------------------------------------------------------------------------------
633656-- Convenience functions
0 commit comments