export const onReview = publicProcedure.input(zCredentials.nullable()).subscription(async function* ({
input,
signal,
}) {
const onReview = async (channel: string, message: string) => {
try {
ee.emit(channel, message);
} catch (err) {
// eslint-disable-next-line no-console
console.error('err', err);
}
};
if (!pubsub.subscriber) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Failed to subscribe to review changes',
});
}
await pubsub.subscriber?.subscribe(SubscriptionEventChannel.WORK_REPORT_REVIEW_CHANGE);
pubsub.subscriber?.on('message', onReview);
try {
for await (const [message] of on(ee, SubscriptionEventChannel.WORK_REPORT_REVIEW_CHANGE, {
signal,
})) {
if (!input) return;
await tryAuthenticate(input);
const successfulReviews = JSON.parse(message ?? {});
yield successfulReviews;
}
} finally {
pubsub.subscriber?.off('message', onReview);
pubsub.subscriber?.unsubscribe(SubscriptionEventChannel.WORK_REPORT_REVIEW_CHANGE);
}
});
export const onReview = publicProcedure.input(zCredentials.nullable()).subscription(async function* ({
input,
signal,
}) {
const onReview = async (channel: string, message: string) => {
try {
ee.emit(channel, message);
} catch (err) {
// eslint-disable-next-line no-console
console.error('err', err);
}
};
if (!pubsub.subscriber) {
throw new TRPCError({
code: 'INTERNAL_SERVER_ERROR',
message: 'Failed to subscribe to review changes',
});
}
await pubsub.subscriber?.subscribe(SubscriptionEventChannel.WORK_REPORT_REVIEW_CHANGE);
pubsub.subscriber?.on('message', onReview);
try {
for await (const [message] of on(ee, SubscriptionEventChannel.WORK_REPORT_REVIEW_CHANGE, {
signal,
})) {
if (!input) return;
await tryAuthenticate(input);
const successfulReviews = JSON.parse(message ?? {});
yield successfulReviews;
}
} finally {
pubsub.subscriber?.off('message', onReview);
pubsub.subscriber?.unsubscribe(SubscriptionEventChannel.WORK_REPORT_REVIEW_CHANGE);
}
});