@@ -20,7 +20,6 @@ import io.getstream.android.core.annotations.StreamInternalApi
2020import io.getstream.android.core.api.log.StreamLogger
2121import io.getstream.android.core.api.model.connection.StreamConnectionState
2222import io.getstream.android.core.api.observers.StreamStartableComponent
23- import io.getstream.android.core.api.socket.listeners.StreamClientListener
2423import io.getstream.android.core.api.subscribe.StreamObservable
2524import io.getstream.android.core.api.subscribe.StreamSubscriptionManager
2625import io.getstream.android.core.internal.watcher.StreamWatcherImpl
@@ -39,8 +38,8 @@ import kotlinx.coroutines.flow.StateFlow
3938 * ## Typical Usage Flow
4039 * 1. Product SDK watches a channel: `watcher.watch("messaging:general")`
4140 * 2. Watcher adds the identifier to its internal registry
42- * 3. Product SDK registers a rewatch listener: `watcher.subscribe(StreamRewatchListener { ids, connectionId ->
43- * resubscribe(ids, connectionId) })`
41+ * 3. Product SDK registers a rewatch listener: `watcher.subscribe(StreamRewatchListener { ids,
42+ * connectionId -> resubscribe(ids, connectionId) })`
4443 * 4. Call `watcher.start()` to begin monitoring connection state changes
4544 * 5. Connection state changes (e.g., reconnection after network loss)
4645 * 6. Watcher invokes listener with all watched entries: `["messaging:general",
@@ -138,24 +137,22 @@ public interface StreamWatcher<T> :
138137}
139138
140139/* *
141- * Creates a new [StreamWatcher] instance with the provided dependencies .
140+ * Creates a new [StreamWatcher] instance that observes connection state changes .
142141 *
143- * This factory method instantiates the default implementation ([StreamWatcherImpl]) and wires all
144- * required dependencies for monitoring connection state changes and triggering rewatch callbacks.
142+ * This factory method instantiates the default implementation ([StreamWatcherImpl]) and creates the
143+ * necessary internal subscription manager. The watcher observes the provided connection state flow
144+ * and triggers rewatch callbacks when the connection state changes to Connected.
145145 *
146146 * ## Parameters
147- * - **scope**: The [CoroutineScope] used for launching async operations (rewatch callbacks). This
148- * scope should typically use `SupervisorJob + Dispatchers.Default` to ensure callback failures
149- * don't cancel the scope and to avoid blocking the main thread. The scope is NOT cancelled when
150- * [StreamWatcher.stop] is called, allowing the component to be restarted.
147+ * - **scope**: The [CoroutineScope] used for launching async operations (collecting state flow and
148+ * invoking rewatch callbacks). This scope should typically use `SupervisorJob + Dispatchers.
149+ * Default` to ensure callback failures don't cancel the scope and to avoid blocking the main
150+ * thread. The scope is NOT cancelled when [StreamWatcher.stop] is called, allowing the component
151+ * to be restarted.
151152 * - **logger**: A [StreamLogger] instance for diagnostic output, tagged appropriately (e.g.,
152- * "SCWatcher"). Used for logging state changes, rewatch events, and errors.
153- * - **streamRewatchSubscriptionManager**: Manages subscriptions to [StreamRewatchListener]. This
154- * manager handles the list of listeners that will be invoked when connection state changes
155- * require re-watching identifiers.
156- * - **streamClientSubscriptionManager**: Manages subscriptions to [StreamClientListener]. This is
157- * used to subscribe to connection state changes (via [StreamClientListener.onState]) and to
158- * surface errors (via [StreamClientListener.onError]) when rewatch callbacks fail.
153+ * "Watcher"). Used for logging state changes, rewatch events, and errors.
154+ * - **connectionState**: A [StateFlow] providing connection state updates. Typically obtained from
155+ * `streamClient.connectionState`. The watcher will collect from this flow when started.
159156 *
160157 * ## Lifecycle
161158 *
@@ -169,18 +166,11 @@ public interface StreamWatcher<T> :
169166 * val scope = CoroutineScope(SupervisorJob() + Dispatchers.Default)
170167 * val logger = StreamLogger.getLogger("MyApp.Watcher")
171168 *
172- * val rewatchManager = StreamSubscriptionManager<StreamRewatchListener<String>>(
173- * logger = logger.withTag("RewatchSubscriptions")
174- * )
175- * val clientManager = StreamSubscriptionManager<StreamClientListener>(
176- * logger = logger.withTag("ClientSubscriptions")
177- * )
178- *
169+ * // Create watcher - only needs the connection state flow
179170 * val watcher = StreamWatcher<String>(
180171 * scope = scope,
181172 * logger = logger,
182- * streamRewatchSubscriptionManager = rewatchManager,
183- * streamClientSubscriptionManager = clientManager
173+ * connectionState = streamClient.connectionState
184174 * )
185175 *
186176 * // Register rewatch listener
@@ -197,10 +187,10 @@ public interface StreamWatcher<T> :
197187 * watcher.watch("livestream:sports")
198188 * ```
199189 *
200- * @param scope Coroutine scope for async operations (rewatch callback invocations)
190+ * @param scope Coroutine scope for async operations (state collection and rewatch callback
191+ * invocations)
201192 * @param logger Logger for diagnostic output and error reporting
202- * @param streamRewatchSubscriptionManager Subscription manager for rewatch listeners
203- * @param streamClientSubscriptionManager Subscription manager for client state listeners
193+ * @param connectionState StateFlow providing connection state updates
204194 * @return A new [StreamWatcher] instance (implementation: [StreamWatcherImpl])
205195 * @see StreamWatcher The interface contract
206196 * @see StreamWatcherImpl The concrete implementation
@@ -210,12 +200,13 @@ public interface StreamWatcher<T> :
210200public fun <T > StreamWatcher (
211201 scope : CoroutineScope ,
212202 logger : StreamLogger ,
213- streamRewatchSubscriptionManager : StreamSubscriptionManager < StreamRewatchListener < T > >,
214- streamClientSubscriptionManager : StreamSubscriptionManager < StreamClientListener >,
215- ): StreamWatcher < T > =
216- StreamWatcherImpl (
203+ connectionState : StateFlow < StreamConnectionState >,
204+ ): StreamWatcher < T > {
205+ val rewatchSubscriptions = StreamSubscriptionManager < StreamRewatchListener < T >>(logger)
206+ return StreamWatcherImpl (
217207 scope = scope,
208+ connectionState = connectionState,
209+ rewatchSubscriptions = rewatchSubscriptions,
218210 logger = logger,
219- rewatchSubscriptions = streamRewatchSubscriptionManager,
220- clientSubscriptions = streamClientSubscriptionManager,
221211 )
212+ }
0 commit comments