Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F32179149
D15255.1765072001.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
D15255.1765072001.diff
View Options
diff --git a/lib/shared/farcaster/farcaster-hooks.js b/lib/shared/farcaster/farcaster-hooks.js
--- a/lib/shared/farcaster/farcaster-hooks.js
+++ b/lib/shared/farcaster/farcaster-hooks.js
@@ -20,6 +20,20 @@
import { useDispatch } from '../../utils/redux-utils.js';
import { useSendDMOperationUtils } from '../dm-ops/dm-op-utils.js';
+async function processInBatches<T, R>(
+ items: $ReadOnlyArray<T>,
+ batchSize: number,
+ processor: (item: T) => Promise<R>,
+): Promise<Array<R>> {
+ const results: Array<R> = [];
+ for (let i = 0; i < items.length; i += batchSize) {
+ const batch = items.slice(i, i + batchSize);
+ const batchResults = await Promise.all(batch.map(processor));
+ results.push(...batchResults);
+ }
+ return results;
+}
+
function useFarcasterConversationsSync(): (limit: number) => Promise<void> {
const [fullInbox, setFullInbox] = React.useState(false);
const [conversations, setConversations] = React.useState<
@@ -73,18 +87,20 @@
void (async () => {
const farcasterConversations: Array<FarcasterConversation> = [];
- for (const conversationId of conversations) {
+
+ const fetchConversation = async (
+ conversationId: string,
+ ): Promise<?FarcasterConversation> => {
try {
const conversationResult = await fetchFarcasterConversation({
conversationId,
});
if (!conversationResult) {
- continue;
+ return null;
}
const farcasterConversation = conversationResult.result.conversation;
- farcasterConversations.push(farcasterConversation);
const thread = createFarcasterRawThreadInfo(farcasterConversation);
const update = {
type: updateTypes.JOIN_THREAD,
@@ -103,12 +119,25 @@
updateInfos: [update],
},
});
+
+ return farcasterConversation;
} catch (e) {
console.error(`Failed fetching conversation ${conversationId}:`, e);
+ return null;
}
- }
+ };
- for (const farcasterConversation of farcasterConversations) {
+ const conversationResults = await processInBatches(
+ conversations,
+ 20,
+ fetchConversation,
+ );
+
+ farcasterConversations.push(...conversationResults.filter(Boolean));
+
+ const fetchMessagesForConversation = async (
+ farcasterConversation: FarcasterConversation,
+ ): Promise<void> => {
try {
let cursor: ?string = null;
let totalMessagesFetched = 0;
@@ -164,7 +193,13 @@
e,
);
}
- }
+ };
+
+ await processInBatches(
+ farcasterConversations,
+ 20,
+ fetchMessagesForConversation,
+ );
setConversations([]);
setInProgress(false);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sun, Dec 7, 1:46 AM (9 h, 56 s)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
5842000
Default Alt Text
D15255.1765072001.diff (2 KB)
Attached To
Mode
D15255: [lib] Process conversations and messages in batches
Attached
Detach File
Event Timeline
Log In to Comment