Skip to content

Commit 80173dc

Browse files
committed
fix: restore main API setEvaluationContext
1 parent 4eb640b commit 80173dc

File tree

2 files changed

+35
-4
lines changed

2 files changed

+35
-4
lines changed

android/src/main/java/dev/openfeature/sdk/OpenFeatureAPI.kt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import java.util.concurrent.CancellationException
1313
@Suppress("TooManyFunctions")
1414
object OpenFeatureAPI {
1515
private var setProviderJob: Deferred<Unit>? = null
16+
private var setEvaluationContextJob: Deferred<Unit>? = null
1617
private val NOOP_PROVIDER = NoOpProvider()
1718
private var provider: FeatureProvider = NOOP_PROVIDER
1819
private var context: EvaluationContext? = null
@@ -106,15 +107,44 @@ object OpenFeatureAPI {
106107
}
107108

108109
/**
109-
* Set the [EvaluationContext] for the SDK.
110+
* Set the [EvaluationContext] for the SDK. This method will block until the context is set and the provider is ready.
111+
*
112+
* If the new context is different compare to the old context, this will cause the provider to reconcile with the new context.
113+
* When the provider "Reconciles" it will set the status to [OpenFeatureStatus.Reconciling].
114+
* When the provider successfully reconciles it will set the status to [OpenFeatureStatus.Ready].
115+
* If the provider fails to reconcile it will set the status to [OpenFeatureStatus.Error].
116+
*
117+
* @param evaluationContext the [EvaluationContext] to set
118+
*/
119+
suspend fun setEvaluationContextAndWait(evaluationContext: EvaluationContext) {
120+
setEvaluationContextInternal(evaluationContext)
121+
}
122+
123+
/**
124+
* Set the [EvaluationContext] for the SDK. This method will return immediately and set the context in a coroutine scope.
125+
*
110126
* If the new context is different compare to the old context, this will cause the provider to reconcile with the new context.
111127
* When the provider "Reconciles" it will set the status to [OpenFeatureStatus.Reconciling].
112128
* When the provider successfully reconciles it will set the status to [OpenFeatureStatus.Ready].
113129
* If the provider fails to reconcile it will set the status to [OpenFeatureStatus.Error].
114130
*
131+
* This method requires you to manually wait for the status to be Ready before using the SDK for flag evaluations.
132+
* This can be done by using the [statusFlow] and waiting for the first Ready status or by accessing [getStatus]
133+
*
134+
*
115135
* @param evaluationContext the [EvaluationContext] to set
116136
*/
117-
suspend fun setEvaluationContext(evaluationContext: EvaluationContext) {
137+
fun setEvaluationContext(
138+
evaluationContext: EvaluationContext,
139+
dispatcher: CoroutineDispatcher = Dispatchers.IO
140+
) {
141+
setEvaluationContextJob?.cancel()
142+
this.setEvaluationContextJob = CoroutineScope(dispatcher).async {
143+
setEvaluationContextInternal(evaluationContext)
144+
}
145+
}
146+
147+
private suspend fun setEvaluationContextInternal(evaluationContext: EvaluationContext) {
118148
val oldContext = context
119149
context = evaluationContext
120150
if (oldContext != evaluationContext) {
@@ -179,6 +209,7 @@ object OpenFeatureAPI {
179209
* The SDK status will be set to [OpenFeatureStatus.NotReady].
180210
*/
181211
fun shutdown() {
212+
setEvaluationContextJob?.cancel(CancellationException("Set context job was cancelled"))
182213
setProviderJob?.cancel(CancellationException("Provider set job was cancelled"))
183214
_statusFlow.tryEmit(OpenFeatureStatus.NotReady)
184215
getProvider().shutdown()

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class DeveloperExperienceTests {
183183
testScheduler.advanceUntilIdle()
184184
// After 2 seconds the slow provider is ready
185185
// Setting a new context should cause the provider to reconcile
186-
OpenFeatureAPI.setEvaluationContext(ImmutableContext(targetingKey = "tk"))
186+
OpenFeatureAPI.setEvaluationContextAndWait(ImmutableContext(targetingKey = "tk"))
187187
testScheduler.advanceUntilIdle()
188188
// After 2 seconds the slow provider is ready again
189189
// Shutting down should cause the status to be NotReady
@@ -219,7 +219,7 @@ class DeveloperExperienceTests {
219219
testScheduler.advanceUntilIdle()
220220

221221
// setting a context will make it reconciling and then ready
222-
OpenFeatureAPI.setEvaluationContext(ImmutableContext(targetingKey = "new"))
222+
OpenFeatureAPI.setEvaluationContextAndWait(ImmutableContext(targetingKey = "new"))
223223
testScheduler.advanceTimeBy(100)
224224

225225
// Shutting down should cause the status to be NotReady

0 commit comments

Comments
 (0)