Skip to content

Commit 4478372

Browse files
committed
tidy listener api on FlagsClient
1 parent d6d6538 commit 4478372

File tree

2 files changed

+13
-56
lines changed

2 files changed

+13
-56
lines changed

features/dd-sdk-android-flags/src/main/kotlin/com/datadog/android/flags/FlagsClient.kt

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import com.datadog.android.core.InternalSdkCore
1616
import com.datadog.android.flags.internal.DatadogFlagsClient
1717
import com.datadog.android.flags.internal.DefaultRumEvaluationLogger
1818
import com.datadog.android.flags.internal.FlagsFeature
19-
import com.datadog.android.flags.internal.FlagsStateChannel
19+
import com.datadog.android.flags.internal.FlagsStateManager
2020
import com.datadog.android.flags.internal.LogWithPolicy
2121
import com.datadog.android.flags.internal.NoOpFlagsClient
2222
import com.datadog.android.flags.internal.NoOpRumEvaluationLogger
@@ -28,7 +28,6 @@ import com.datadog.android.flags.internal.repository.DefaultFlagsRepository
2828
import com.datadog.android.flags.internal.repository.NoOpFlagsRepository
2929
import com.datadog.android.flags.internal.repository.net.PrecomputeMapper
3030
import com.datadog.android.flags.model.EvaluationContext
31-
import com.datadog.android.flags.model.FlagsClientState
3231
import com.datadog.android.flags.model.ResolutionDetails
3332
import com.datadog.android.internal.utils.DDCoreSubscription
3433
import org.json.JSONObject
@@ -151,21 +150,12 @@ interface FlagsClient {
151150
*/
152151
fun <T : Any> resolve(flagKey: String, defaultValue: T): ResolutionDetails<T>
153152

154-
/**
155-
* Gets the current state of this [FlagsClient].
156-
*
157-
* The state indicates whether the client is ready to evaluate flags, currently loading
158-
* new flag values, or in an error state.
159-
*
160-
* @return The current [FlagsClientState].
161-
*/
162-
fun getCurrentState(): FlagsClientState
163-
164153
/**
165154
* Registers a listener to receive state change notifications.
166155
*
167-
* The listener will be notified whenever the client's state changes (e.g., from NOT_READY
168-
* to READY, or from READY to RECONCILING when the evaluation context changes).
156+
* The listener will be notified whenever the client's state changes (e.g., from
157+
* [FlagsClientState.NotReady] to [FlagsClientState.Ready], or from [FlagsClientState.Ready]
158+
* to [FlagsClientState.Reconciling] when the evaluation context changes).
169159
*
170160
* @param listener The [FlagsStateListener] to register.
171161
*/
@@ -432,8 +422,9 @@ interface FlagsClient {
432422

433423
val precomputeMapper = PrecomputeMapper(featureSdkCore.internalLogger)
434424

435-
val flagStateChannel = FlagsStateChannel(
436-
subscription = DDCoreSubscription.create()
425+
val flagStateManager = FlagsStateManager(
426+
subscription = DDCoreSubscription.create(),
427+
executorService = executorService
437428
)
438429

439430
val evaluationsManager = EvaluationsManager(
@@ -442,7 +433,7 @@ interface FlagsClient {
442433
flagsRepository = flagsRepository,
443434
assignmentsReader = assignmentsDownloader,
444435
precomputeMapper = precomputeMapper,
445-
flagStateChannel = flagStateChannel
436+
flagStateManager = flagStateManager
446437
)
447438

448439
val rumEvaluationLogger = createRumEvaluationLogger(featureSdkCore)
@@ -454,7 +445,7 @@ interface FlagsClient {
454445
flagsConfiguration = configuration,
455446
rumEvaluationLogger = rumEvaluationLogger,
456447
processor = flagsFeature.processor,
457-
flagStateChannel = flagStateChannel
448+
flagStateManager = flagStateManager
458449
)
459450
}
460451
}

features/dd-sdk-android-flags/src/main/kotlin/com/datadog/android/flags/internal/DatadogFlagsClient.kt

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@ import com.datadog.android.flags.internal.model.PrecomputedFlag
1616
import com.datadog.android.flags.internal.repository.FlagsRepository
1717
import com.datadog.android.flags.model.ErrorCode
1818
import com.datadog.android.flags.model.EvaluationContext
19-
import com.datadog.android.flags.model.FlagsClientState
2019
import com.datadog.android.flags.model.ResolutionDetails
2120
import com.datadog.android.flags.model.ResolutionReason
2221
import org.json.JSONObject
23-
import java.util.concurrent.atomic.AtomicReference
2422

2523
/**
2624
* Production implementation of [FlagsClient] that integrates with Datadog's flag evaluation system.
@@ -38,7 +36,7 @@ import java.util.concurrent.atomic.AtomicReference
3836
* @param flagsConfiguration configuration for the flags feature
3937
* @param rumEvaluationLogger responsible for sending flag evaluations to RUM.
4038
* @param processor responsible for writing exposure batches to be sent to flags backend.
41-
* @param flagStateChannel channel for managing state change listeners
39+
* @param flagStateManager channel for managing state change listeners
4240
*/
4341
@Suppress("TooManyFunctions") // All functions are necessary for flag evaluation lifecycle
4442
internal class DatadogFlagsClient(
@@ -48,38 +46,8 @@ internal class DatadogFlagsClient(
4846
private val flagsConfiguration: FlagsConfiguration,
4947
private val rumEvaluationLogger: RumEvaluationLogger,
5048
private val processor: EventsProcessor,
51-
private val flagStateChannel: FlagsStateChannel
49+
private val flagStateManager: FlagsStateManager
5250
) : FlagsClient {
53-
54-
// region State Management
55-
56-
/**
57-
* The current state of this client.
58-
* Thread-safe: uses atomic reference for lock-free reads and updates.
59-
*/
60-
private val currentState = AtomicReference(FlagsClientState.NOT_READY)
61-
62-
/**
63-
* Updates the client state and notifies all registered listeners.
64-
*
65-
* This method is thread-safe and guarantees that listeners receive state changes in order.
66-
* The notification is delegated to [flagStateChannel] which handles synchronization.
67-
*
68-
* @param newState The new state to transition to.
69-
* @param error Optional error that caused the state change.
70-
*/
71-
internal fun updateState(newState: FlagsClientState, error: Throwable? = null) {
72-
currentState.set(newState)
73-
when (newState) {
74-
FlagsClientState.NOT_READY -> flagStateChannel.notifyNotReady()
75-
FlagsClientState.READY -> flagStateChannel.notifyReady()
76-
FlagsClientState.RECONCILING -> flagStateChannel.notifyReconciling()
77-
FlagsClientState.ERROR -> flagStateChannel.notifyError(error)
78-
}
79-
}
80-
81-
// endregion
82-
8351
// region FlagsClient
8452

8553
/**
@@ -184,14 +152,12 @@ internal class DatadogFlagsClient(
184152
}
185153
}
186154

187-
override fun getCurrentState(): FlagsClientState = currentState.get()
188-
189155
override fun addStateListener(listener: FlagsStateListener) {
190-
flagStateChannel.addListener(listener)
156+
flagStateManager.addListener(listener)
191157
}
192158

193159
override fun removeStateListener(listener: FlagsStateListener) {
194-
flagStateChannel.removeListener(listener)
160+
flagStateManager.removeListener(listener)
195161
}
196162

197163
// endregion

0 commit comments

Comments
 (0)