This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
439 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
OpenFeature/src/main/java/dev/openfeature/sdk/async/AsyncClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package dev.openfeature.sdk.async | ||
|
||
import dev.openfeature.sdk.OpenFeatureClient | ||
import dev.openfeature.sdk.Value | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.map | ||
|
||
interface AsyncClient { | ||
fun observeBooleanValue(key: String, default: Boolean): Flow<Boolean> | ||
fun observeIntValue(key: String, default: Int): Flow<Int> | ||
fun observeStringValue(key: String, default: String): Flow<String> | ||
fun observeDoubleValue(key: String, default: Double): Flow<Double> | ||
fun observeValue(key: String, default: Value): Flow<Value> | ||
} | ||
|
||
internal class AsyncClientImpl( | ||
private val client: OpenFeatureClient | ||
) : AsyncClient { | ||
private fun <T> observeEvents(callback: () -> T) = observeProviderReady() | ||
.map { callback() } | ||
.distinctUntilChanged() | ||
|
||
override fun observeBooleanValue(key: String, default: Boolean) = observeEvents { | ||
client.getBooleanValue(key, default) | ||
} | ||
|
||
override fun observeIntValue(key: String, default: Int) = observeEvents { | ||
client.getIntegerValue(key, default) | ||
} | ||
|
||
override fun observeStringValue(key: String, default: String) = observeEvents { | ||
client.getStringValue(key, default) | ||
} | ||
|
||
override fun observeDoubleValue(key: String, default: Double): Flow<Double> = observeEvents { | ||
client.getDoubleValue(key, default) | ||
} | ||
|
||
override fun observeValue(key: String, default: Value): Flow<Value> = observeEvents { | ||
client.getObjectValue(key, default) | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
OpenFeature/src/main/java/dev/openfeature/sdk/async/Extensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package dev.openfeature.sdk.async | ||
|
||
import dev.openfeature.sdk.OpenFeatureClient | ||
import dev.openfeature.sdk.events.EventHandler | ||
import dev.openfeature.sdk.events.OpenFeatureEvents | ||
import dev.openfeature.sdk.events.observe | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.cancel | ||
import kotlinx.coroutines.flow.onStart | ||
import kotlinx.coroutines.flow.take | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
|
||
fun OpenFeatureClient.toAsync(): AsyncClient { | ||
return AsyncClientImpl(this) | ||
} | ||
|
||
internal fun observeProviderReady() = EventHandler.eventsObserver() | ||
.observe<OpenFeatureEvents.ProviderReady>() | ||
.onStart { | ||
if (EventHandler.providerStatus().isProviderReady()) { | ||
this.emit(OpenFeatureEvents.ProviderReady) | ||
} | ||
} | ||
|
||
suspend fun awaitProviderReady() = suspendCancellableCoroutine { continuation -> | ||
val coroutineScope = CoroutineScope(Dispatchers.IO) | ||
coroutineScope.launch { | ||
observeProviderReady() | ||
.take(1) | ||
.collect { | ||
continuation.resumeWith(Result.success(Unit)) | ||
} | ||
} | ||
|
||
coroutineScope.launch { | ||
EventHandler.eventsObserver() | ||
.observe<OpenFeatureEvents.ProviderError>() | ||
.take(1) | ||
.collect { | ||
continuation.resumeWith(Result.failure(it.error)) | ||
} | ||
} | ||
|
||
continuation.invokeOnCancellation { | ||
coroutineScope.cancel() | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
OpenFeature/src/main/java/dev/openfeature/sdk/events/EventHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package dev.openfeature.sdk.events | ||
|
||
import kotlinx.coroutines.CoroutineDispatcher | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.cancelChildren | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.filterIsInstance | ||
import kotlinx.coroutines.launch | ||
import kotlin.reflect.KClass | ||
|
||
interface ProviderStatus { | ||
fun isProviderReady(): Boolean | ||
} | ||
|
||
interface EventObserver { | ||
fun <T : OpenFeatureEvents> observe(kClass: KClass<T>): Flow<T> | ||
} | ||
|
||
interface EventsPublisher { | ||
fun publish(event: OpenFeatureEvents) | ||
} | ||
|
||
inline fun <reified T : OpenFeatureEvents> EventObserver.observe() = observe(T::class) | ||
|
||
class EventHandler(dispatcher: CoroutineDispatcher) : EventObserver, EventsPublisher, ProviderStatus { | ||
private val sharedFlow: MutableSharedFlow<OpenFeatureEvents> = MutableSharedFlow() | ||
private val isProviderReady = MutableStateFlow(false) | ||
private val job = Job() | ||
private val coroutineScope = CoroutineScope(job + dispatcher) | ||
|
||
init { | ||
coroutineScope.launch { | ||
sharedFlow.collect { | ||
when (it) { | ||
is OpenFeatureEvents.ProviderReady -> isProviderReady.value = true | ||
is OpenFeatureEvents.ProviderShutDown -> { | ||
isProviderReady.value = false | ||
job.cancelChildren() | ||
} | ||
else -> { | ||
// do nothing | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun publish(event: OpenFeatureEvents) { | ||
coroutineScope.launch { | ||
sharedFlow.emit(event) | ||
} | ||
} | ||
|
||
override fun <T : OpenFeatureEvents> observe(kClass: KClass<T>): Flow<T> = sharedFlow | ||
.filterIsInstance(kClass) | ||
|
||
override fun isProviderReady(): Boolean { | ||
return isProviderReady.value | ||
} | ||
|
||
companion object { | ||
@Volatile | ||
private var instance: EventHandler? = null | ||
|
||
private fun getInstance(dispatcher: CoroutineDispatcher) = | ||
instance ?: synchronized(this) { | ||
instance ?: create(dispatcher).also { instance = it } | ||
} | ||
|
||
fun eventsObserver(dispatcher: CoroutineDispatcher = Dispatchers.IO): EventObserver = | ||
getInstance(dispatcher) | ||
internal fun providerStatus(dispatcher: CoroutineDispatcher = Dispatchers.IO): ProviderStatus = | ||
getInstance(dispatcher) | ||
fun eventsPublisher(dispatcher: CoroutineDispatcher = Dispatchers.IO): EventsPublisher = | ||
getInstance(dispatcher) | ||
|
||
private fun create(dispatcher: CoroutineDispatcher) = EventHandler(dispatcher) | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
OpenFeature/src/main/java/dev/openfeature/sdk/events/OpenFeatureEvents.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package dev.openfeature.sdk.events | ||
|
||
sealed class OpenFeatureEvents { | ||
object ProviderReady : OpenFeatureEvents() | ||
object ProviderConfigurationChanged : OpenFeatureEvents() | ||
data class ProviderError(val error: Throwable) : OpenFeatureEvents() | ||
object ProviderStale : OpenFeatureEvents() | ||
object ProviderShutDown : OpenFeatureEvents() | ||
} |
Oops, something went wrong.