Skip to content

Commit

Permalink
docs: Update Events docs and signature (#90)
Browse files Browse the repository at this point in the history
Signed-off-by: Fabrizio Demaria <[email protected]>
Signed-off-by: Fabrizio Demaria <[email protected]>
Co-authored-by: Nicklas Lundin <[email protected]>
  • Loading branch information
fabriziodemaria and nicklasl authored Jan 15, 2024
1 parent 466115b commit 9ab5006
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 12 deletions.
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ dependencies {
### Usage

```kotlin
// configure a provider and get client
coroutineScope.launch(Dispatchers.IO) {
// configure a provider, wait for it to complete its initialization tasks
OpenFeatureAPI.setProviderAndWait(customProvider)
val client = OpenFeatureAPI.getClient()

// get a bool flag value
// get a bool flag value
client.getBooleanValue("boolFlag", default = false)
}
```
Expand All @@ -74,7 +74,7 @@ coroutineScope.launch(Dispatchers.IO) {
|| [Hooks](#hooks) | Add functionality to various stages of the flag evaluation life-cycle. |
|| Logging | Integrate with popular logging packages. |
|| Named clients | Utilize multiple providers in a single application. |
| | [Eventing](#eventing) | React to state changes in the provider or flag management system. |
| | [Eventing](#eventing) | React to state changes in the provider or flag management system. |
|| [Shutdown](#shutdown) | Gracefully clean up a provider during application shutdown. |
| ⚠️ | [Extending](#extending) | Extend OpenFeature with custom providers and hooks. |

Expand All @@ -89,9 +89,11 @@ If the provider you're looking for hasn't been created yet, see the [develop a p
Once you've added a provider as a dependency, it can be registered with OpenFeature like this:

```kotlin
OpenFeatureAPI.setProvider(MyProvider())
OpenFeatureAPI.setProviderAndWait(MyProvider())
```

_(Asynchronous API that doesn't wait is also available)_


### Targeting

Expand Down Expand Up @@ -137,13 +139,28 @@ 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.

Example usage:
```kotlin
OpenFeatureAPI.observeEvents<OpenFeatureEvents.ProviderReady>()
?.collect {
// do something once the provider is ready
}
viewModelScope.launch {
OpenFeatureAPI.observe<OpenFeatureEvents.ProviderReady>().collect {
println(">> ProviderReady event received")
}
}

viewModelScope.launch {
OpenFeatureAPI.setProviderAndWait(
ConfidenceFeatureProvider.create(
applicationContext,
clientSecret
),
Dispatchers.IO,
myEvaluationContext
)
}
```

<!-- (It's only possible to observe events from the global `OpenFeatureAPI`, until multiple providers are supported) -->

### Shutdown

The OpenFeature API provides a close function to perform a cleanup of the registered provider.
Expand Down Expand Up @@ -210,6 +227,13 @@ class NewProvider(override val hooks: List<Hook<*>>, override val metadata: Meta
// add necessary changes on context change
}

override fun observe(): Flow<OpenFeatureEvents> {
// return a `Flow` of the Events
}

override fun getProviderStatus(): OpenFeatureEvents {
// return the event representative of the current Provider Status
}
}
```

Expand Down
4 changes: 2 additions & 2 deletions android/src/main/java/dev/openfeature/sdk/async/Extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ internal fun FeatureProvider.observeProviderReady() = observe<OpenFeatureEvents.
Observe events from currently configured Provider.
*/
@OptIn(ExperimentalCoroutinesApi::class)
fun OpenFeatureAPI.observeEvents(): Flow<OpenFeatureEvents> {
internal inline fun <reified T : OpenFeatureEvents> OpenFeatureAPI.observe(): Flow<T> {
return sharedProvidersFlow.flatMapLatest { provider ->
provider.observe()
provider.observe<T>()
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package dev.openfeature.sdk

import dev.openfeature.sdk.async.observeEvents
import dev.openfeature.sdk.async.observe
import dev.openfeature.sdk.async.setProviderAndWait
import dev.openfeature.sdk.events.OpenFeatureEvents
import dev.openfeature.sdk.exceptions.ErrorCode
import dev.openfeature.sdk.helpers.AlwaysBrokenProvider
import dev.openfeature.sdk.helpers.DoSomethingProvider
Expand Down Expand Up @@ -95,7 +96,7 @@ class DeveloperExperienceTests {
val dispatcher = StandardTestDispatcher(testScheduler)
var eventCount = 0
CoroutineScope(dispatcher).launch {
OpenFeatureAPI.observeEvents().collect {
OpenFeatureAPI.observe<OpenFeatureEvents.ProviderReady>().collect {
eventCount++
}
}
Expand Down

0 comments on commit 9ab5006

Please sign in to comment.