Skip to content

Commit d236bf7

Browse files
committed
Change checkPurchaseStatus interval from every second to every 5 seconds for performance
1 parent e5af3b1 commit d236bf7

File tree

1 file changed

+26
-26
lines changed

1 file changed

+26
-26
lines changed

components/stocks/stock-purchase.tsx

+26-26
Original file line numberDiff line numberDiff line change
@@ -63,40 +63,40 @@ export function Purchase({
6363

6464
useEffect(() => {
6565
const checkPurchaseStatus = () => {
66-
if (purchaseStatus === 'requires_action') {
67-
// check for purchase completion
68-
// Find the tool message with the matching toolCallId
69-
const toolMessage = aiState.messages.find(
70-
message =>
71-
message.role === 'tool' &&
72-
message.content.some(part => part.toolCallId === toolCallId)
73-
);
74-
75-
if (toolMessage) {
76-
const toolMessageIndex = aiState.messages.indexOf(toolMessage);
77-
// Check if the next message is a system message containing "purchased"
78-
const nextMessage = aiState.messages[toolMessageIndex + 1];
79-
if (
80-
nextMessage?.role === 'system' &&
81-
nextMessage.content.includes('purchased')
82-
) {
83-
setPurchaseStatus('completed');
84-
} else {
85-
// Check for expiration
86-
const requestedAt = toolMessage.createdAt;
87-
if (!requestedAt || unixTsNow() - requestedAt > 30) {
88-
setPurchaseStatus('expired');
89-
}
66+
if (purchaseStatus !== 'requires_action') {
67+
return;
68+
}
69+
// check for purchase completion
70+
// Find the tool message with the matching toolCallId
71+
const toolMessage = aiState.messages.find(
72+
message =>
73+
message.role === 'tool' &&
74+
message.content.some(part => part.toolCallId === toolCallId)
75+
);
76+
77+
if (toolMessage) {
78+
const toolMessageIndex = aiState.messages.indexOf(toolMessage);
79+
// Check if the next message is a system message containing "purchased"
80+
const nextMessage = aiState.messages[toolMessageIndex + 1];
81+
if (
82+
nextMessage?.role === 'system' &&
83+
nextMessage.content.includes('purchased')
84+
) {
85+
setPurchaseStatus('completed');
86+
} else {
87+
// Check for expiration
88+
const requestedAt = toolMessage.createdAt;
89+
if (!requestedAt || unixTsNow() - requestedAt > 30) {
90+
setPurchaseStatus('expired');
9091
}
9192
}
9293
}
9394
};
94-
9595
checkPurchaseStatus();
9696

9797
let intervalId: NodeJS.Timeout | null = null;
9898
if (purchaseStatus === 'requires_action') {
99-
intervalId = setInterval(checkPurchaseStatus, 1000);
99+
intervalId = setInterval(checkPurchaseStatus, 5000);
100100
}
101101

102102
return () => {

0 commit comments

Comments
 (0)