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:
parent
e3ff872b5c
commit
985b8e0950
13 changed files with 107 additions and 102 deletions
|
@ -1,7 +1,8 @@
|
|||
import type { Entity, EntityLiving, Id } 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";
|
||||
|
||||
export abstract class ClientEntity {
|
||||
export abstract class ClientEntity<Api extends ApiEntity> {
|
||||
/**
|
||||
Millisecond offset used to indicate this is a new entitity.
|
||||
*/
|
||||
|
@ -73,10 +74,10 @@ export abstract class ClientEntity {
|
|||
/**
|
||||
Apply an update delivered from the API to this entity.
|
||||
*/
|
||||
abstract apiUpdate(api: EntityLiving, opts: { zone: Zone, locale: string }): void
|
||||
abstract apiUpdate(api: Api, opts: { zone: Zone, locale: string }): void
|
||||
|
||||
/**
|
||||
Serialise this entity to the API format. Not allowed if {@link deleted} is true.
|
||||
*/
|
||||
abstract toApi(): Entity
|
||||
abstract toApi(): Api | ApiTombstone
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@ import { ClientEntity } from "~/utils/client-entity";
|
|||
import { ClientUser } from "~/utils/client-user";
|
||||
import { describe, expect, test } from "vitest";
|
||||
import { DateTime, FixedOffsetZone } from "~/shared/utils/luxon";
|
||||
import type { ApiUser } from "~/shared/types/api";
|
||||
import type { ApiTombstone, ApiUser } from "~/shared/types/api";
|
||||
|
||||
const locale = "en-GB";
|
||||
const now = DateTime.now().setLocale(locale);
|
||||
|
@ -28,7 +28,7 @@ function fixtureClientMap() {
|
|||
);
|
||||
}
|
||||
|
||||
function fixtureApiMap(): ApiUser[] {
|
||||
function fixtureApiMap(): (ApiUser | ApiTombstone)[] {
|
||||
return [
|
||||
{
|
||||
id: 1,
|
||||
|
@ -183,7 +183,7 @@ describe("class ClientMap", () => {
|
|||
if (tomb)
|
||||
map.tombstones.set(1, tomb);
|
||||
// Update
|
||||
let update: ApiUser;
|
||||
let update: ApiUser | ApiTombstone;
|
||||
if (action === "A") {
|
||||
update = {
|
||||
id: 1,
|
||||
|
@ -224,7 +224,7 @@ describe("class ClientMap", () => {
|
|||
if (tomb)
|
||||
map.tombstones.set(1, tomb);
|
||||
// Update
|
||||
let update: ApiUser;
|
||||
let update: ApiUser | ApiTombstone;
|
||||
if (action === "A") {
|
||||
update = {
|
||||
id: 1,
|
||||
|
|
|
@ -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()),
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
import { ClientEntity } from "~/utils/client-entity";
|
||||
import { ClientSchedule, ClientScheduleEventSlot, ClientScheduleLocation, ClientScheduleShiftSlot, toIso } from "~/utils/client-schedule";
|
||||
import { describe, expect, test } from "vitest";
|
||||
import type { ApiSchedule } from "~/shared/types/api";
|
||||
import type { Id, Living } from "~/shared/types/common";
|
||||
import type { ApiEntity, ApiSchedule } from "~/shared/types/api";
|
||||
import { DateTime, FixedOffsetZone } from "~/shared/utils/luxon";
|
||||
|
||||
const locale = "en-GB";
|
||||
|
@ -70,7 +69,7 @@ function fixtureClientSchedule(multiSlot = false) {
|
|||
return schedule;
|
||||
}
|
||||
|
||||
function fixtureApiSchedule(): Living<ApiSchedule> {
|
||||
function fixtureApiSchedule(): ApiSchedule {
|
||||
return {
|
||||
id: 111,
|
||||
updatedAt: nowIso,
|
||||
|
@ -164,7 +163,7 @@ describe("class ClientSchedule", () => {
|
|||
expect(schedule.toApi(false)).toEqual(fixtureApiSchedule())
|
||||
});
|
||||
|
||||
const entityTests: [string, (schedule: ClientSchedule) => ClientEntity][] = [
|
||||
const entityTests: [string, (schedule: ClientSchedule) => ClientEntity<ApiEntity>][] = [
|
||||
[
|
||||
"location",
|
||||
() => ClientScheduleLocation.create(3, "New location", "", { zone, locale })
|
||||
|
|
|
@ -1,23 +1,25 @@
|
|||
import { DateTime, FixedOffsetZone, Zone } from "~/shared/utils/luxon";
|
||||
import type {
|
||||
ApiEntity,
|
||||
ApiSchedule,
|
||||
ApiScheduleEvent,
|
||||
ApiScheduleEventSlot,
|
||||
ApiScheduleLocation,
|
||||
ApiScheduleRole,
|
||||
ApiScheduleShift,
|
||||
ApiScheduleShiftSlot
|
||||
ApiScheduleShiftSlot,
|
||||
ApiTombstone
|
||||
} from "~/shared/types/api";
|
||||
import type { Entity, Id, Living, Tombstone } from "~/shared/types/common";
|
||||
import { mapEquals, setEquals } from "~/shared/utils/functions";
|
||||
import type { Id } from "~/shared/types/common";
|
||||
import { setEquals } from "~/shared/utils/functions";
|
||||
import { ClientEntity } from "~/utils/client-entity";
|
||||
|
||||
function filterAlive<T extends Entity>(entities?: T[]) {
|
||||
return (entities ?? []).filter((entity) => !entity.deleted) as Living<T>[];
|
||||
function filterEntity<T extends ApiEntity>(entities?: (T | ApiTombstone)[]) {
|
||||
return (entities ?? []).filter((entity) => !entity.deleted) as T[];
|
||||
}
|
||||
|
||||
function filterTombstone<T extends Entity>(entities?: T[]) {
|
||||
return (entities ?? []).filter((entity) => entity.deleted) as Tombstone<T>[];
|
||||
function filterTombstone<T extends ApiEntity>(entities?: (T | ApiTombstone)[]) {
|
||||
return (entities ?? []).filter((entity) => entity.deleted) as ApiTombstone[];
|
||||
}
|
||||
|
||||
export function toIso(timestamp: DateTime) {
|
||||
|
@ -37,7 +39,7 @@ function mapWithout<K, V>(map: Map<K, V>, key: K) {
|
|||
}
|
||||
|
||||
|
||||
export class ClientScheduleLocation extends ClientEntity {
|
||||
export class ClientScheduleLocation extends ClientEntity<ApiScheduleLocation> {
|
||||
serverName: string;
|
||||
serverDescription: string;
|
||||
|
||||
|
@ -86,7 +88,7 @@ export class ClientScheduleLocation extends ClientEntity {
|
|||
);
|
||||
}
|
||||
|
||||
static fromApi(api: Living<ApiScheduleLocation>, opts: { zone: Zone, locale: string }) {
|
||||
static fromApi(api: ApiScheduleLocation, opts: { zone: Zone, locale: string }) {
|
||||
return new this(
|
||||
api.id,
|
||||
DateTime.fromISO(api.updatedAt, opts),
|
||||
|
@ -96,7 +98,7 @@ export class ClientScheduleLocation extends ClientEntity {
|
|||
);
|
||||
}
|
||||
|
||||
override apiUpdate(api: Living<ApiScheduleLocation>, opts: { zone: Zone, locale: string }) {
|
||||
override apiUpdate(api: ApiScheduleLocation, opts: { zone: Zone, locale: string }) {
|
||||
const wasModified = this.isModified();
|
||||
this.serverUpdatedAt = DateTime.fromISO(api.updatedAt, opts);
|
||||
this.serverDeleted = false;
|
||||
|
@ -107,7 +109,7 @@ export class ClientScheduleLocation extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
toApi(): ApiScheduleLocation {
|
||||
toApi(): ApiScheduleLocation | ApiTombstone {
|
||||
if (this.deleted) {
|
||||
return {
|
||||
id: this.id,
|
||||
|
@ -124,7 +126,7 @@ export class ClientScheduleLocation extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
export class ClientScheduleEvent extends ClientEntity {
|
||||
export class ClientScheduleEvent extends ClientEntity<ApiScheduleEvent> {
|
||||
schedule!: ClientSchedule;
|
||||
serverName: string;
|
||||
serverCrew: boolean;
|
||||
|
@ -237,7 +239,7 @@ export class ClientScheduleEvent extends ClientEntity {
|
|||
}
|
||||
|
||||
static fromApi(
|
||||
api: Living<ApiScheduleEvent>,
|
||||
api: ApiScheduleEvent,
|
||||
opts: { zone: Zone, locale: string },
|
||||
) {
|
||||
return new this(
|
||||
|
@ -255,7 +257,7 @@ export class ClientScheduleEvent extends ClientEntity {
|
|||
}
|
||||
|
||||
override apiUpdate(
|
||||
api: Living<ApiScheduleEvent>,
|
||||
api: ApiScheduleEvent,
|
||||
opts: { zone: Zone, locale: string },
|
||||
) {
|
||||
const wasModified = this.isModified();
|
||||
|
@ -273,7 +275,7 @@ export class ClientScheduleEvent extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
toApi(): ApiScheduleEvent {
|
||||
toApi(): ApiScheduleEvent | ApiTombstone {
|
||||
if (this.deleted) {
|
||||
return {
|
||||
id: this.id,
|
||||
|
@ -437,7 +439,7 @@ export class ClientScheduleEventSlot {
|
|||
}
|
||||
}
|
||||
|
||||
export class ClientScheduleRole extends ClientEntity {
|
||||
export class ClientScheduleRole extends ClientEntity<ApiScheduleRole> {
|
||||
serverName: string;
|
||||
serverDescription: string;
|
||||
|
||||
|
@ -486,7 +488,7 @@ export class ClientScheduleRole extends ClientEntity {
|
|||
);
|
||||
}
|
||||
|
||||
static fromApi(api: Living<ApiScheduleRole>, opts: { zone: Zone, locale: string }) {
|
||||
static fromApi(api: ApiScheduleRole, opts: { zone: Zone, locale: string }) {
|
||||
return new this(
|
||||
api.id,
|
||||
DateTime.fromISO(api.updatedAt, opts),
|
||||
|
@ -496,7 +498,7 @@ export class ClientScheduleRole extends ClientEntity {
|
|||
);
|
||||
}
|
||||
|
||||
override apiUpdate(api: Living<ApiScheduleRole>, opts: { zone: Zone, locale: string }) {
|
||||
override apiUpdate(api: ApiScheduleRole, opts: { zone: Zone, locale: string }) {
|
||||
const wasModified = this.isModified();
|
||||
this.serverUpdatedAt = DateTime.fromISO(api.updatedAt, opts);
|
||||
this.serverDeleted = false;
|
||||
|
@ -507,7 +509,7 @@ export class ClientScheduleRole extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
toApi(): ApiScheduleRole {
|
||||
toApi(): ApiScheduleRole | ApiTombstone {
|
||||
if (this.deleted) {
|
||||
return {
|
||||
id: this.id,
|
||||
|
@ -524,7 +526,7 @@ export class ClientScheduleRole extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
export class ClientScheduleShift extends ClientEntity {
|
||||
export class ClientScheduleShift extends ClientEntity<ApiScheduleShift> {
|
||||
schedule!: ClientSchedule;
|
||||
serverRoleId: Id;
|
||||
serverName: string;
|
||||
|
@ -616,7 +618,7 @@ export class ClientScheduleShift extends ClientEntity {
|
|||
}
|
||||
|
||||
static fromApi(
|
||||
api: Living<ApiScheduleShift>,
|
||||
api: ApiScheduleShift,
|
||||
opts: { zone: Zone, locale: string },
|
||||
) {
|
||||
return new this(
|
||||
|
@ -631,7 +633,7 @@ export class ClientScheduleShift extends ClientEntity {
|
|||
}
|
||||
|
||||
override apiUpdate(
|
||||
api: Living<ApiScheduleShift>,
|
||||
api: ApiScheduleShift,
|
||||
opts: { zone: Zone, locale: string },
|
||||
) {
|
||||
const wasModified = this.isModified();
|
||||
|
@ -646,7 +648,7 @@ export class ClientScheduleShift extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
toApi(): ApiScheduleShift {
|
||||
toApi(): ApiScheduleShift | ApiTombstone {
|
||||
if (this.deleted) {
|
||||
return {
|
||||
id: this.id,
|
||||
|
@ -787,7 +789,7 @@ export class ClientScheduleShiftSlot {
|
|||
}
|
||||
}
|
||||
|
||||
export class ClientSchedule extends ClientEntity {
|
||||
export class ClientSchedule extends ClientEntity<ApiSchedule> {
|
||||
nextClientId = -1;
|
||||
|
||||
constructor(
|
||||
|
@ -854,7 +856,7 @@ export class ClientSchedule extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
static fromApi(api: Living<ApiSchedule>, opts: { zone: Zone, locale: string }) {
|
||||
static fromApi(api: ApiSchedule, opts: { zone: Zone, locale: string }) {
|
||||
const eventSlots = idMap((api.events ?? [])
|
||||
.filter(event => !event.deleted)
|
||||
.flatMap(event => event.slots.map(slot => ClientScheduleEventSlot.fromApi(slot, event.id, opts)))
|
||||
|
@ -890,7 +892,7 @@ export class ClientSchedule extends ClientEntity {
|
|||
return schedule;
|
||||
}
|
||||
|
||||
toApi(diff = false): ApiSchedule {
|
||||
toApi(diff = false): ApiSchedule | ApiTombstone {
|
||||
if (this.deleted) {
|
||||
return {
|
||||
id: this.id,
|
||||
|
@ -908,7 +910,7 @@ export class ClientSchedule extends ClientEntity {
|
|||
}
|
||||
}
|
||||
|
||||
override apiUpdate(update: Living<ApiSchedule>, opts: { zone: Zone, locale: string }) {
|
||||
override apiUpdate(update: ApiSchedule, opts: { zone: Zone, locale: string }) {
|
||||
if (update.deleted)
|
||||
throw new Error("ClientSchedule.apiUpdate: Unexpected deletion");
|
||||
if (update.id !== this.id)
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
import type { ApiUser, ApiUserType } from "~/shared/types/api";
|
||||
import type { Entity, EntityLiving, Id, Living } from "~/shared/types/common";
|
||||
import type { Id } from "~/shared/types/common";
|
||||
import { DateTime, Zone } from "~/shared/utils/luxon";
|
||||
import { ClientEntity } from "~/utils/client-entity";
|
||||
|
||||
export class ClientUser extends ClientEntity {
|
||||
export class ClientUser extends ClientEntity<ApiUser> {
|
||||
serverName: string | undefined;
|
||||
serverType: ApiUserType
|
||||
|
||||
|
@ -52,7 +52,7 @@ export class ClientUser extends ClientEntity {
|
|||
)
|
||||
}
|
||||
|
||||
static fromApi(api: Living<ApiUser>, opts: { zone: Zone, locale: string }) {
|
||||
static fromApi(api: ApiUser, opts: { zone: Zone, locale: string }) {
|
||||
return new this(
|
||||
api.id,
|
||||
DateTime.fromISO(api.updatedAt, opts),
|
||||
|
@ -62,7 +62,7 @@ export class ClientUser extends ClientEntity {
|
|||
);
|
||||
}
|
||||
|
||||
override apiUpdate(api: Living<ApiUser>, opts: { zone: Zone, locale: string }) {
|
||||
override apiUpdate(api: ApiUser, opts: { zone: Zone, locale: string }) {
|
||||
const wasModified = this.isModified();
|
||||
this.serverUpdatedAt = DateTime.fromISO(api.updatedAt, opts);
|
||||
this.serverDeleted = false;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue