Skip to content

Commit f692d62

Browse files
committed
Revert "fix: db initialization logic"
This reverts commit a0a98c6.
1 parent a0a98c6 commit f692d62

File tree

4 files changed

+62
-20
lines changed

4 files changed

+62
-20
lines changed

package/src/components/Chat/Chat.tsx

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

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

6+
import { LoadingIndicator as LoadingIndicatorDefault } from './components/LoadingIndicator';
67
import { useAppSettings } from './hooks/useAppSettings';
78
import { useCreateChatContext } from './hooks/useCreateChatContext';
89
import { useIsOnline } from './hooks/useIsOnline';
@@ -137,11 +138,6 @@ export type ChatProps<
137138
style?: DeepPartial<Theme>;
138139
};
139140

140-
const initialisedDatabaseConfig: {
141-
initialised: boolean;
142-
userID?: string;
143-
} = { initialised: false, userID: '' };
144-
145141
const ChatWithContext = <
146142
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
147143
>(
@@ -154,6 +150,7 @@ const ChatWithContext = <
154150
enableOfflineSupport = false,
155151
i18nInstance,
156152
ImageComponent = Image,
153+
LoadingIndicator = LoadingIndicatorDefault,
157154
resizableCDNHosts = ['.stream-io-cdn.com'],
158155
style,
159156
} = props;
@@ -162,7 +159,6 @@ const ChatWithContext = <
162159

163160
// Setup translators
164161
const translators = useStreami18n(i18nInstance);
165-
const userID = client.userID;
166162

167163
/**
168164
* Setup connection event listeners
@@ -172,6 +168,13 @@ const ChatWithContext = <
172168
closeConnectionOnBackground,
173169
);
174170

171+
const [initialisedDatabaseConfig, setInitialisedDatabaseConfig] = useState<{
172+
initialised: boolean;
173+
userID?: string;
174+
}>({
175+
initialised: false,
176+
});
177+
175178
/**
176179
* Setup muted user listener
177180
* TODO: reimplement
@@ -181,6 +184,8 @@ const ChatWithContext = <
181184
const debugRef = useDebugContext();
182185
const isDebugModeEnabled = __DEV__ && debugRef && debugRef.current;
183186

187+
const userID = client.userID;
188+
184189
// Set the `resizableCDNHosts` as per the prop.
185190
StreamChatRN.setConfig({ resizableCDNHosts });
186191

@@ -207,6 +212,12 @@ const ChatWithContext = <
207212

208213
useEffect(() => {
209214
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 });
210221
DBSyncManager.init(client as unknown as StreamChat);
211222
}
212223
// eslint-disable-next-line react-hooks/exhaustive-deps
@@ -228,18 +239,10 @@ const ChatWithContext = <
228239
// on unmount if it exists to prevent a memory leak.
229240
useEffect(() => () => DBSyncManager.connectionChangedListener?.unsubscribe(), []);
230241

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);
242+
const initialisedDatabase =
243+
initialisedDatabaseConfig.initialised && userID === initialisedDatabaseConfig.userID;
238244

239-
useSyncDatabase({
240-
client,
241-
enableOfflineSupport,
242-
});
245+
const appSettings = useAppSettings(client, isOnline, enableOfflineSupport, initialisedDatabase);
243246

244247
const chatContext = useCreateChatContext({
245248
appSettings,
@@ -254,6 +257,17 @@ const ChatWithContext = <
254257
setActiveChannel,
255258
});
256259

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+
257271
return (
258272
<ChatProvider<StreamChatGenerics> value={chatContext}>
259273
<TranslationProvider
@@ -278,6 +292,15 @@ const ChatWithContext = <
278292
* - connectionRecovering - whether or not websocket is reconnecting
279293
* - isOnline - whether or not set user is active
280294
* - 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
281304
*/
282305
export const Chat = <
283306
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
import { ActivityIndicator, StyleSheet, View } from 'react-native';
3+
4+
export const LoadingIndicator = () => (
5+
<View style={styles.container}>
6+
<ActivityIndicator size='large' />
7+
</View>
8+
);
9+
10+
const styles = StyleSheet.create({
11+
container: {
12+
alignItems: 'center',
13+
flex: 1,
14+
justifyContent: 'center',
15+
},
16+
});

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

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

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

7980
return appSettings;
8081
};

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,25 @@ import type { DefaultStreamChatGenerics } from '../../../types/types';
99
type Params<StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics> = {
1010
client: StreamChat<StreamChatGenerics>;
1111
enableOfflineSupport: boolean;
12+
initialisedDatabase: boolean;
1213
};
1314
export const useSyncDatabase = <
1415
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics,
1516
>({
1617
client,
1718
enableOfflineSupport,
19+
initialisedDatabase,
1820
}: Params<StreamChatGenerics>) => {
1921
useEffect(() => {
2022
let listener: ReturnType<StreamChat['on']> | undefined;
2123

22-
if (enableOfflineSupport) {
24+
if (enableOfflineSupport && initialisedDatabase) {
2325
listener = client?.on((event) => handleEventToSyncDB(event, client));
2426
}
2527

2628
return () => {
2729
listener?.unsubscribe();
2830
};
2931
// eslint-disable-next-line react-hooks/exhaustive-deps
30-
}, [client]);
32+
}, [client, initialisedDatabase]);
3133
};

0 commit comments

Comments
 (0)