refactor: front end code to unified manager api

Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
2026-06-20 18:56:35 -04:00
parent 053c272fe4
commit c4cb1512d2
21 changed files with 1484 additions and 927 deletions
+89 -40
View File
@@ -5,34 +5,85 @@
import type {
ServiceInterface,
ServiceCapabilitiesInterface,
ServiceIdentity,
ServiceLocation
ServiceLocation,
ServiceModelInterface
} from "@/types/service";
import { Identity } from './identity';
import { Location } from './location';
import { MutationProxy } from './mutation-proxy';
import { clonePlain } from './clone-plain';
export class ServiceObject implements ServiceInterface {
export class ServiceObject implements ServiceModelInterface {
private _original: ServiceInterface;
private _mutated: Partial<ServiceInterface>;
private _mutationProxy = new MutationProxy<ServiceInterface>(() => this._original, () => this._mutated);
_data!: ServiceInterface;
_location: Location | null | undefined = undefined;
_identity: Identity | null | undefined = undefined;
constructor() {
this._data = {
this._original = {
'@type': 'people:service',
version: 1,
provider: '',
identifier: null,
label: null,
enabled: false,
capabilities: {}
};
this._mutated = {};
this._data = this._mutationProxy.create();
}
fromJson(data: ServiceInterface): ServiceObject {
this._data = data;
this._original = clonePlain(data);
this._mutated = {};
this._data = this._mutationProxy.create();
this._location = undefined;
this._identity = undefined;
return this;
}
toJson(): ServiceInterface {
return this._data;
toJson(): ServiceInterface;
toJson(delta: true): Partial<ServiceInterface>;
toJson(delta?: boolean): ServiceInterface | Partial<ServiceInterface> {
if (delta) {
const json: Partial<ServiceInterface> = clonePlain(this._mutated);
if (this._location?.mutated()) {
json.location = this._location.toJson(true) as ServiceInterface['location'];
}
if (this._identity?.mutated()) {
json.identity = this._identity.toJson(true) as ServiceInterface['identity'];
}
return json;
}
const json: ServiceInterface = {
...clonePlain(this._original),
...clonePlain(this._mutated),
};
if (this._location !== undefined) {
json.location = this._location ? this._location.toJson() : null;
}
if (this._identity !== undefined) {
json.identity = this._identity ? this._identity.toJson() : null;
}
return json;
}
clone(): ServiceObject {
return new ServiceObject().fromJson(this.toJson());
}
mutated(): boolean {
return Reflect.ownKeys(this._mutated).length > 0 || (this._location?.mutated() ?? false) || (this._identity?.mutated() ?? false);
}
capable(capability: keyof ServiceCapabilitiesInterface): boolean {
@@ -49,10 +100,6 @@ export class ServiceObject implements ServiceInterface {
/** Immutable Properties */
get '@type'(): string {
return this._data['@type'];
}
get provider(): string {
return this._data.provider;
}
@@ -61,8 +108,8 @@ export class ServiceObject implements ServiceInterface {
return this._data.identifier;
}
get capabilities(): ServiceCapabilitiesInterface | undefined {
return this._data.capabilities;
get capabilities(): ServiceCapabilitiesInterface {
return this._data.capabilities ?? {};
}
/** Mutable Properties */
@@ -83,46 +130,48 @@ export class ServiceObject implements ServiceInterface {
this._data.enabled = value;
}
get location(): ServiceLocation | null {
return this._data.location ?? null;
get location(): Location | null {
if (this._location !== undefined) {
return this._location;
}
if (this._data.location) {
this._location = Location.fromJson(this._data.location as ServiceLocation);
return this._location;
}
this._location = null;
return null;
}
set location(value: ServiceLocation | null) {
this._data.location = value;
set location(value: Location | null) {
this._location = value;
}
get identity(): ServiceIdentity | null {
return this._data.identity ?? null;
get identity(): Identity | null {
if (this._identity !== undefined) {
return this._identity;
}
if (this._data.identity) {
this._identity = Identity.fromJson(this._data.identity);
return this._identity;
}
this._identity = null;
return null;
}
set identity(value: ServiceIdentity | null) {
this._data.identity = value;
set identity(value: Identity | null) {
this._identity = value;
}
get auxiliary(): Record<string, any> {
return this._data.auxiliary ?? {};
}
set auxiliary(value: Record<string, any>) {
this._data.auxiliary = value;
}
/** Helper Methods */
/**
* Get identity as a class instance for easier manipulation
*/
getIdentity(): Identity | null {
if (!this._data.identity) return null;
return Identity.fromJson(this._data.identity);
}
/**
* Get location as a class instance for easier manipulation
*/
getLocation(): Location | null {
if (!this._data.location) return null;
return Location.fromJson(this._data.location);
}
}