Skip to content

Commit 9ab5006

Browse files
docs: Update Events docs and signature (#90)
Signed-off-by: Fabrizio Demaria <[email protected]> Signed-off-by: Fabrizio Demaria <[email protected]> Co-authored-by: Nicklas Lundin <[email protected]>
1 parent 466115b commit 9ab5006

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

README.md

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ dependencies {
5555
### Usage
5656

5757
```kotlin
58-
// configure a provider and get client
5958
coroutineScope.launch(Dispatchers.IO) {
59+
// configure a provider, wait for it to complete its initialization tasks
6060
OpenFeatureAPI.setProviderAndWait(customProvider)
6161
val client = OpenFeatureAPI.getClient()
6262

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

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

9191
```kotlin
92-
OpenFeatureAPI.setProvider(MyProvider())
92+
OpenFeatureAPI.setProviderAndWait(MyProvider())
9393
```
9494

95+
_(Asynchronous API that doesn't wait is also available)_
96+
9597

9698
### Targeting
9799

@@ -137,13 +139,28 @@ Some providers support additional events, such as `PROVIDER_CONFIGURATION_CHANGE
137139

138140
Please refer to the documentation of the provider you're using to see what events are supported.
139141

142+
Example usage:
140143
```kotlin
141-
OpenFeatureAPI.observeEvents<OpenFeatureEvents.ProviderReady>()
142-
?.collect {
143-
// do something once the provider is ready
144-
}
144+
viewModelScope.launch {
145+
OpenFeatureAPI.observe<OpenFeatureEvents.ProviderReady>().collect {
146+
println(">> ProviderReady event received")
147+
}
148+
}
149+
150+
viewModelScope.launch {
151+
OpenFeatureAPI.setProviderAndWait(
152+
ConfidenceFeatureProvider.create(
153+
applicationContext,
154+
clientSecret
155+
),
156+
Dispatchers.IO,
157+
myEvaluationContext
158+
)
159+
}
145160
```
146161

162+
<!-- (It's only possible to observe events from the global `OpenFeatureAPI`, until multiple providers are supported) -->
163+
147164
### Shutdown
148165

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

230+
override fun observe(): Flow<OpenFeatureEvents> {
231+
// return a `Flow` of the Events
232+
}
233+
234+
override fun getProviderStatus(): OpenFeatureEvents {
235+
// return the event representative of the current Provider Status
236+
}
213237
}
214238
```
215239

android/src/main/java/dev/openfeature/sdk/async/Extensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ internal fun FeatureProvider.observeProviderReady() = observe<OpenFeatureEvents.
4848
Observe events from currently configured Provider.
4949
*/
5050
@OptIn(ExperimentalCoroutinesApi::class)
51-
fun OpenFeatureAPI.observeEvents(): Flow<OpenFeatureEvents> {
51+
internal inline fun <reified T : OpenFeatureEvents> OpenFeatureAPI.observe(): Flow<T> {
5252
return sharedProvidersFlow.flatMapLatest { provider ->
53-
provider.observe()
53+
provider.observe<T>()
5454
}
5555
}
5656

android/src/test/java/dev/openfeature/sdk/DeveloperExperienceTests.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package dev.openfeature.sdk
22

3-
import dev.openfeature.sdk.async.observeEvents
3+
import dev.openfeature.sdk.async.observe
44
import dev.openfeature.sdk.async.setProviderAndWait
5+
import dev.openfeature.sdk.events.OpenFeatureEvents
56
import dev.openfeature.sdk.exceptions.ErrorCode
67
import dev.openfeature.sdk.helpers.AlwaysBrokenProvider
78
import dev.openfeature.sdk.helpers.DoSomethingProvider
@@ -95,7 +96,7 @@ class DeveloperExperienceTests {
9596
val dispatcher = StandardTestDispatcher(testScheduler)
9697
var eventCount = 0
9798
CoroutineScope(dispatcher).launch {
98-
OpenFeatureAPI.observeEvents().collect {
99+
OpenFeatureAPI.observe<OpenFeatureEvents.ProviderReady>().collect {
99100
eventCount++
100101
}
101102
}

0 commit comments

Comments
 (0)