mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-06-16 06:48:33 +00:00
refactor(api): clean up comments and improve abort handling in search route
This commit is contained in:
@ -166,7 +166,6 @@ export const POST = async (req: Request) => {
|
|||||||
|
|
||||||
const encoder = new TextEncoder();
|
const encoder = new TextEncoder();
|
||||||
|
|
||||||
// Create an AbortController to handle cancellation
|
|
||||||
const abortController = new AbortController();
|
const abortController = new AbortController();
|
||||||
const { signal } = abortController;
|
const { signal } = abortController;
|
||||||
|
|
||||||
@ -174,27 +173,21 @@ export const POST = async (req: Request) => {
|
|||||||
start(controller) {
|
start(controller) {
|
||||||
let sources: any[] = [];
|
let sources: any[] = [];
|
||||||
|
|
||||||
// Send an initial message to keep the connection alive
|
|
||||||
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
||||||
type: 'init',
|
type: 'init',
|
||||||
data: 'Stream connected'
|
data: 'Stream connected'
|
||||||
}) + "\n\n"));
|
}) + "\n\n"));
|
||||||
|
|
||||||
// Set up cleanup function for when client disconnects
|
|
||||||
signal.addEventListener('abort', () => {
|
signal.addEventListener('abort', () => {
|
||||||
// Remove all listeners from emitter to prevent memory leaks
|
|
||||||
emitter.removeAllListeners();
|
emitter.removeAllListeners();
|
||||||
|
|
||||||
// Close the controller if it's still active
|
|
||||||
try {
|
try {
|
||||||
controller.close();
|
controller.close();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// Controller might already be closed
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('data', (data: string) => {
|
emitter.on('data', (data: string) => {
|
||||||
// Check if request has been cancelled before processing
|
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -218,7 +211,6 @@ export const POST = async (req: Request) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('end', () => {
|
emitter.on('end', () => {
|
||||||
// Check if request has been cancelled before processing
|
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
controller.enqueue(encoder.encode("data: " + JSON.stringify({
|
||||||
@ -228,7 +220,6 @@ export const POST = async (req: Request) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
emitter.on('error', (error: any) => {
|
emitter.on('error', (error: any) => {
|
||||||
// Check if request has been cancelled before processing
|
|
||||||
if (signal.aborted) return;
|
if (signal.aborted) return;
|
||||||
|
|
||||||
controller.error(error);
|
controller.error(error);
|
||||||
|
Reference in New Issue
Block a user