|
|
import type { RgthreeModelInfo } from "typings/rgthree";
|
|
|
import { rgthreeApi } from "./rgthree_api.js";
|
|
|
|
|
|
import { api } from "../../scripts/api.js";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ModelInfoService extends EventTarget {
|
|
|
private readonly loraToInfo = new Map<string, RgthreeModelInfo | null>();
|
|
|
|
|
|
constructor() {
|
|
|
super();
|
|
|
api.addEventListener("rgthree-refreshed-lora-info", this.handleLoraAsyncUpdate.bind(this));
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private setFreshLoraData(file: string, info: RgthreeModelInfo) {
|
|
|
this.loraToInfo.set(file, info);
|
|
|
this.dispatchEvent(
|
|
|
new CustomEvent("rgthree-model-service-lora-details", { detail: { lora: info } }),
|
|
|
);
|
|
|
}
|
|
|
|
|
|
async getLora(file: string, refresh = false, light = false) {
|
|
|
if (this.loraToInfo.has(file) && !refresh) {
|
|
|
return this.loraToInfo.get(file)!;
|
|
|
}
|
|
|
return this.fetchLora(file, refresh, light);
|
|
|
}
|
|
|
|
|
|
async fetchLora(file: string, refresh = false, light = false) {
|
|
|
let info = null;
|
|
|
if (!refresh) {
|
|
|
info = await rgthreeApi.getLorasInfo(file, light);
|
|
|
} else {
|
|
|
info = await rgthreeApi.refreshLorasInfo(file);
|
|
|
}
|
|
|
if (!light) {
|
|
|
this.loraToInfo.set(file, info);
|
|
|
}
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
async refreshLora(file: string) {
|
|
|
return this.fetchLora(file, true);
|
|
|
}
|
|
|
|
|
|
async clearLoraFetchedData(file: string) {
|
|
|
await rgthreeApi.clearLorasInfo(file);
|
|
|
this.loraToInfo.delete(file);
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
async saveLoraPartial(file: string, data: Partial<RgthreeModelInfo>) {
|
|
|
let info = await rgthreeApi.saveLoraInfo(file, data);
|
|
|
this.loraToInfo.set(file, info);
|
|
|
return info;
|
|
|
}
|
|
|
|
|
|
private handleLoraAsyncUpdate(event: CustomEvent<{ data: RgthreeModelInfo }>) {
|
|
|
const info = event.detail?.data as RgthreeModelInfo;
|
|
|
if (info?.file) {
|
|
|
this.setFreshLoraData(info.file, info);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
export const SERVICE = new ModelInfoService();
|
|
|
|