File size: 2,115 Bytes
d051564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { RgthreeModelInfo } from "typings/rgthree.js";
import { rgthreeApi } from "./rgthree_api.js";
import { api } from "scripts/api.js";

/**
 * A singleton service to fetch and cache model infos from rgthree-comfy.
 */
class ModelInfoService extends EventTarget {
  private readonly loraToInfo = new Map<string, RgthreeModelInfo | null>();

  constructor() {
    super();
    api.addEventListener(
      "rgthree-refreshed-lora-info",
      this.handleLoraAsyncUpdate.bind(this) as EventListener,
    );
  }

  /**
   * Single point to set data into the info cache, and fire an event. Note, this doesn't determine
   * if the data is actually different.
   */
  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();