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

net.devslash.data.ForDuration.kt Maven / Gradle / Ivy

There is a newer version: 0.26.2
Show newest version
package net.devslash.data

import net.devslash.*
import java.time.Duration
import java.time.Instant

class ForDuration(private val duration: Duration, private val supplier: () -> T, private val clazz: Class) : CallDecorator, RequestDataSupplier, LifecycleController {

  companion object {
    inline operator fun  invoke(duration: Duration, noinline supplier: () -> T): ForDuration {
      return ForDuration(duration, supplier, T::class.java)
    }
  }

  private var expireTime: Instant? = null

  override fun accept(call: Call): Call {
    return Call(call.url, call.urlProvider, 1, call.rateOptions, call.headers, call.type, this, call.body, call.onError, call.beforeHooks, call.afterHooks, this)
  }

  override fun getRequestExpiry(): Instant {
    return expireTime!!
  }

  override fun getRequestQueueDepth(): Int = 0

  override suspend fun getDataForRequest(): ListRequestData? {
    if (expireTime == null) {
      expireTime = Instant.now().plus(duration)
    } else if (Instant.now().isAfter(expireTime)) {
      return null
    }
    return ListRequestData(supplier.invoke(), clazz)
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy