export const mediaGeneted = protectedProcedure
.input(mediaGenetedSchema)
.subscription(async function* (opts) {
const { input, signal } = opts;
const { chatId } = input;
const eventKey = `media-test`;
console.log("🔥🔥🔥 SUBSCRIPTION STARTED:");
console.log(" - Event Key:", eventKey);
console.log(" - Chat ID:", chatId);
console.log(
" - Initial Listener Count:",
mediaEvents.listenerCount(eventKey),
);
try {
for await (const [data] of on(mediaEvents, eventKey, { signal })) {
console.log("🔥🔥🔥 EVENT RECEIVED");
console.log(" - Raw data:", data);
console.log(" - Data type:", typeof data);
console.log(" - Data keys:", data ? Object.keys(data) : "null");
const event = data as MediaEvent;
if (event && event.chatId === chatId) {
console.log("🔥🔥🔥 YIELDING EVENT:");
console.log(" - Event Chat ID:", event.chatId);
console.log(" - Expected Chat ID:", chatId);
console.log(" - Match:", event.chatId === chatId);
yield tracked(event.chatId, event);
console.log("🔥🔥🔥 EVENT YIELDED SUCCESSFULLY");
} else {
console.log("🔥🔥🔥 EVENT FILTERED OUT:");
console.log(" - Event Chat ID:", event?.chatId);
console.log(" - Expected Chat ID:", chatId);
console.log(" - Event exists:", !!event);
}
}
} catch (error) {
if (
error instanceof Error &&
(error.name === "AbortError" || error.message?.includes("aborted"))
) {
console.log("🔥 Subscription aborted (normal)");
return;
}
console.error("🔥 Subscription error:", error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Subscription failed",
cause: error,
});
}
});
export const mediaGeneted = protectedProcedure
.input(mediaGenetedSchema)
.subscription(async function* (opts) {
const { input, signal } = opts;
const { chatId } = input;
const eventKey = `media-test`;
console.log("🔥🔥🔥 SUBSCRIPTION STARTED:");
console.log(" - Event Key:", eventKey);
console.log(" - Chat ID:", chatId);
console.log(
" - Initial Listener Count:",
mediaEvents.listenerCount(eventKey),
);
try {
for await (const [data] of on(mediaEvents, eventKey, { signal })) {
console.log("🔥🔥🔥 EVENT RECEIVED");
console.log(" - Raw data:", data);
console.log(" - Data type:", typeof data);
console.log(" - Data keys:", data ? Object.keys(data) : "null");
const event = data as MediaEvent;
if (event && event.chatId === chatId) {
console.log("🔥🔥🔥 YIELDING EVENT:");
console.log(" - Event Chat ID:", event.chatId);
console.log(" - Expected Chat ID:", chatId);
console.log(" - Match:", event.chatId === chatId);
yield tracked(event.chatId, event);
console.log("🔥🔥🔥 EVENT YIELDED SUCCESSFULLY");
} else {
console.log("🔥🔥🔥 EVENT FILTERED OUT:");
console.log(" - Event Chat ID:", event?.chatId);
console.log(" - Expected Chat ID:", chatId);
console.log(" - Event exists:", !!event);
}
}
} catch (error) {
if (
error instanceof Error &&
(error.name === "AbortError" || error.message?.includes("aborted"))
) {
console.log("🔥 Subscription aborted (normal)");
return;
}
console.error("🔥 Subscription error:", error);
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Subscription failed",
cause: error,
});
}
});