@@ -6,8 +6,9 @@ import kotlinx.coroutines.CoroutineScope
66import kotlinx.coroutines.Deferred
77import kotlinx.coroutines.Dispatchers
88import kotlinx.coroutines.async
9+ import kotlinx.coroutines.flow.Flow
910import kotlinx.coroutines.flow.MutableSharedFlow
10- import kotlinx.coroutines.flow.SharedFlow
11+ import kotlinx.coroutines.flow.distinctUntilChanged
1112import java.util.concurrent.CancellationException
1213
1314@Suppress(" TooManyFunctions" )
@@ -27,7 +28,7 @@ object OpenFeatureAPI {
2728 /* *
2829 * A flow of [OpenFeatureStatus] that emits the current status of the SDK.
2930 */
30- val statusFlow: SharedFlow <OpenFeatureStatus > get() = _statusFlow
31+ val statusFlow: Flow <OpenFeatureStatus > get() = _statusFlow .distinctUntilChanged()
3132
3233 var hooks: List <Hook <* >> = listOf ()
3334 private set
@@ -73,22 +74,21 @@ object OpenFeatureAPI {
7374 initialContext : EvaluationContext ? = null
7475 ) {
7576 this @OpenFeatureAPI.provider = provider
76- // TODO consider if stale status should emit? _statusFlow.tryEmit (OpenFeatureStatus.Stale )
77+ _statusFlow .emit (OpenFeatureStatus .NotReady )
7778 if (initialContext != null ) context = initialContext
7879 try {
7980 getProvider().initialize(context)
80- _statusFlow .tryEmit (OpenFeatureStatus .Ready )
81+ _statusFlow .emit (OpenFeatureStatus .Ready )
8182 } catch (e: OpenFeatureError ) {
82- _statusFlow .tryEmit (OpenFeatureStatus .Error (e))
83+ _statusFlow .emit (OpenFeatureStatus .Error (e))
8384 } catch (e: Throwable ) {
84- _statusFlow .tryEmit (
85+ _statusFlow .emit (
8586 OpenFeatureStatus .Error (
8687 OpenFeatureError .GeneralError (
8788 e.message ? : e.javaClass.name
8889 )
8990 )
9091 )
91- // TODO deal with things by setting status to Error or Fatal
9292 }
9393 }
9494
@@ -104,6 +104,7 @@ object OpenFeatureAPI {
104104 */
105105 fun clearProvider () {
106106 provider = NOOP_PROVIDER
107+ _statusFlow .tryEmit(OpenFeatureStatus .NotReady )
107108 }
108109
109110 /* *
@@ -148,15 +149,14 @@ object OpenFeatureAPI {
148149 val oldContext = context
149150 context = evaluationContext
150151 if (oldContext != evaluationContext) {
151- _statusFlow .tryEmit (OpenFeatureStatus .Reconciling )
152+ _statusFlow .emit (OpenFeatureStatus .Reconciling )
152153 try {
153154 getProvider().onContextSet(oldContext, evaluationContext)
154- _statusFlow .tryEmit (OpenFeatureStatus .Ready )
155+ _statusFlow .emit (OpenFeatureStatus .Ready )
155156 } catch (e: OpenFeatureError ) {
156- _statusFlow .tryEmit(OpenFeatureStatus .Error (e))
157- // TODO how do we handle fatal errors?
157+ _statusFlow .emit(OpenFeatureStatus .Error (e))
158158 } catch (e: Throwable ) {
159- _statusFlow .tryEmit (
159+ _statusFlow .emit (
160160 OpenFeatureStatus .Error (
161161 OpenFeatureError .GeneralError (
162162 e.message ? : e.javaClass.name
@@ -211,6 +211,7 @@ object OpenFeatureAPI {
211211 fun shutdown () {
212212 setEvaluationContextJob?.cancel(CancellationException (" Set context job was cancelled" ))
213213 setProviderJob?.cancel(CancellationException (" Provider set job was cancelled" ))
214+ provider = NOOP_PROVIDER
214215 _statusFlow .tryEmit(OpenFeatureStatus .NotReady )
215216 getProvider().shutdown()
216217 clearHooks()
@@ -219,5 +220,5 @@ object OpenFeatureAPI {
219220 /* *
220221 * Get the current [OpenFeatureStatus] of the SDK.
221222 */
222- fun getStatus (): OpenFeatureStatus = statusFlow .replayCache.first()
223+ fun getStatus (): OpenFeatureStatus = _statusFlow .replayCache.first()
223224}
0 commit comments