@@ -13,6 +13,7 @@ import java.util.concurrent.CancellationException
1313@Suppress(" TooManyFunctions" )
1414object 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()
0 commit comments