@@ -3,7 +3,6 @@ import { Image, Platform } from 'react-native';
33
44import type { Channel , StreamChat } from 'stream-chat' ;
55
6- import { LoadingIndicator as LoadingIndicatorDefault } from './components/LoadingIndicator' ;
76import { useAppSettings } from './hooks/useAppSettings' ;
87import { useCreateChatContext } from './hooks/useCreateChatContext' ;
98import { useIsOnline } from './hooks/useIsOnline' ;
@@ -138,6 +137,11 @@ export type ChatProps<
138137 style ?: DeepPartial < Theme > ;
139138 } ;
140139
140+ const initialisedDatabaseConfig : {
141+ initialised : boolean ;
142+ userID ?: string ;
143+ } = { initialised : false , userID : '' } ;
144+
141145const ChatWithContext = <
142146 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
143147> (
@@ -150,7 +154,6 @@ const ChatWithContext = <
150154 enableOfflineSupport = false ,
151155 i18nInstance,
152156 ImageComponent = Image ,
153- LoadingIndicator = LoadingIndicatorDefault ,
154157 resizableCDNHosts = [ '.stream-io-cdn.com' ] ,
155158 style,
156159 } = props ;
@@ -159,6 +162,7 @@ const ChatWithContext = <
159162
160163 // Setup translators
161164 const translators = useStreami18n ( i18nInstance ) ;
165+ const userID = client . userID ;
162166
163167 /**
164168 * Setup connection event listeners
@@ -168,13 +172,6 @@ const ChatWithContext = <
168172 closeConnectionOnBackground ,
169173 ) ;
170174
171- const [ initialisedDatabaseConfig , setInitialisedDatabaseConfig ] = useState < {
172- initialised : boolean ;
173- userID ?: string ;
174- } > ( {
175- initialised : false ,
176- } ) ;
177-
178175 /**
179176 * Setup muted user listener
180177 * TODO: reimplement
@@ -184,8 +181,6 @@ const ChatWithContext = <
184181 const debugRef = useDebugContext ( ) ;
185182 const isDebugModeEnabled = __DEV__ && debugRef && debugRef . current ;
186183
187- const userID = client . userID ;
188-
189184 // Set the `resizableCDNHosts` as per the prop.
190185 StreamChatRN . setConfig ( { resizableCDNHosts } ) ;
191186
@@ -212,12 +207,6 @@ const ChatWithContext = <
212207
213208 useEffect ( ( ) => {
214209 if ( userID && enableOfflineSupport ) {
215- // This acts as a lock for some very rare occurrences of concurrency
216- // issues we've encountered before with the QuickSqliteClient being
217- // uninitialized before it's being invoked.
218- setInitialisedDatabaseConfig ( { initialised : false , userID } ) ;
219- QuickSqliteClient . initializeDatabase ( ) ;
220- setInitialisedDatabaseConfig ( { initialised : true , userID } ) ;
221210 DBSyncManager . init ( client as unknown as StreamChat ) ;
222211 }
223212 // eslint-disable-next-line react-hooks/exhaustive-deps
@@ -239,10 +228,18 @@ const ChatWithContext = <
239228 // on unmount if it exists to prevent a memory leak.
240229 useEffect ( ( ) => ( ) => DBSyncManager . connectionChangedListener ?. unsubscribe ( ) , [ ] ) ;
241230
242- const initialisedDatabase =
243- initialisedDatabaseConfig . initialised && userID === initialisedDatabaseConfig . userID ;
231+ if ( enableOfflineSupport && ! initialisedDatabaseConfig . initialised ) {
232+ QuickSqliteClient . initializeDatabase ( ) ;
233+ initialisedDatabaseConfig . userID = userID ;
234+ initialisedDatabaseConfig . initialised = true ;
235+ }
236+
237+ const appSettings = useAppSettings ( client , isOnline , enableOfflineSupport ) ;
244238
245- const appSettings = useAppSettings ( client , isOnline , enableOfflineSupport , initialisedDatabase ) ;
239+ useSyncDatabase ( {
240+ client,
241+ enableOfflineSupport,
242+ } ) ;
246243
247244 const chatContext = useCreateChatContext ( {
248245 appSettings,
@@ -257,17 +254,6 @@ const ChatWithContext = <
257254 setActiveChannel,
258255 } ) ;
259256
260- useSyncDatabase ( {
261- client,
262- enableOfflineSupport,
263- initialisedDatabase,
264- } ) ;
265-
266- if ( userID && enableOfflineSupport && ! initialisedDatabase ) {
267- // if user id has been set and offline support is enabled, we need to wait for database to be initialised
268- return LoadingIndicator ? < LoadingIndicator /> : null ;
269- }
270-
271257 return (
272258 < ChatProvider < StreamChatGenerics > value = { chatContext } >
273259 < TranslationProvider
@@ -292,15 +278,6 @@ const ChatWithContext = <
292278 * - connectionRecovering - whether or not websocket is reconnecting
293279 * - isOnline - whether or not set user is active
294280 * - setActiveChannel - function to set the currently active channel
295- *
296- * The Chat Component takes the following generics in order:
297- * - At (AttachmentType) - custom Attachment object extension
298- * - Ct (ChannelType) - custom Channel object extension
299- * - Co (CommandType) - custom Command string union extension
300- * - Ev (EventType) - custom Event object extension
301- * - Me (MessageType) - custom Message object extension
302- * - Re (ReactionType) - custom Reaction object extension
303- * - Us (UserType) - custom User object extension
304281 */
305282export const Chat = <
306283 StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics ,
0 commit comments