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.
This commit is contained in:
Hornwitser 2025-06-24 15:19:11 +02:00
parent e3ff872b5c
commit 985b8e0950
13 changed files with 107 additions and 102 deletions

View file

@ -1,15 +1,19 @@
import { type Entity, type EntityLiving, type Id, type Living } from "~/shared/types/common";
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<T extends ClientEntity> {
fromApi(api: EntityLiving, opts: { zone: Zone, locale: string }): T,
export interface EntityClass<Api extends ApiEntity, Ent extends ClientEntity<Api>> {
fromApi(api: Api, opts: { zone: Zone, locale: string }): Ent,
}
export class ClientMap<T extends ClientEntity> {
export class ClientMap<
Ent extends ClientEntity<Api>,
Api extends ApiEntity = Ent extends ClientEntity<infer Api> ? Api : never
> {
constructor(
public EntityClass: EntityClass<T>,
public map: Map<Id, T>,
public EntityClass: EntityClass<Api, Ent>,
public map: Map<Id, Ent>,
public tombstones: Map<Id, number>,
) {
}
@ -34,7 +38,7 @@ export class ClientMap<T extends ClientEntity> {
return [...this.map.values()].some(entity => entity.isModified());
}
add(entity: T) {
add(entity: Ent) {
if (this.map.has(entity.id)) {
throw new Error("ClientMap.add: Entity already exists");
}
@ -62,12 +66,12 @@ export class ClientMap<T extends ClientEntity> {
}
}
static fromApi<T extends Entity, U extends ClientEntity>(
EntityClass: EntityClass<U>,
entities: T[],
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) as Living<T>[];
const living = entities.filter(entity => !entity.deleted);
const tombstones = entities.filter(entity => entity.deleted === true);
return new this(
EntityClass,
@ -76,7 +80,7 @@ export class ClientMap<T extends ClientEntity> {
);
}
apiUpdate(entities: Entity[], opts: { zone: Zone, locale: string }) {
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) {
@ -119,7 +123,7 @@ export class ClientMap<T extends ClientEntity> {
}
}
toApi(diff: boolean): Entity[] {
toApi(diff: boolean): (Api | ApiTombstone)[] {
if (!diff) {
return [
...[...this.map.values()].map(entity => entity.toApi()),