Skip to content

Commit 34e6af2

Browse files
committed
fix: race condition with query scheduling
1 parent dc01d4d commit 34e6af2

File tree

2 files changed

+30
-59
lines changed

2 files changed

+30
-59
lines changed

src/channel_manager.ts

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -268,9 +268,6 @@ export class ChannelManager {
268268

269269
const queryChannelsRequest = async (retryCount = 0) => {
270270
try {
271-
if (retryCount <= 2) {
272-
throw new Error('Failing intentionally');
273-
}
274271
const channels = await this.client.queryChannels(
275272
filters,
276273
sort,
@@ -332,46 +329,42 @@ export class ChannelManager {
332329
error: undefined,
333330
}));
334331

335-
if (
336-
this.client.offlineDb?.getChannelsForQuery &&
337-
this.client.user?.id &&
338-
!initialized
339-
) {
340-
const channelsFromDB = await this.client.offlineDb.getChannelsForQuery({
341-
userId: this.client.user.id,
342-
filters,
343-
sort,
344-
});
345-
346-
console.log('CHANNELS FROM DB PRE: ', channelsFromDB);
347-
348-
if (channelsFromDB) {
349-
console.log('GOT CHANNELS FROM DB !', initialized, isLoading);
350-
const offlineChannels = this.client.hydrateActiveChannels(channelsFromDB, {
351-
offlineMode: true,
352-
skipInitialization: [], // passing empty array will clear out the existing messages from channel state, this removes the possibility of duplicate messages
332+
if (this.client.offlineDb?.getChannelsForQuery && this.client.user?.id) {
333+
if (!initialized) {
334+
const channelsFromDB = await this.client.offlineDb.getChannelsForQuery({
335+
userId: this.client.user.id,
336+
filters,
337+
sort,
353338
});
354339

355-
this.state.partialNext({ channels: offlineChannels });
340+
console.log('CHANNELS FROM DB PRE: ', channelsFromDB);
356341

357-
console.log('IS IT SYNCED: ', this.client.offlineDb.syncManager.syncStatus);
342+
if (channelsFromDB) {
343+
console.log('GOT CHANNELS FROM DB !', initialized, isLoading);
344+
const offlineChannels = this.client.hydrateActiveChannels(channelsFromDB, {
345+
offlineMode: true,
346+
skipInitialization: [], // passing empty array will clear out the existing messages from channel state, this removes the possibility of duplicate messages
347+
});
358348

359-
if (!this.client.offlineDb.syncManager.syncStatus) {
360-
this.client.offlineDb.syncManager.scheduleSyncStatusChangeCallback(
361-
async (syncStatus) => {
362-
console.log('WILL TRY NOW VAL: ', syncStatus);
363-
if (syncStatus) {
364-
await queryChannelsRequest();
365-
}
366-
},
367-
);
368-
return;
349+
this.state.partialNext({ channels: offlineChannels });
369350
}
370351
}
371-
await queryChannelsRequest();
372-
} else {
373-
await queryChannelsRequest();
352+
console.log('IS IT SYNCED: ', this.client.offlineDb.syncManager.syncStatus);
353+
354+
if (!this.client.offlineDb.syncManager.syncStatus) {
355+
this.client.offlineDb.syncManager.scheduleSyncStatusChangeCallback(
356+
async (syncStatus) => {
357+
console.log('WILL TRY NOW VAL: ', syncStatus);
358+
if (syncStatus) {
359+
await queryChannelsRequest();
360+
}
361+
},
362+
);
363+
return;
364+
}
365+
// await queryChannelsRequest();
374366
}
367+
await queryChannelsRequest();
375368
} catch (error) {
376369
this.client.logger('error', (error as Error).message);
377370
this.state.next((currentState) => ({

src/client.ts

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,7 @@ export class StreamChat {
616616
);
617617

618618
try {
619-
const setUserResponse = await this.setUserPromise;
620-
console.log('TEST USER: ', setUserResponse);
621-
return setUserResponse;
619+
return await this.setUserPromise;
622620
} catch (err) {
623621
if (this.persistUserOnConnectionFailure) {
624622
// cleanup client to allow the user to retry connectUser again
@@ -3207,26 +3205,6 @@ export class StreamChat {
32073205

32083206
const { os, model } = this.deviceIdentifier ?? {};
32093207

3210-
console.log(
3211-
'USER AGENT STRING: ',
3212-
(
3213-
[
3214-
// reports the device OS, if provided
3215-
['os', os],
3216-
// reports the device model, if provided
3217-
['device_model', model],
3218-
// reports which bundle is being picked from the exports
3219-
['client_bundle', clientBundle],
3220-
] as const
3221-
).reduce(
3222-
(withArguments, [key, value]) =>
3223-
value && value.length > 0
3224-
? withArguments.concat(`|${key}=${value}`)
3225-
: withArguments,
3226-
userAgentString,
3227-
),
3228-
);
3229-
32303208
return (
32313209
[
32323210
// reports the device OS, if provided

0 commit comments

Comments
 (0)