Skip to content

Commit fa44f7f

Browse files
committed
feat: reimplement dropPendingTasks
1 parent 5f2111a commit fa44f7f

File tree

6 files changed

+37
-19
lines changed

6 files changed

+37
-19
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,6 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15031503
const removeMessage: MessagesContextValue['removeMessage'] = useStableCallback(
15041504
async (message) => {
15051505
if (channel) {
1506-
// TODO: See if it's easy to refactor this to be able to accept DB queries
15071506
channel.state.removeMessage(message);
15081507
copyMessagesStateFromChannel(channel);
15091508

@@ -1513,13 +1512,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15131512
}
15141513

15151514
if (client.offlineDb) {
1516-
// FIXME: Batch these maybe ?.
1517-
await Promise.all([
1518-
client.offlineDb.dropPendingTasks({ messageId: message.id }),
1519-
client.offlineDb.hardDeleteMessage({
1520-
id: message.id,
1521-
}),
1522-
]);
1515+
await client.offlineDb.handleRemoveMessage({ messageId: message.id });
15231516
}
15241517
},
15251518
);

package/src/components/Chat/Chat.tsx

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,14 +163,6 @@ const ChatWithContext = (props: PropsWithChildren<ChatProps>) => {
163163
*/
164164
const { connectionRecovering, isOnline } = useIsOnline(client, closeConnectionOnBackground);
165165

166-
// const [initialisedDatabaseConfig, setInitialisedDatabaseConfig] = useState<{
167-
// initialised: boolean;
168-
// userID?: string;
169-
// }>({
170-
// initialised: false,
171-
// userID: client.userID,
172-
// });
173-
174166
const { initialized: offlineDbInitialized, userId: offlineDbUserId } =
175167
useStateStore(client.offlineDb?.state, selector) ?? {};
176168

package/src/store/OfflineDB.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export class OfflineDB extends AbstractOfflineDB {
7070

7171
deleteMessagesForChannel = api.deleteMessagesForChannel;
7272

73+
dropPendingTasks = api.dropPendingTasks;
74+
7375
hardDeleteMessage = api.deleteMessage;
7476

7577
softDeleteMessage = api.softDeleteMessage;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { createDeleteQuery } from '../sqlite-utils/createDeleteQuery';
2+
import { SqliteClient } from '../SqliteClient';
3+
4+
/**
5+
* dropPendingTasks - Drops all pending tasks from the DB given a specific messageId.
6+
* Useful for when we do some message actions on a failed message and then we decide to
7+
* delete it afterwards, removing it from the state.
8+
*
9+
* @param {Object} param
10+
* @param {string} param.messageId The messageId for which we want to remove the pending tasks.
11+
* @param {boolean} param.flush Whether we should immediately execute the query or return it as a prepared one.
12+
*
13+
* @return {() => void} - A function that can be called to remove the task from the database
14+
*/
15+
export const dropPendingTasks = async ({
16+
messageId,
17+
flush = true,
18+
}: {
19+
messageId: string;
20+
flush?: boolean;
21+
}) => {
22+
const queries = [createDeleteQuery('pendingTasks', { messageId })];
23+
SqliteClient.logger?.('info', 'dropPendingTasks', {
24+
messageId,
25+
});
26+
27+
if (flush) {
28+
await SqliteClient.executeSqlBatch(queries);
29+
}
30+
31+
return queries;
32+
};

package/src/store/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ export * from './deletePendingTask';
3030
export * from './getPendingTasks';
3131
export * from './softDeleteMessage';
3232
export * from './channelExists';
33+
export * from './dropPendingTasks';

package/src/store/schema.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import type { MessageLabel, Role } from 'stream-chat';
2-
3-
import type { PendingTaskTypes } from './types';
1+
import type { MessageLabel, PendingTaskTypes, Role } from 'stream-chat';
42

53
import type { ValueOf } from '../types/types';
64

0 commit comments

Comments
 (0)