Maybe this
This commit is contained in:
parent
2c85c17ee2
commit
236475ed38
4 changed files with 18 additions and 25 deletions
|
@ -2,15 +2,6 @@ import Ably from "ably";
|
|||
import { env } from "~/env.mjs";
|
||||
import type { EventType } from "../utils/types";
|
||||
|
||||
const globalForAbly = globalThis as unknown as {
|
||||
ably: Ably.Types.RealtimePromise;
|
||||
};
|
||||
|
||||
export const ably =
|
||||
globalForAbly.ably || new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
||||
|
||||
if (env.NODE_ENV !== "production") globalForAbly.ably = ably;
|
||||
|
||||
export const publishToChannel = async (
|
||||
ablyInstance: Ably.Types.RealtimePromise,
|
||||
channel: string,
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
import { z } from "zod";
|
||||
import { publishToChannel } from "~/server/ably";
|
||||
import Ably from "ably";
|
||||
|
||||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
||||
|
||||
import { fetchCache, invalidateCache, setCache } from "~/server/redis";
|
||||
|
@ -17,8 +15,6 @@ export const roomRouter = createTRPCRouter({
|
|||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
if (ctx.session) {
|
||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
||||
|
||||
const room = await ctx.prisma.room.create({
|
||||
data: {
|
||||
userId: ctx.session.user.id,
|
||||
|
@ -33,7 +29,7 @@ export const roomRouter = createTRPCRouter({
|
|||
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
|
||||
|
||||
await publishToChannel(
|
||||
ably,
|
||||
ctx.ably,
|
||||
`${ctx.session.user.id}`,
|
||||
"ROOM_LIST_UPDATE",
|
||||
"CREATE"
|
||||
|
@ -125,8 +121,6 @@ export const roomRouter = createTRPCRouter({
|
|||
})
|
||||
)
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
||||
|
||||
if (input.reset) {
|
||||
if (input.log) {
|
||||
const oldRoom = await ctx.prisma.room.findUnique({
|
||||
|
@ -209,7 +203,12 @@ export const roomRouter = createTRPCRouter({
|
|||
});
|
||||
|
||||
if (newRoom) {
|
||||
await publishToChannel(ably, `${newRoom.id}`, "ROOM_UPDATE", "UPDATE");
|
||||
await publishToChannel(
|
||||
ctx.ably,
|
||||
`${newRoom.id}`,
|
||||
"ROOM_UPDATE",
|
||||
"UPDATE"
|
||||
);
|
||||
}
|
||||
|
||||
return !!newRoom;
|
||||
|
@ -219,8 +218,6 @@ export const roomRouter = createTRPCRouter({
|
|||
delete: protectedProcedure
|
||||
.input(z.object({ id: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
||||
|
||||
const deletedRoom = await ctx.prisma.room.delete({
|
||||
where: {
|
||||
id: input.id,
|
||||
|
@ -233,14 +230,14 @@ export const roomRouter = createTRPCRouter({
|
|||
await invalidateCache(`kv_roomlist_${ctx.session.user.id}`);
|
||||
|
||||
await publishToChannel(
|
||||
ably,
|
||||
ctx.ably,
|
||||
`${ctx.session.user.id}`,
|
||||
"ROOM_LIST_UPDATE",
|
||||
"DELETE"
|
||||
);
|
||||
|
||||
await publishToChannel(
|
||||
ably,
|
||||
ctx.ably,
|
||||
`${deletedRoom.id}`,
|
||||
"ROOM_UPDATE",
|
||||
"DELETE"
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import { z } from "zod";
|
||||
import { publishToChannel } from "~/server/ably";
|
||||
import Ably from "ably";
|
||||
|
||||
import type { Room } from "@prisma/client";
|
||||
import { createTRPCRouter, protectedProcedure } from "~/server/api/trpc";
|
||||
|
@ -68,8 +67,6 @@ export const voteRouter = createTRPCRouter({
|
|||
set: protectedProcedure
|
||||
.input(z.object({ value: z.string(), roomId: z.string() }))
|
||||
.mutation(async ({ ctx, input }) => {
|
||||
const ably = new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY);
|
||||
|
||||
const vote = await ctx.prisma.vote.upsert({
|
||||
where: {
|
||||
userId_roomId: {
|
||||
|
@ -104,7 +101,12 @@ export const voteRouter = createTRPCRouter({
|
|||
await invalidateCache(`kv_votecount_admin`);
|
||||
await invalidateCache(`kv_votes_${input.roomId}`);
|
||||
|
||||
await publishToChannel(ably, `${vote.roomId}`, "VOTE_UPDATE", "UPDATE");
|
||||
await publishToChannel(
|
||||
ctx.ably,
|
||||
`${vote.roomId}`,
|
||||
"VOTE_UPDATE",
|
||||
"UPDATE"
|
||||
);
|
||||
}
|
||||
|
||||
return !!vote;
|
||||
|
|
|
@ -19,6 +19,7 @@ import { type Session } from "next-auth";
|
|||
|
||||
import { getServerAuthSession } from "~/server/auth";
|
||||
import { prisma } from "~/server/db";
|
||||
import Ably from "ably";
|
||||
|
||||
type CreateContextOptions = {
|
||||
session: Session | null;
|
||||
|
@ -37,6 +38,7 @@ type CreateContextOptions = {
|
|||
const createInnerTRPCContext = (opts: CreateContextOptions) => {
|
||||
return {
|
||||
session: opts.session,
|
||||
ably: new Ably.Realtime.Promise(env.ABLY_PRIVATE_KEY),
|
||||
prisma,
|
||||
};
|
||||
};
|
||||
|
@ -65,6 +67,7 @@ export const createTRPCContext = async (opts: CreateNextContextOptions) => {
|
|||
*/
|
||||
import { initTRPC, TRPCError } from "@trpc/server";
|
||||
import superjson from "superjson";
|
||||
import { env } from "~/env.mjs";
|
||||
|
||||
const t = initTRPC.context<typeof createTRPCContext>().create({
|
||||
transformer: superjson,
|
||||
|
|
Loading…
Add table
Reference in a new issue