Skip to content

Commit 8b1e40d

Browse files
committed
fix: remove rollback
1 parent f1f208a commit 8b1e40d

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import throttle from 'lodash/throttle';
77
import { lookup } from 'mime-types';
88
import {
99
Channel as ChannelClass,
10+
ChannelResponse,
1011
ChannelState,
1112
Channel as ChannelType,
1213
DeleteMessageOptions,
@@ -102,7 +103,6 @@ import {
102103
isImagePickerAvailable,
103104
NativeHandlers,
104105
} from '../../native';
105-
import * as dbApi from '../../store/apis';
106106
import { ChannelUnreadState, FileTypes } from '../../types/types';
107107
import { addReactionToLocalState } from '../../utils/addReactionToLocalState';
108108
import { compressedImageURI } from '../../utils/compressImage';
@@ -1300,9 +1300,13 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13001300
attachment.image_url = uploadResponse.file;
13011301
delete attachment.originalFile;
13021302

1303-
await dbApi.updateMessage({
1304-
message: { ...updatedMessage, cid: channel.cid },
1305-
});
1303+
client.offlineDb?.executeQuerySafely(
1304+
(db) =>
1305+
db.updateMessage({
1306+
message: { ...updatedMessage, cid: channel.cid },
1307+
}),
1308+
{ method: 'upsertAppSettings' },
1309+
);
13061310
}
13071311

13081312
if (attachment.type !== FileTypes.Image && file?.uri) {
@@ -1321,9 +1325,13 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13211325
}
13221326

13231327
delete attachment.originalFile;
1324-
await dbApi.updateMessage({
1325-
message: { ...updatedMessage, cid: channel.cid },
1326-
});
1328+
client.offlineDb?.executeQuerySafely(
1329+
(db) =>
1330+
db.updateMessage({
1331+
message: { ...updatedMessage, cid: channel.cid },
1332+
}),
1333+
{ method: 'upsertAppSettings' },
1334+
);
13271335
}
13281336
}
13291337
}
@@ -1344,7 +1352,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13441352
retrying?: boolean;
13451353
}) => {
13461354
let failedMessageUpdated = false;
1347-
const handleFailedMessage = async () => {
1355+
const handleFailedMessage = () => {
13481356
if (!failedMessageUpdated) {
13491357
const updatedMessage = {
13501358
...localMessage,
@@ -1355,11 +1363,13 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13551363
threadInstance?.upsertReplyLocally?.({ message: updatedMessage });
13561364
optimisticallyUpdatedNewMessages.delete(localMessage.id);
13571365

1358-
if (enableOfflineSupport) {
1359-
await dbApi.updateMessage({
1360-
message: updatedMessage,
1361-
});
1362-
}
1366+
client.offlineDb?.executeQuerySafely(
1367+
(db) =>
1368+
db.updateMessage({
1369+
message: updatedMessage,
1370+
}),
1371+
{ method: 'upsertAppSettings' },
1372+
);
13631373

13641374
failedMessageUpdated = true;
13651375
}
@@ -1397,11 +1407,14 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
13971407
status: MessageStatusTypes.RECEIVED,
13981408
};
13991409

1400-
if (enableOfflineSupport) {
1401-
await dbApi.updateMessage({
1402-
message: { ...newMessageResponse, cid: channel.cid },
1403-
});
1404-
}
1410+
client.offlineDb?.executeQuerySafely(
1411+
(db) =>
1412+
db.updateMessage({
1413+
message: { ...newMessageResponse, cid: channel.cid },
1414+
}),
1415+
{ method: 'upsertAppSettings' },
1416+
);
1417+
14051418
if (retrying) {
14061419
replaceMessage(localMessage, newMessageResponse);
14071420
} else {
@@ -1425,15 +1438,22 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14251438
threadInstance?.upsertReplyLocally?.({ message: localMessage });
14261439
optimisticallyUpdatedNewMessages.add(localMessage.id);
14271440

1428-
if (enableOfflineSupport) {
1429-
// While sending a message, we add the message to local db with failed status, so that
1430-
// if app gets closed before message gets sent and next time user opens the app
1431-
// then user can see that message in failed state and can retry.
1432-
// If succesfull, it will be updated with received status.
1433-
await dbApi.upsertMessages({
1434-
messages: [{ ...localMessage, cid: channel.cid, status: MessageStatusTypes.FAILED }],
1435-
});
1436-
}
1441+
// While sending a message, we add the message to local db with failed status, so that
1442+
// if app gets closed before message gets sent and next time user opens the app
1443+
// then user can see that message in failed state and can retry.
1444+
// If succesfull, it will be updated with received status.
1445+
client.offlineDb?.executeQuerySafely(
1446+
async (db) => {
1447+
if (channel.data) {
1448+
await db.upsertChannelData({ channel: channel.data as unknown as ChannelResponse });
1449+
}
1450+
await db.upsertMessages({
1451+
// @ts-ignore
1452+
messages: [{ ...localMessage, cid: channel.cid, status: MessageStatusTypes.FAILED }],
1453+
});
1454+
},
1455+
{ method: 'upsertAppSettings' },
1456+
);
14371457

14381458
await sendMessageRequest({ localMessage, message, options });
14391459
},

package/src/components/Channel/hooks/useCreateChannelContext.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ export const useCreateChannelContext = ({
4343

4444
const readUsers = Object.values(read);
4545
const readUsersLength = readUsers.length;
46-
const readUsersLastReads = readUsers.map(({ last_read }) => last_read.toISOString()).join();
46+
const readUsersLastReads = readUsers
47+
.map(({ last_read }) => last_read?.toISOString() ?? '')
48+
.join();
4749
const stringifiedChannelUnreadState = JSON.stringify(channelUnreadState);
4850

4951
const channelContext: ChannelContextValue = useMemo(

package/src/store/SqliteClient.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ export class SqliteClient {
9696
});
9797
await this.db.executeBatch(finalQueries);
9898
} catch (e) {
99-
this.db?.execute('ROLLBACK');
10099
this.logger?.('error', 'SqlBatch queries failed', {
101100
error: e,
102101
queries,

0 commit comments

Comments
 (0)