@@ -749,12 +749,28 @@ deferMany :: (Foldable f, Monad n)
749749 => (RuntimeSplice n a -> Splice n )
750750 -> RuntimeSplice n (f a )
751751 -> Splice n
752- deferMany f getItems = do
752+ deferMany = deferManyElse $ return mempty
753+
754+
755+ ------------------------------------------------------------------------------
756+ -- | A version of 'deferMany' which has a default splice to run in the case
757+ -- when there are no elements in the given list.
758+ deferManyElse :: (Foldable f , Monad n )
759+ => Splice n
760+ -> (RuntimeSplice n a -> Splice n )
761+ -> RuntimeSplice n (f a )
762+ -> Splice n
763+ deferManyElse def f getItems = do
753764 promise <- newEmptyPromise
754765 chunks <- f $ getPromise promise
766+ defaultChunk <- def
755767 return $ yieldRuntime $ do
756768 items <- getItems
757- foldMapM (\ item -> putPromise promise item >> codeGen chunks) items
769+ -- Use this instead of null for compatibility with pre 4.8 base
770+ if foldr (\ _ _ -> False ) True items
771+ then codeGen defaultChunk
772+ else foldMapM (\ item -> putPromise promise item >>
773+ codeGen chunks) items
758774
759775
760776------------------------------------------------------------------------------
@@ -773,6 +789,23 @@ defer pf n = do
773789 return $ action `mappend` res
774790
775791
792+ ------------------------------------------------------------------------------
793+ -- | Much like 'either', takes a runtime computation and branches to the
794+ -- respective splice depending on the runtime value.
795+ deferEither :: Monad n
796+ => (RuntimeSplice n a -> Splice n )
797+ -> (RuntimeSplice n b -> Splice n )
798+ -> RuntimeSplice n (Either a b ) -> Splice n
799+ deferEither pfa pfb n = do
800+ pa <- newEmptyPromise
801+ pb <- newEmptyPromise
802+ failureChunk <- pfa $ getPromise pa
803+ successChunk <- pfb $ getPromise pb
804+ return $ yieldRuntime $ n >>= either
805+ (\ x -> putPromise pa x >> codeGen failureChunk)
806+ (\ x -> putPromise pb x >> codeGen successChunk)
807+
808+
776809------------------------------------------------------------------------------
777810-- | A version of defer which applies a function on the runtime value.
778811deferMap :: Monad n
0 commit comments