module Wire.Sem.Paging
(
Page,
PagingState,
PagingBounds,
Paging (..),
withChunks,
SimplePaging,
)
where
import Data.Kind
import Imports
type family Page p a :: (page :: Type) | page -> p a
type family PagingState p a = (ps :: Type)
type family PagingBounds p a :: Type
class Paging p where
pageItems :: Page p a -> [a]
pageHasMore :: Page p a -> Bool
pageState :: Page p a -> PagingState p a
data SimplePaging
type instance Page SimplePaging a = [a]
type instance PagingState SimplePaging a = ()
type instance PagingBounds SimplePaging a = Int32
instance Paging SimplePaging where
pageItems :: forall a. Page SimplePaging a -> [a]
pageItems = [a] -> [a]
Page SimplePaging a -> [a]
forall a. a -> a
id
pageHasMore :: forall a. Page SimplePaging a -> Bool
pageHasMore Page SimplePaging a
_ = Bool
False
pageState :: forall a. Page SimplePaging a -> PagingState SimplePaging a
pageState Page SimplePaging a
_ = ()
withChunks ::
(Paging p, Monad m) =>
(Maybe (PagingState p i) -> m (Page p i)) ->
([i] -> m ()) ->
m ()
withChunks :: forall p (m :: * -> *) i.
(Paging p, Monad m) =>
(Maybe (PagingState p i) -> m (Page p i)) -> ([i] -> m ()) -> m ()
withChunks Maybe (PagingState p i) -> m (Page p i)
pager [i] -> m ()
action = do
Page p i
page <- Maybe (PagingState p i) -> m (Page p i)
pager Maybe (PagingState p i)
forall a. Maybe a
Nothing
Page p i -> m ()
go Page p i
page
where
go :: Page p i -> m ()
go Page p i
page = do
[i] -> m ()
action (Page p i -> [i]
forall p a. Paging p => Page p a -> [a]
forall a. Page p a -> [a]
pageItems Page p i
page)
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Page p i -> Bool
forall p a. Paging p => Page p a -> Bool
forall a. Page p a -> Bool
pageHasMore Page p i
page) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
Page p i
page' <- Maybe (PagingState p i) -> m (Page p i)
pager (PagingState p i -> Maybe (PagingState p i)
forall a. a -> Maybe a
Just (Page p i -> PagingState p i
forall p a. Paging p => Page p a -> PagingState p a
forall a. Page p a -> PagingState p a
pageState Page p i
page))
Page p i -> m ()
go Page p i
page'