Skip to content

Commit

Permalink
make the setting provider awaiting
Browse files Browse the repository at this point in the history
Signed-off-by: vahid torkaman <[email protected]>
  • Loading branch information
vahidlazio committed Dec 15, 2023
1 parent a5991cb commit cd34e5e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
22 changes: 11 additions & 11 deletions OpenFeature/src/main/java/dev/openfeature/sdk/async/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ fun OpenFeatureClient.toAsync(): AsyncClient? {
}
}

suspend fun OpenFeatureAPI.setProviderAndWait(
provider: FeatureProvider,
dispatcher: CoroutineDispatcher
) {
setProvider(provider)
provider.awaitReady(dispatcher)
}

internal fun FeatureProvider.observeProviderReady() = observe<OpenFeatureEvents.ProviderReady>()
.onStart {
if (isProviderReady()) {
this.emit(OpenFeatureEvents.ProviderReady)
}
}

suspend fun OpenFeatureAPI.awaitProviderReady(
dispatcher: CoroutineDispatcher = Dispatchers.IO
) {
val provider = getProvider()
requireNotNull(provider)
return provider.awaitProviderReady(dispatcher)
}

fun OpenFeatureAPI.observeEvents(): Flow<OpenFeatureEvents>? {
return getProvider()?.observe()
inline fun <reified T : OpenFeatureEvents> OpenFeatureAPI.observeEvents(): Flow<T>? {
return getProvider()?.observe<T>()
}

suspend fun FeatureProvider.awaitProviderReady(
suspend fun FeatureProvider.awaitReady(
dispatcher: CoroutineDispatcher = Dispatchers.IO
) = suspendCancellableCoroutine { continuation ->
val coroutineScope = CoroutineScope(dispatcher)
Expand Down
19 changes: 6 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,12 @@ This will get a build from the head of the mentioned branch.

```kotlin
// configure a provider and get client
OpenFeatureAPI.setProvider(customProvider)
val client = OpenFeatureAPI.getClient()
coroutineScope.launch(Dispatchers.IO) {
OpenFeatureAPI.setProviderAndWait(customProvider)
val client = OpenFeatureAPI.getClient()

// get a bool flag value
client.getBooleanValue("boolFlag", default = false)

// get a bool flag after "ready" signal from provider
coroutineScope.launch {
WithContext(Dispatchers.IO) {
client.awaitProviderReady()
}
client.getBooleanValue("boolFlag", default = false)
client.getBooleanValue("boolFlag", default = false)
}
```

Expand Down Expand Up @@ -163,9 +157,8 @@ Some providers support additional events, such as `PROVIDER_CONFIGURATION_CHANGE
Please refer to the documentation of the provider you're using to see what events are supported.

```kotlin
OpenFeatureAPI.eventsObserver()
.observe<OpenFeatureEvents.ProviderReady>()
.collect {
OpenFeatureAPI.observeEvents<OpenFeatureEvents.ProviderReady>()
?.collect {
// do something once the provider is ready
}
```
Expand Down

0 comments on commit cd34e5e

Please sign in to comment.