# All files / stores scripts.js

90.66% Statements 68/75
63.63% Branches 21/33
85.71% Functions 12/14
90.27% Lines 65/72

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    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
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151

    3x
    12x

    2x

    1x

    4x
    4x
    4x

    4x
    4x

    4x
    4x
    4x

    4x
    1x

    1x
    1x

    3x
    4x

    2x
    2x
    2x

    2x
    2x

    2x
    2x
    2x

    2x
    1x
    1x

    1x
    2x

    2x
    2x
    2x

    2x
    2x

    2x
    2x
    2x

    2x
    1x
    1x

    1x
    2x

    3x
    3x

    3x

    3x

    3x
    1x
    1x

    2x

    3x

    1x

    1x
    1x
    1x
    1x

    1x

    1x

    1x
    1x
    1x
    1x

    1x

     import { defineStore } from "pinia";
    import { scriptsApi } from "../api/modules/scripts";

    export const useScriptsStore = defineStore("scripts", {
      state: () => ({
        actions: [],
        regular: [],
        scopes: [],
        isLoadingActions: false,
        isLoadingRegular: false,
        isLoadingScopes: false,
        errorActions: null,
        errorRegular: null,
        errorScopes: null,
        runningAliases: new Set(),
        lastRunResult: null,
        _actionsAbortController: null,
        _regularAbortController: null,
        _scopesAbortController: null,
      }),

      getters: {
        totalActions: (state) => state.actions.length,
        totalRegular: (state) => state.regular.length,
        totalScopes: (state) => state.scopes.length,
        isRunning: (state) => (alias) => state.runningAliases.has(alias),
      },

      actions: {
        async loadActions() {
          this._actionsAbortController?.abort();
          const controller = new AbortController();
          this._actionsAbortController = controller;

          this.isLoadingActions = true;
          this.errorActions = null;

          const result = await scriptsApi.actionsList({ signal: controller.signal });
          this._actionsAbortController = null;
          this.isLoadingActions = false;

          if (!result.ok) {
            Iif (result.error?.type === "timeout") {
              return result;
            }
            this.errorActions = result.error;
            return result;
          }

          this.actions = result.data?.data?.scripts || [];
          return result;
        },

        async loadRegular() {
          this._regularAbortController?.abort();
          const controller = new AbortController();
          this._regularAbortController = controller;

          this.isLoadingRegular = true;
          this.errorRegular = null;

          const result = await scriptsApi.regularList({ signal: controller.signal });
          this._regularAbortController = null;
          this.isLoadingRegular = false;

          if (!result.ok) {
            Eif (result.error?.type === "timeout") {
              return result;
            }
            this.errorRegular = result.error;
            return result;
          }

          this.regular = result.data?.data?.scripts || [];
          return result;
        },

        async loadScopes() {
          this._scopesAbortController?.abort();
          const controller = new AbortController();
          this._scopesAbortController = controller;

          this.isLoadingScopes = true;
          this.errorScopes = null;

          const result = await scriptsApi.scopesList({ signal: controller.signal });
          this._scopesAbortController = null;
          this.isLoadingScopes = false;

          if (!result.ok) {
            Eif (result.error?.type === "timeout") {
              return result;
            }
            this.errorScopes = result.error;
            return result;
          }

          this.scopes = result.data?.data?.scopes || [];
          return result;
        },

        async runScript(alias, params = {}) {
          this.runningAliases.add(alias);
          this.lastRunResult = null;

          const result = await scriptsApi.runAction(alias, params);

          this.runningAliases.delete(alias);

          if (!result.ok) {
            this.lastRunResult = { alias, ok: false, error: result.error };
            return result;
          }

          this.lastRunResult = {
            alias,
            ok: true,
            data: result.data?.data?.return?.result,
            execTime: result.data?.data?.return?.exec_time,
          };
          return result;
        },

        async setRegularState(alias, enabled) {
          const result = await scriptsApi.setRegularState(alias, enabled);

          Eif (result.ok) {
            const idx = this.regular.findIndex((s) => s.alias === alias);
            Eif (idx !== -1) {
              this.regular[idx] = { ...this.regular[idx], state: enabled ? "enabled" : "disabled" };
            }
          }

          return result;
        },

        async setScopeState(name, enabled) {
          const result = await scriptsApi.setScopeState(name, enabled);

          Eif (result.ok) {
            const idx = this.scopes.findIndex((s) => s.name === name);
            Eif (idx !== -1) {
              this.scopes[idx] = { ...this.scopes[idx], state: enabled ? "enabled" : "disabled" };
            }
          }

          return result;
        },
      },
    });