1+ /*
2+ * Copyright (c) 2014-2025 Stream.io Inc. All rights reserved.
3+ *
4+ * Licensed under the Stream License;
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * https://github.com/GetStream/stream-core-android/blob/main/LICENSE
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+
117package io.getstream.android.core.api.watcher
218
319import io.getstream.android.core.annotations.StreamInternalApi
@@ -6,7 +22,6 @@ import io.getstream.android.core.api.model.StreamCid
622import io.getstream.android.core.api.observers.StreamStartableComponent
723import io.getstream.android.core.api.socket.listeners.StreamClientListener
824import io.getstream.android.core.api.subscribe.StreamObservable
9- import io.getstream.android.core.api.subscribe.StreamSubscription
1025import io.getstream.android.core.api.subscribe.StreamSubscriptionManager
1126import io.getstream.android.core.internal.watcher.StreamCidWatcherImpl
1227
@@ -15,22 +30,22 @@ import io.getstream.android.core.internal.watcher.StreamCidWatcherImpl
1530 * triggers re-watching when the connection state changes.
1631 *
1732 * This component is critical for maintaining active subscriptions across reconnection cycles. When
18- * the WebSocket disconnects and reconnects, all active watches must be re-established on the server.
19- * [StreamCidWatcher] tracks which [StreamCid]s (Channel IDs) are currently being watched and
20- * invokes a callback on every connection state change, allowing the product SDK to re-subscribe.
33+ * the WebSocket disconnects and reconnects, all active watches must be re-established on the
34+ * server. [StreamCidWatcher] tracks which [StreamCid]s (Channel IDs) are currently being watched
35+ * and invokes a callback on every connection state change, allowing the product SDK to
36+ * re-subscribe.
2137 *
2238 * ## Typical Usage Flow
23- *
2439 * 1. Product SDK watches a channel: `watcher.watch(StreamCid.parse("messaging:general"))`
2540 * 2. Watcher adds the CID to its internal registry
26- * 3. Product SDK registers a rewatch listener: `watcher.subscribe(StreamCidRewatchListener { cids -> resubscribe(cids) })`
41+ * 3. Product SDK registers a rewatch listener: `watcher.subscribe(StreamCidRewatchListener { cids
42+ * -> resubscribe(cids) })`
2743 * 4. Call `watcher.start()` to begin monitoring connection state changes
2844 * 5. Connection state changes (e.g., reconnection after network loss)
2945 * 6. Watcher invokes listener with all watched CIDs: `["messaging:general", "livestream:sports"]`
3046 * 7. Product SDK re-establishes server-side watches for each CID
3147 *
3248 * ## Threading
33- *
3449 * - All methods are thread-safe and can be called from any thread
3550 * - The rewatch callback is invoked asynchronously on an internal coroutine scope
3651 * - Callback invocation happens asynchronously on connection state changes
@@ -47,7 +62,8 @@ import io.getstream.android.core.internal.watcher.StreamCidWatcherImpl
4762 * @see StreamStartableComponent Lifecycle contract for start/stop operations
4863 */
4964@StreamInternalApi
50- public interface StreamCidWatcher : StreamObservable <StreamCidRewatchListener >, StreamStartableComponent {
65+ public interface StreamCidWatcher :
66+ StreamObservable <StreamCidRewatchListener >, StreamStartableComponent {
5167
5268 /* *
5369 * Registers a channel or resource as actively watched.
@@ -56,9 +72,11 @@ public interface StreamCidWatcher : StreamObservable<StreamCidRewatchListener>,
5672 * changes (especially during reconnection), the rewatch callback will include this CID in the
5773 * list of resources that need to be re-subscribed.
5874 *
59- * Multiple calls with the same [cid] are idempotent—only one entry is maintained per unique CID.
75+ * Multiple calls with the same [cid] are idempotent—only one entry is maintained per unique
76+ * CID.
6077 *
6178 * ## Example
79+ *
6280 * ```kotlin
6381 * val cid = StreamCid.parse("messaging:general")
6482 * watcher.watch(cid)
@@ -70,7 +88,7 @@ public interface StreamCidWatcher : StreamObservable<StreamCidRewatchListener>,
7088 * @return [Result.success] with the [cid] if registration succeeded, or [Result.failure] if an
7189 * error occurred (e.g., internal registry corruption)
7290 */
73- public fun watch (cid : StreamCid ) : Result <StreamCid >
91+ public fun watch (cid : StreamCid ): Result <StreamCid >
7492
7593 /* *
7694 * Unregisters a channel or resource from the watch registry.
@@ -81,6 +99,7 @@ public interface StreamCidWatcher : StreamObservable<StreamCidRewatchListener>,
8199 * Calling this method with a CID that is not being watched is a no-op (not an error).
82100 *
83101 * ## Example
102+ *
84103 * ```kotlin
85104 * val cid = StreamCid.parse("messaging:general")
86105 * watcher.stopWatching(cid)
@@ -92,17 +111,18 @@ public interface StreamCidWatcher : StreamObservable<StreamCidRewatchListener>,
92111 * @return [Result.success] with the [cid] if removal succeeded, or [Result.failure] if an error
93112 * occurred (e.g., internal registry corruption)
94113 */
95- public fun stopWatching (cid : StreamCid ) : Result <StreamCid >
114+ public fun stopWatching (cid : StreamCid ): Result <StreamCid >
96115
97116 /* *
98117 * Removes all entries from the watch registry.
99118 *
100- * After calling this method, the rewatch callback will NOT be invoked on subsequent
101- * connection state changes (since the registry is empty) until new resources are watched.
119+ * After calling this method, the rewatch callback will NOT be invoked on subsequent connection
120+ * state changes (since the registry is empty) until new resources are watched.
102121 *
103122 * This method is typically called during cleanup or logout to ensure no stale watches remain.
104123 *
105124 * ## Example
125+ *
106126 * ```kotlin
107127 * // During logout
108128 * watcher.clear()
@@ -120,10 +140,10 @@ public interface StreamCidWatcher : StreamObservable<StreamCidRewatchListener>,
120140public fun StreamCidWatcher (
121141 logger : StreamLogger ,
122142 streamRewatchSubscriptionManager : StreamSubscriptionManager <StreamCidRewatchListener >,
123- streamClientSubscriptionManager : StreamSubscriptionManager <StreamClientListener >
124- ) : StreamCidWatcher = StreamCidWatcherImpl (
125- logger = logger,
126- rewatchSubscriptions = streamRewatchSubscriptionManager ,
127- clientSubscriptions = streamClientSubscriptionManager
128- )
129-
143+ streamClientSubscriptionManager : StreamSubscriptionManager <StreamClientListener >,
144+ ): StreamCidWatcher =
145+ StreamCidWatcherImpl (
146+ logger = logger ,
147+ rewatchSubscriptions = streamRewatchSubscriptionManager,
148+ clientSubscriptions = streamClientSubscriptionManager,
149+ )
0 commit comments