iceshrimp-161sh/packages/backend/src/misc/antenna-cache.ts

37 lines
824 B
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import { Antennas } from "@/models/index.js";
import type { Antenna } from "@/models/entities/antenna.js";
import { subscriber } from "@/db/redis.js";
2021-03-23 07:06:56 +01:00
let antennasFetched = false;
let antennas: Antenna[] = [];
export async function getAntennas() {
if (!antennasFetched) {
antennas = await Antennas.find();
antennasFetched = true;
}
return antennas;
}
2023-01-13 05:40:33 +01:00
subscriber.on("message", async (_, data) => {
2021-03-23 07:06:56 +01:00
const obj = JSON.parse(data);
2023-01-13 05:40:33 +01:00
if (obj.channel === "internal") {
2021-03-23 07:06:56 +01:00
const { type, body } = obj.message;
switch (type) {
2023-01-13 05:40:33 +01:00
case "antennaCreated":
2021-03-23 07:06:56 +01:00
antennas.push(body);
break;
2023-01-13 05:40:33 +01:00
case "antennaUpdated":
antennas[antennas.findIndex((a) => a.id === body.id)] = body;
2021-03-23 07:06:56 +01:00
break;
2023-01-13 05:40:33 +01:00
case "antennaDeleted":
antennas = antennas.filter((a) => a.id !== body.id);
2021-03-23 07:06:56 +01:00
break;
default:
break;
}
}
});