1
0
mirror of https://github.com/koloml/furbooru-tagging-assistant.git synced 2025-12-23 23:02:58 +00:00

Making entity name public, trying to better support new types

This commit is contained in:
2024-11-27 22:48:31 +04:00
parent 666d374057
commit f6ab60d939
2 changed files with 22 additions and 10 deletions

View File

@@ -1,16 +1,28 @@
import {validateImportedEntity} from "$lib/extension/transporting/validators.js";
import {exportEntityToObject} from "$lib/extension/transporting/exporters.js";
import StorageEntity from "./base/StorageEntity.js";
import StorageEntity from "$lib/extension/base/StorageEntity.ts";
import {compressToEncodedURIComponent, decompressFromEncodedURIComponent} from "lz-string";
type EntityConstructor<T extends StorageEntity> =
(new (id: string, settings: Record<string, any>) => T)
& typeof StorageEntity;
export default class EntitiesTransporter<EntityType> {
readonly #targetEntityConstructor: new (...any: any[]) => EntityType;
export default class EntitiesTransporter<EntityType extends StorageEntity> {
readonly #targetEntityConstructor: EntityConstructor<EntityType>;
/**
* Name of the entity, exported directly from the constructor.
* @private
*/
get #entityName() {
// How the hell should I even do this?
return ((this.#targetEntityConstructor as any) as typeof StorageEntity)._entityName;
}
/**
* @param entityConstructor Class which should be used for import or export.
*/
constructor(entityConstructor: new (...any: any[]) => EntityType) {
if (!(entityConstructor.prototype instanceof StorageEntity)) {
throw new TypeError('Invalid class provided as the target for importing!');
}
constructor(entityConstructor: EntityConstructor<EntityType>) {
this.#targetEntityConstructor = entityConstructor;
}
@@ -23,7 +35,7 @@ export default class EntitiesTransporter<EntityType extends StorageEntity> {
validateImportedEntity(
importedObject,
this.#targetEntityConstructor._entityName
this.#entityName
);
return new this.#targetEntityConstructor(
@@ -45,7 +57,7 @@ export default class EntitiesTransporter<EntityType extends StorageEntity> {
const exportableObject = exportEntityToObject(
entityObject,
this.#targetEntityConstructor._entityName
this.#entityName
);
return JSON.stringify(exportableObject, null, 2);

View File

@@ -24,7 +24,7 @@ export default abstract class StorageEntity<SettingsType extends Object = {}> {
return this.#settings;
}
protected static _entityName: string = "entity";
public static readonly _entityName: string = "entity";
async save() {
await EntitiesController.updateEntity(