owltide/utils/client-map.ts
Hornwitser 985b8e0950 Refactor base types for entities and tombstones
Rename the base Entity type to ApiEntity, and the base EntityToombstone
to ApiTombstone to better reflect the reality that its only used in the
API interface and that the client and server types uses its own base if
any.

Remove EntityLiving and pull EntityTombstone out of of the base entity
type so that the types based on ApiEntity are always living entities and
if it's possible for it to contain tombstone this will be explicitly
told with the type including a union with ApiTombstone.

Refactor the types of the ClientEntity and ClientMap to better reflect
the types of the entities it stores and converts to/from.
2025-06-24 15:19:11 +02:00

153 lines
3.9 KiB
TypeScript

import type { ApiEntity, ApiTombstone } from "~/shared/types/api";
import type { Id } from "~/shared/types/common";
import { DateTime, Zone } from "~/shared/utils/luxon";
import { ClientEntity } from "~/utils/client-entity";
export interface EntityClass<Api extends ApiEntity, Ent extends ClientEntity<Api>> {
fromApi(api: Api, opts: { zone: Zone, locale: string }): Ent,
}
export class ClientMap<
Ent extends ClientEntity<Api>,
Api extends ApiEntity = Ent extends ClientEntity<infer Api> ? Api : never
> {
constructor(
public EntityClass: EntityClass<Api, Ent>,
public map: Map<Id, Ent>,
public tombstones: Map<Id, number>,
) {
}
get(id: Id) {
return this.map.get(id);
}
keys() {
return this.map.keys();
}
values() {
return this.map.values();
}
get size() {
return this.map.size;
}
isModified() {
return [...this.map.values()].some(entity => entity.isModified());
}
add(entity: Ent) {
if (this.map.has(entity.id)) {
throw new Error("ClientMap.add: Entity already exists");
}
this.map.set(entity.id, entity);
}
discard() {
for (const id of this.keys()) {
this.discardId(id);
}
}
discardId(id: Id) {
const entity = this.map.get(id);
if (!entity) {
throw new Error("ClientMap.discard: entity does not exist.");
}
if (
this.tombstones.has(id)
|| entity.isNew()
) {
this.map.delete(id);
} else {
entity.discard();
}
}
static fromApi<Api extends ApiEntity, Ent extends ClientEntity<Api>>(
EntityClass: EntityClass<Api, Ent>,
entities: (Api | ApiTombstone)[],
opts: { zone: Zone, locale: string },
) {
const living = entities.filter(entity => !entity.deleted);
const tombstones = entities.filter(entity => entity.deleted === true);
return new this(
EntityClass,
idMap(living.map(apiEntity => EntityClass.fromApi(apiEntity, opts))),
new Map(tombstones.map(tombstone => [tombstone.id, Date.parse(tombstone.updatedAt)])),
);
}
apiUpdate(entities: (Api | ApiTombstone)[], opts: { zone: Zone, locale: string }) {
const living = entities.filter(entity => !entity.deleted);
const tombstones = entities.filter(entity => entity.deleted === true);
for (const entity of living) {
const tombstoneMs = this.tombstones.get(entity.id);
const updatedMs = Date.parse(entity.updatedAt);
if (tombstoneMs !== undefined) {
if (tombstoneMs > updatedMs) {
continue;
}
this.tombstones.delete(entity.id);
}
const stored = this.map.get(entity.id);
if (stored) {
if (stored.updatedAt.toMillis() > updatedMs) {
continue;
}
stored.apiUpdate(entity, opts);
} else {
this.map.set(entity.id, this.EntityClass.fromApi(entity, opts))
}
}
for (const tombstone of tombstones) {
const updatedMs = Date.parse(tombstone.updatedAt);
const tombstoneMs = this.tombstones.get(tombstone.id);
const stored = this.map.get(tombstone.id);
if (
stored && stored.serverUpdatedAt.toMillis() > updatedMs
|| tombstoneMs && tombstoneMs > updatedMs
) {
continue;
}
this.tombstones.set(tombstone.id, updatedMs);
if (stored && stored.isModified() && !stored.deleted) {
stored.serverUpdatedAt = DateTime.fromISO(tombstone.updatedAt, opts);
stored.serverDeleted = true;
} else if (stored) {
this.map.delete(tombstone.id);
}
}
}
toApi(diff: boolean): (Api | ApiTombstone)[] {
if (!diff) {
return [
...[...this.map.values()].map(entity => entity.toApi()),
...[...this.tombstones].map(([id, updatedMs]) => ({
id,
updatedAt: new Date(updatedMs).toISOString(),
deleted: true as const,
})),
];
}
return [
...[...this.map.values()]
.filter(entity => entity.isModified() && !entity.deleted)
.map(entity => entity.toApi())
,
...[...this.map.values()]
.filter(entity => entity.deleted)
.map(entity => ({
id: entity.id,
updatedAt: toIso(entity.updatedAt),
deleted: true as const,
}))
,
];
}
}