All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.algolia.client.extensions.internal.Iterable.kt Maven / Gradle / Ivy

Go to download

"Algolia is a powerful search-as-a-service solution, made easy to use with API clients, UI libraries, and pre-built integrations. Algolia API Client for Kotlin lets you easily use the Algolia Search REST API from your JVM project, such as Android or backend implementations."

There is a newer version: 3.12.2
Show newest version
package com.algolia.client.extensions.internal

import com.algolia.client.exception.AlgoliaIterableException
import kotlinx.coroutines.delay
import kotlin.time.Duration

public data class IterableError(
  public val validate: (T) -> Boolean,
  public val message: ((T) -> String)? = null
)

public suspend fun  createIterable(
  execute: suspend (T?) -> T,
  validate: (T) -> Boolean,
  aggregator: ((T) -> Unit)? = null,
  timeout: () -> Duration = { Duration.ZERO },
  error: IterableError? = null
): T {
  suspend fun executor(previousResponse: T? = null): T {
    val response = execute(previousResponse)

    if (aggregator != null) {
      aggregator(response)
    }

    if (validate(response)) {
      return response
    }

    if (error != null && error.validate(response)) {
      val message = error.message?.invoke(response) ?: "An error occurred"
      throw AlgoliaIterableException(message)
    }

    delay(timeout().inWholeMilliseconds)

    return executor(response)
  }

  return executor()
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy