11import React , { PropsWithChildren , useEffect , useState } from 'react' ;
22import { Image , Platform } from 'react-native' ;
33
4- import type { Channel } from 'stream-chat' ;
4+ import { Channel , OfflineDBState } from 'stream-chat' ;
55
66import { useAppSettings } from './hooks/useAppSettings' ;
77import { useCreateChatContext } from './hooks/useCreateChatContext' ;
@@ -20,12 +20,12 @@ import {
2020 DEFAULT_USER_LANGUAGE ,
2121 TranslationProvider ,
2222} from '../../contexts/translationContext/TranslationContext' ;
23+ import { useStateStore } from '../../hooks' ;
2324import { useStreami18n } from '../../hooks/useStreami18n' ;
2425import init from '../../init' ;
2526
2627import { NativeHandlers } from '../../native' ;
2728import { OfflineDB } from '../../store/OfflineDB' ;
28- import { SqliteClient } from '../../store/SqliteClient' ;
2929
3030import type { Streami18n } from '../../utils/i18n/Streami18n' ;
3131import { version } from '../../version.json' ;
@@ -134,6 +134,12 @@ export type ChatProps = Pick<ChatContextValue, 'client'> &
134134 style ?: DeepPartial < Theme > ;
135135 } ;
136136
137+ const selector = ( nextValue : OfflineDBState ) =>
138+ ( {
139+ initialized : nextValue . initialized ,
140+ userId : nextValue . userId ,
141+ } ) as const ;
142+
137143const ChatWithContext = ( props : PropsWithChildren < ChatProps > ) => {
138144 const {
139145 children,
@@ -157,13 +163,16 @@ const ChatWithContext = (props: PropsWithChildren<ChatProps>) => {
157163 */
158164 const { connectionRecovering, isOnline } = useIsOnline ( client , closeConnectionOnBackground ) ;
159165
160- const [ initialisedDatabaseConfig , setInitialisedDatabaseConfig ] = useState < {
161- initialised : boolean ;
162- userID ?: string ;
163- } > ( {
164- initialised : false ,
165- userID : client . userID ,
166- } ) ;
166+ // const [initialisedDatabaseConfig, setInitialisedDatabaseConfig] = useState<{
167+ // initialised: boolean;
168+ // userID?: string;
169+ // }>({
170+ // initialised: false,
171+ // userID: client.userID,
172+ // });
173+
174+ const { initialized : offlineDbInitialized , userId : offlineDbUserId } =
175+ useStateStore ( client . offlineDb ?. state , selector ) ?? { } ;
167176
168177 /**
169178 * Setup muted user listener
@@ -212,35 +221,18 @@ const ChatWithContext = (props: PropsWithChildren<ChatProps>) => {
212221 return ;
213222 }
214223
215- const initializeDatabase = ( ) => {
224+ const initializeDatabase = async ( ) => {
216225 // TODO: Rethink this, it looks ugly
217226 if ( ! client . offlineDb ) {
218227 client . setOfflineDBApi ( new OfflineDB ( { client } ) ) ;
219228 }
220- // This acts as a lock for some very rare occurrences of concurrency
221- // issues we've encountered before with the QuickSqliteClient being
222- // uninitialized before it's being invoked.
223- setInitialisedDatabaseConfig ( { initialised : false , userID } ) ;
224- SqliteClient . initializeDatabase ( )
225- . then ( async ( ) => {
226- setInitialisedDatabaseConfig ( { initialised : true , userID } ) ;
227- if ( client . offlineDb ) {
228- await client . offlineDb ?. syncManager . init ( ) ;
229- // client.offlineDb.initialised = initialised;
230- }
231- } )
232- . catch ( ( error ) => {
233- console . log ( 'Error Initializing DB:' , error ) ;
234- } ) ;
235- } ;
236229
237- initializeDatabase ( ) ;
238-
239- return ( ) => {
240- if ( userID && enableOfflineSupport ) {
241- // SqliteClient.closeDB();
230+ if ( client . offlineDb ) {
231+ await client . offlineDb . init ( userID ) ;
242232 }
243233 } ;
234+
235+ initializeDatabase ( ) ;
244236 // eslint-disable-next-line react-hooks/exhaustive-deps
245237 } , [ userID , enableOfflineSupport ] ) ;
246238
@@ -258,12 +250,11 @@ const ChatWithContext = (props: PropsWithChildren<ChatProps>) => {
258250 // In case something went wrong, make sure to also unsubscribe the listener
259251 // on unmount if it exists to prevent a memory leak.
260252 // FIXME: Should be wrapped in its own unregistration mechanism
261- client . offlineDb ?. syncManager . connectionChangedListener ?. unsubscribe ( ) ;
253+ // client.offlineDb?.syncManager.connectionChangedListener?.unsubscribe();
262254 } ;
263255 } , [ client ] ) ;
264256
265- const initialisedDatabase =
266- initialisedDatabaseConfig . initialised && userID === initialisedDatabaseConfig . userID ;
257+ const initialisedDatabase = ! ! offlineDbInitialized && userID === offlineDbUserId ;
267258
268259 const appSettings = useAppSettings ( client , isOnline , enableOfflineSupport , initialisedDatabase ) ;
269260
0 commit comments