summaryrefslogtreecommitdiff
path: root/frontend/src/frozen-plugins-service.tsx
blob: fa43a99e9e6ff108dc31d9004fc069513d5405b7 (plain)
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
import { DeckyState } from './components/DeckyState';
import { PluginUpdateMapping } from './store';
import { getSetting, setSetting } from './utils/settings';

/**
 * A Service class for managing the state and actions related to the frozen plugins feature.
 *
 * It's mostly responsible for sending setting updates to the server and keeping the local state in sync.
 */
export class FrozenPluginService {
  constructor(private deckyState: DeckyState) {}

  init() {
    getSetting<string[]>('frozenPlugins', []).then((frozenPlugins) => {
      this.deckyState.setFrozenPlugins(frozenPlugins);
    });
  }

  /**
   * Sends the new frozen plugins list to the server and persists it locally in the decky state
   *
   * @param frozenPlugins The new list of frozen plugins
   */
  async update(frozenPlugins: string[]) {
    await setSetting('frozenPlugins', frozenPlugins);
    this.deckyState.setFrozenPlugins(frozenPlugins);

    // Remove pending updates for frozen plugins
    const updates = this.deckyState.publicState().updates;

    if (updates) {
      const filteredUpdates = new Map() as PluginUpdateMapping;
      updates.forEach((v, k) => {
        if (!frozenPlugins.includes(k)) {
          filteredUpdates.set(k, v);
        }
      });

      this.deckyState.setUpdates(filteredUpdates);
    }
  }

  /**
   * Refreshes the state of frozen plugins in the local state
   */
  async invalidate() {
    this.deckyState.setFrozenPlugins(await getSetting('frozenPlugins', []));
  }
}