File size: 3,355 Bytes
4450790
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import type {RgthreeModelInfo} from "typings/rgthree.js";
import {rgthreeApi} from "./rgthree_api.js";
import {api} from "scripts/api.js";

/**
 * Abstract class defining information syncing for different types.
 */
abstract class BaseModelInfoService extends EventTarget {
  private readonly fileToInfo = new Map<string, RgthreeModelInfo | null>();

  protected abstract apiRefreshEventString: string;

  protected abstract apiFetchInfo(file: string, light: boolean): Promise<RgthreeModelInfo | null>;
  protected abstract apiRefreshInfo(file: string): Promise<RgthreeModelInfo | null>;
  protected abstract apiSaveInfo(
    file: string,
    data: Partial<RgthreeModelInfo>,
  ): Promise<RgthreeModelInfo | null>;
  protected abstract apiClearInfo(file: string): Promise<void>;

  constructor() {
    super();
    this.init();
  }

  private init() {
    api.addEventListener(
      this.apiRefreshEventString,
      this.handleAsyncUpdate.bind(this) as EventListener,
    );
  }

  async getInfo(file: string, refresh: boolean, light: boolean) {
    if (this.fileToInfo.has(file) && !refresh) {
      return this.fileToInfo.get(file)!;
    }
    return this.fetchInfo(file, refresh, light);
  }

  async refreshInfo(file: string) {
    return this.fetchInfo(file, true);
  }

  async clearFetchedInfo(file: string) {
    await this.apiClearInfo(file);
    // await rgthreeApi.clearLorasInfo(file);
    this.fileToInfo.delete(file);
    return null;
  }

  async savePartialInfo(file: string, data: Partial<RgthreeModelInfo>) {
    let info = await this.apiSaveInfo(file, data);
    this.fileToInfo.set(file, info);
    return info;
  }

  handleAsyncUpdate(event: CustomEvent<{data: RgthreeModelInfo}>) {
    const info = event.detail?.data as RgthreeModelInfo;
    if (info?.file) {
      this.setFreshInfo(info.file, info);
    }
  }

  private async fetchInfo(file: string, refresh = false, light = false) {
    let info = null;
    if (!refresh) {
      info = await this.apiFetchInfo(file, light);
      // info = await rgthreeApi.getLorasInfo(file, light);
    } else {
      info = await this.apiRefreshInfo(file);
      // info = await rgthreeApi.refreshLorasInfo(file);
    }
    if (!light) {
      this.fileToInfo.set(file, info);
    }
    return info;
  }

  /**
   * 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 setFreshInfo(file: string, info: RgthreeModelInfo) {
    this.fileToInfo.set(file, info);
    // this.dispatchEvent(
    //   new CustomEvent("rgthree-model-service-lora-details", { detail: { lora: info } }),
    // );
  }
}

/**
 * Lora type implementation of ModelInfoTypeService.
 */
class LoraInfoService extends BaseModelInfoService {
  protected apiRefreshEventString = "rgthree-refreshed-lora-info";

  protected override apiFetchInfo(file: string, light: boolean) {
    return rgthreeApi.getLorasInfo(file, light);
  }
  protected override apiRefreshInfo(file: string) {
    return rgthreeApi.refreshLorasInfo(file);
  }
  protected override apiSaveInfo(file: string, data: Partial<RgthreeModelInfo>) {
    return rgthreeApi.saveLoraInfo(file, data);
  }
  protected override apiClearInfo(file: string) {
    return rgthreeApi.clearLorasInfo(file);
  }
}


export const LORA_INFO_SERVICE = new LoraInfoService();