@@ -749,12 +749,27 @@ 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+ if null items
770+ then codeGen defaultChunk
771+ else foldMapM (\ item -> putPromise promise item >>
772+ codeGen chunks) items
758773
759774
760775------------------------------------------------------------------------------
@@ -773,6 +788,23 @@ defer pf n = do
773788 return $ action `mappend` res
774789
775790
791+ ------------------------------------------------------------------------------
792+ -- | Much like 'either', takes a runtime computation and branches to the
793+ -- respective splice depending on the runtime value.
794+ deferEither :: Monad n
795+ => (RuntimeSplice n a -> Splice n )
796+ -> (RuntimeSplice n b -> Splice n )
797+ -> RuntimeSplice n (Either a b ) -> Splice n
798+ deferEither pfa pfb n = do
799+ pa <- newEmptyPromise
800+ pb <- newEmptyPromise
801+ failureChunk <- pfa $ getPromise pa
802+ successChunk <- pfb $ getPromise pb
803+ return $ yieldRuntime $ n >>= either
804+ (\ x -> putPromise pa x >> codeGen failureChunk)
805+ (\ x -> putPromise pb x >> codeGen successChunk)
806+
807+
776808------------------------------------------------------------------------------
777809-- | A version of defer which applies a function on the runtime value.
778810deferMap :: Monad n
0 commit comments