Skip to content

Commit

Permalink
Update Instance.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
hsinha610 authored Jul 30, 2024
1 parent 509ce2d commit 0e98451
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions src/main/kotlin/com/featurevisor/sdk/Instance.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package com.featurevisor.sdk
import com.featurevisor.sdk.FeaturevisorError.MissingDatafileOptions
import com.featurevisor.types.*
import com.featurevisor.types.EventName.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
Expand All @@ -18,7 +16,7 @@ typealias InterceptContext = (Context) -> Context
typealias DatafileFetchHandler = (datafileUrl: String) -> Result<DatafileContent>

var emptyDatafile = DatafileContent(
schemaVersion = "1",
schemaVersion = "1",
revision = "unknown",
attributes = emptyList(),
segments = emptyList(),
Expand Down Expand Up @@ -58,8 +56,6 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
internal var configureBucketKey = options.configureBucketKey
internal var configureBucketValue = options.configureBucketValue
internal var refreshJob: Job? = null
private var fetchJob: Job? = null
internal val coroutineScope = CoroutineScope(Dispatchers.IO)

init {
with(options) {
Expand Down Expand Up @@ -103,26 +99,17 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
}

datafileUrl != null -> {
datafileReader = DatafileReader(options.datafile ?: emptyDatafile)
fetchJob = fetchDatafileContentJob(
url = datafileUrl,
logger = logger,
handleDatafileFetch = handleDatafileFetch,
retryCount = retryCount.coerceAtLeast(1),
retryInterval = retryInterval.coerceAtLeast(0),
coroutineScope = coroutineScope,
) { result ->
result.onSuccess { fetchResult ->
val datafileContent = fetchResult.datafileContent
datafileReader = DatafileReader(datafileContent)
datafileReader = DatafileReader(options.datafile?: emptyDatafile)
fetchDatafileContent(datafileUrl, handleDatafileFetch) { result ->
if (result.isSuccess) {
datafileReader = DatafileReader(result.getOrThrow())
statuses.ready = true
emitter.emit(READY, datafileContent, fetchResult.responseBodyString)
emitter.emit(READY, result.getOrThrow())
if (refreshInterval != null) startRefreshing()
}.onFailure { error ->
logger?.error("Failed to fetch datafile: $error")
} else {
logger?.error("Failed to fetch datafile: $result")
emitter.emit(ERROR)
}
cancelFetchRetry()
}
}

Expand All @@ -131,12 +118,6 @@ class FeaturevisorInstance private constructor(options: InstanceOptions) {
}
}

// Provide a mechanism to cancel the fetch job if retry count is more than one
fun cancelFetchRetry() {
fetchJob?.cancel()
fetchJob = null
}

fun setLogLevels(levels: List<Logger.LogLevel>) {
this.logger?.setLevels(levels)
}
Expand Down

0 comments on commit 0e98451

Please sign in to comment.