我正在使用返回分页资源的服务。它暴露一个单个呼叫,它是由下面的接口定义:使用分页的Iterable [T],就好像它是连续的Iterable [T]
trait Service {
getPage(pageSize: Int, pageCursor: String): AsyncPage[Resource]
}
的getPage
函数返回一个AsyncPage[T]
对象,这是这样实现的:
/**
* A page of contents that are retrieved asynchronously from their origin
*
* @param content The resource object
* @param nextPageCursor The token representing the next page, or empty if no more pages to consume
* @tparam T The type of resource withing the page
*/
case class AsyncPage[T](
val content: Future[Iterable[T]],
val nextPageCursor : Future[String]
) { }
的页面的内容被异步检索来自服务使用的任何存储系统。
由于我的应用程序的需要,我并不关心页面。我想编写一些代码,使我可以像使用单一的Iterable[T]
一样消耗服务的资源。
但是,我想保持服务的懒惰。我不想要求超过必要的页数。这意味着我不想要求下一个页面,直到我没有使用前一个元素。
每当我有一个消耗首页的整个Iterable[T]
,我想要的代码使用getPage(...)
功能,并提供从nextPageCursor
的最后一页的pageCursor
参数要求下页。
你能指导我如何做到这一点吗?
那么'Iterable [T]'会在这些'Future's块上呢?否则,我认为你可以做的最好的是'Iterable [Future [Iterable [T]]''。 –
我的应用程序基于Akka actor模型框架,该框架不鼓励阻止actor主题并告诉您纯粹异步工作 –