Skip to content

Commit

Permalink
fix: do not send heartbeat to closed controller
Browse files Browse the repository at this point in the history
  • Loading branch information
backrunner committed Aug 25, 2024
1 parent 84ce79d commit 0842c17
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/pages/api/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,4 +191,4 @@ export const POST: APIRoute = async ({ request, locals }) => {
headers: { 'Content-Type': 'application/json' },
});
}
};
};
7 changes: 6 additions & 1 deletion src/pages/api/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ export const GET: APIRoute = async ({ request, locals }) => {
return;
}
try {
controller.enqueue(`event: heartbeat\ndata: ${new Date().toISOString()}\n\n`);
// Check if the controller is closed
if (controller.desiredSize !== null) {
controller.enqueue(`event: heartbeat\ndata: ${new Date().toISOString()}\n\n`);
} else {
throw new Error('Controller is closed');
}
} catch (error) {
console.error('Error sending heartbeat:', error);
clearInterval(heartbeatInterval);
Expand Down

0 comments on commit 0842c17

Please sign in to comment.