Skip to content

Commit a0a98c6

Browse files
committed
fix: db initialization logic
1 parent b1807ac commit a0a98c6

File tree

4 files changed

+20
-62
lines changed

4 files changed

+20
-62
lines changed

package/src/components/Chat/Chat.tsx

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Image, Platform } from 'react-native';
33

44
import type { Channel, StreamChat } from 'stream-chat';
55

6-
import { LoadingIndicator as LoadingIndicatorDefault } from './components/LoadingIndicator';
76
import { useAppSettings } from './hooks/useAppSettings';
87
import { useCreateChatContext } from './hooks/useCreateChatContext';
98
import { 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+
141145
const 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
*/
305282
export const Chat = <
306283
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,

package/src/components/Chat/components/LoadingIndicator.tsx

Lines changed: 0 additions & 16 deletions
This file was deleted.

package/src/components/Chat/hooks/useAppSettings.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const useAppSettings = <
1212
client: StreamChat<StreamChatGenerics>,
1313
isOnline: boolean | null,
1414
enableOfflineSupport: boolean,
15-
initialisedDatabase: boolean,
1615
): AppSettingsAPIResponse | null => {
1716
const [appSettings, setAppSettings] = useState<AppSettingsAPIResponse | null>(null);
1817
const isMounted = useIsMountedRef();
@@ -75,7 +74,7 @@ export const useAppSettings = <
7574

7675
enforeAppSettings();
7776
// eslint-disable-next-line react-hooks/exhaustive-deps
78-
}, [client, isOnline, initialisedDatabase]);
77+
}, [client, isOnline]);
7978

8079
return appSettings;
8180
};

package/src/components/Chat/hooks/useSyncDatabase.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,23 @@ import type { DefaultStreamChatGenerics } from '../../../types/types';
99
type Params<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> = {
1010
client: StreamChat<StreamChatGenerics>;
1111
enableOfflineSupport: boolean;
12-
initialisedDatabase: boolean;
1312
};
1413
export const useSyncDatabase = <
1514
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
1615
>({
1716
client,
1817
enableOfflineSupport,
19-
initialisedDatabase,
2018
}: Params<StreamChatGenerics>) => {
2119
useEffect(() => {
2220
let listener: ReturnType<StreamChat['on']> | undefined;
2321

24-
if (enableOfflineSupport && initialisedDatabase) {
22+
if (enableOfflineSupport) {
2523
listener = client?.on((event) => handleEventToSyncDB(event, client));
2624
}
2725

2826
return () => {
2927
listener?.unsubscribe();
3028
};
3129
// eslint-disable-next-line react-hooks/exhaustive-deps
32-
}, [client, initialisedDatabase]);
30+
}, [client]);
3331
};

0 commit comments

Comments
 (0)