lila/modules/common/src/main/paginator/Adapter.scala

42 lines
968 B
Scala

package lila.common
package paginator
import play.api.libs.concurrent.Execution.Implicits._
trait AdapterLike[A] {
/**
* Returns the total number of results.
*/
def nbResults: Fu[Int]
/**
* Returns a slice of the results.
*
* @param offset The number of elements to skip, starting from zero
* @param length The maximum number of elements to return
*/
def slice(offset: Int, length: Int): Fu[Seq[A]]
/**
* FUNCTOR INTERFACE
*/
def map[B](f: A B): AdapterLike[B] = new AdapterLike[B] {
def nbResults = AdapterLike.this.nbResults
def slice(offset: Int, length: Int) =
AdapterLike.this.slice(offset, length) map2 f
}
def mapFuture[B](f: A Fu[B]): AdapterLike[B] = new AdapterLike[B] {
def nbResults = AdapterLike.this.nbResults
def slice(offset: Int, length: Int) =
AdapterLike.this.slice(offset, length) flatMap { results
results.map(f).sequence
}
}
}