Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AND-172] Await Depednecy Resolve process until ChatClient is properly initialized #5514

Merged
merged 3 commits into from
Dec 13, 2024

Conversation

JcMinarro
Copy link
Member

@JcMinarro JcMinarro commented Dec 12, 2024

🎯 Goal

Await Depednecy Resolve process until ChatClient is properly initialized or timeout is over to avoid race-conditions in situations where the Chat Components are initialized without waiting the user is connected.

🧪 Testing

Using the following patch we can verify the GlobalState can be resolved requesting it even before the connectUser() method is invoked.

Patch
Subject: [PATCH] crash investigation
---
Index: stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ChatHelper.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ChatHelper.kt b/stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ChatHelper.kt
--- a/stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ChatHelper.kt	(revision bbeee84dd2a16f5fa8f22cdd6d2b3399fd6bbe3f)
+++ b/stream-chat-android-compose-sample/src/main/java/io/getstream/chat/android/compose/sample/ChatHelper.kt	(date 1734007345405)
@@ -30,10 +30,13 @@
 import io.getstream.chat.android.models.Message
 import io.getstream.chat.android.models.UploadAttachmentsNetworkType
 import io.getstream.chat.android.offline.plugin.factory.StreamOfflinePluginFactory
+import io.getstream.chat.android.state.extensions.globalState
 import io.getstream.chat.android.state.plugin.config.StatePluginConfig
 import io.getstream.chat.android.state.plugin.factory.StreamStatePluginFactory
 import io.getstream.result.Error
+import kotlinx.coroutines.GlobalScope
 import kotlinx.coroutines.flow.transformWhile
+import kotlinx.coroutines.launch
 
 /**
  * A helper class that is responsible for initializing the SDK and connecting/disconnecting
@@ -100,6 +103,10 @@
         onSuccess: () -> Unit = {},
         onError: (Error) -> Unit = {},
     ) {
+        GlobalScope.launch {
+            println("JcLog: I am going to get the global state before connecting the user")
+            println("JcLog: I obtained the global state -> ${ChatClient.instance().globalState}")
+        }
         ChatClient.instance().run {
             clientState.initializationState
                 .transformWhile {
Index: stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt b/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt
--- a/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt	(revision bbeee84dd2a16f5fa8f22cdd6d2b3399fd6bbe3f)
+++ b/stream-chat-android-client/src/main/java/io/getstream/chat/android/client/ChatClient.kt	(date 1734007345405)
@@ -386,6 +386,7 @@
             }
         }
         while (initState == InitializationState.INITIALIZING && spendTime < timeoutMilliseconds) {
+            println("JcLog: Waiting for initialization to complete")
             java.lang.Thread.sleep(INITIALIZATION_DELAY)
             spendTime += INITIALIZATION_DELAY
         }

🎉 GIF

@JcMinarro JcMinarro force-pushed the poc/dependency-resolve-with-timout branch from ede7003 to bbeee84 Compare December 12, 2024 12:45
@JcMinarro JcMinarro changed the title [PoC] Lazy dependency resolve [AND-172] Await Depednecy Resolve process until ChatClient is properly initialized Dec 12, 2024
@JcMinarro JcMinarro marked this pull request as ready for review December 12, 2024 13:11
@JcMinarro JcMinarro requested a review from a team as a code owner December 12, 2024 13:11
@JcMinarro JcMinarro force-pushed the poc/dependency-resolve-with-timout branch 2 times, most recently from 09be2e0 to 9d3b888 Compare December 12, 2024 13:18
@JcMinarro JcMinarro enabled auto-merge (squash) December 12, 2024 17:25
@JcMinarro JcMinarro force-pushed the poc/dependency-resolve-with-timout branch 2 times, most recently from 257e1a5 to 2728e5b Compare December 12, 2024 17:29
@JcMinarro JcMinarro force-pushed the poc/dependency-resolve-with-timout branch from 2728e5b to dc2e34b Compare December 13, 2024 10:52
@JcMinarro JcMinarro force-pushed the poc/dependency-resolve-with-timout branch from dc2e34b to 2378f37 Compare December 13, 2024 12:50
@JcMinarro JcMinarro merged commit 40bcc0f into develop Dec 13, 2024
9 checks passed
@JcMinarro JcMinarro deleted the poc/dependency-resolve-with-timout branch December 13, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants