Refactor API types and sync logic
Rename and refactor the types passed over the API to be based on an entity that's either living or a tombstone. A living entity has a deleted property that's either undefined or false, while a tombstone has a deleted property set to true. All entities have a numeric id and an updatedAt timestamp. To sync entities, an array of replacements are passed around. Living entities are replaced with tombstones when they're deleted. And tombstones are replaced with living entities when restored.
This commit is contained in:
parent
251e83f640
commit
fe06d0d6bd
36 changed files with 1242 additions and 834 deletions
|
@ -1,6 +1,6 @@
|
|||
import { Schedule } from "~/shared/types/schedule"
|
||||
import { readAccounts } from "~/server/database";
|
||||
import { canSeeCrew } from "./utils/schedule";
|
||||
import type { ApiAccount, ApiEvent } from "~/shared/types/api";
|
||||
|
||||
function sendMessage(
|
||||
stream: WritableStream<string>,
|
||||
|
@ -65,22 +65,58 @@ export function cancelSessionStreams(sessionId: number) {
|
|||
}
|
||||
}
|
||||
|
||||
export async function broadcastUpdate(schedule: Schedule) {
|
||||
const encodeEventCache = new WeakMap<ApiEvent, Map<ApiAccount["type"] | undefined, string>>();
|
||||
function encodeEvent(event: ApiEvent, accountType: ApiAccount["type"] | undefined) {
|
||||
const cache = encodeEventCache.get(event);
|
||||
const cacheEntry = cache?.get(accountType);
|
||||
if (cacheEntry) {
|
||||
return cacheEntry;
|
||||
}
|
||||
|
||||
let data: string;
|
||||
if (event.type === "schedule-update") {
|
||||
if (!canSeeCrew(accountType)) {
|
||||
event = {
|
||||
type: event.type,
|
||||
updatedFrom: event.updatedFrom,
|
||||
data: filterSchedule(event.data),
|
||||
};
|
||||
}
|
||||
data = JSON.stringify(event);
|
||||
} else {
|
||||
throw Error(`encodeEvent cannot encode ${event.type} event`);
|
||||
}
|
||||
|
||||
if (cache) {
|
||||
cache.set(accountType, data);
|
||||
} else {
|
||||
encodeEventCache.set(event, new Map([[accountType, data]]));
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
export async function broadcastEvent(event: ApiEvent) {
|
||||
const id = Date.now();
|
||||
console.log(`broadcasting update to ${streams.size} clients`);
|
||||
if (!streams.size) {
|
||||
return;
|
||||
}
|
||||
const accounts = await readAccounts();
|
||||
const filteredSchedule = filterSchedule(schedule);
|
||||
for (const [stream, streamData] of streams) {
|
||||
let accountType: string | undefined;
|
||||
if (streamData.accountId !== undefined) {
|
||||
accountType = accounts.find(a => a.id === streamData.accountId)?.type
|
||||
// Account events are specially handled and only sent to the account they belong to.
|
||||
if (event.type === "account-update") {
|
||||
if (streamData.accountId === event.data.id) {
|
||||
sendMessage(stream, `id: ${id}\nevent: update\ndata: ${JSON.stringify(event)}\n\n`);
|
||||
}
|
||||
|
||||
} else {
|
||||
let accountType: ApiAccount["type"] | undefined;
|
||||
if (streamData.accountId !== undefined) {
|
||||
accountType = accounts.find(a => a.id === streamData.accountId)?.type
|
||||
}
|
||||
const data = encodeEvent(event, accountType)
|
||||
sendMessage(stream, `id: ${id}\nevent: update\ndata: ${data}\n\n`);
|
||||
}
|
||||
const data = JSON.stringify(canSeeCrew(accountType) ? schedule : filteredSchedule);
|
||||
const message = `id: ${id}\nevent: update\ndata: ${data}\n\n`
|
||||
sendMessage(stream, message);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue