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 @@ -68,18 +68,21 @@ const FARCASTER_DATA_BATCH_SIZE = 20; const MAX_RETRIES = 3; const RETRY_DELAY_MS = 1000; +const MAX_BATCH_MESSAGE_COUNT = 1000; class BatchedUpdates { userIDs: Set; updateInfos: Array; messageInfos: Array; additionalMessageInfos: Array; + messageCount: number; constructor() { this.userIDs = new Set(); this.updateInfos = ([]: Array); this.messageInfos = ([]: Array); this.additionalMessageInfos = ([]: Array); + this.messageCount = 0; } addUserID(userID: string): void { @@ -92,22 +95,32 @@ addUpdateInfo(updateInfo: ClientUpdateInfo): void { this.updateInfos.push(updateInfo); + if ( + updateInfo.type === updateTypes.JOIN_THREAD && + updateInfo.rawMessageInfos + ) { + this.messageCount += updateInfo.rawMessageInfos.length; + } } addMessageInfo(messageInfo: RawMessageInfo): void { this.messageInfos.push(messageInfo); + this.messageCount += 1; } addMessageInfos(messageInfos: Array): void { this.messageInfos.push(...messageInfos); + this.messageCount += messageInfos.length; } addAdditionalMessageInfo(messageInfo: RawMessageInfo): void { this.additionalMessageInfos.push(messageInfo); + this.messageCount += 1; } addAdditionalMessageInfos(messageInfos: Array): void { this.additionalMessageInfos.push(...messageInfos); + this.messageCount += messageInfos.length; } isEmpty(): boolean { @@ -119,11 +132,16 @@ ); } + getMessageCount(): number { + return this.messageCount; + } + merge(other: BatchedUpdates): void { other.userIDs.forEach(userID => this.userIDs.add(userID)); this.updateInfos.push(...other.updateInfos); this.messageInfos.push(...other.messageInfos); this.additionalMessageInfos.push(...other.additionalMessageInfos); + this.messageCount += other.messageCount; } getReduxPayload(): ProcessFarcasterOpsPayload { @@ -185,22 +203,28 @@ const batchResults = await Promise.all(batchPromises); - const allBatchedUpdates = new BatchedUpdates(); - const validResults: Array = []; + let batchedUpdates = new BatchedUpdates(); for (const itemResult of batchResults) { if (itemResult) { - validResults.push(itemResult.result); - allBatchedUpdates.merge(itemResult.updates); + results.push(itemResult.result); + batchedUpdates.merge(itemResult.updates); + const messagesCount = batchedUpdates.getMessageCount(); + + if (messagesCount > MAX_BATCH_MESSAGE_COUNT) { + dispatch({ + type: processFarcasterOpsActionType, + payload: batchedUpdates.getReduxPayload(), + }); + batchedUpdates = new BatchedUpdates(); + } } } - results.push(...validResults); - - if (!allBatchedUpdates.isEmpty()) { + if (!batchedUpdates.isEmpty()) { dispatch({ type: processFarcasterOpsActionType, - payload: allBatchedUpdates.getReduxPayload(), + payload: batchedUpdates.getReduxPayload(), }); }