All files / stores scanning.js

95.45% Statements 21/22
62.5% Branches 5/8
100% Functions 5/5
95.45% Lines 21/22

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      2x 7x                   1x           5x 5x 5x   5x 5x     5x       1x 1x   1x 1x     1x 1x     4x 5x       2x 2x 2x       2x        
import { defineStore } from "pinia";
import { devicesApi } from "../api/modules/devices";
 
export const useScanningStore = defineStore("scanning", {
  state: () => ({
    mode: "setup",
    devices: [],
    isLoading: false,
    error: null,
    _scanAbortController: null,
  }),
 
  getters: {
    total(state) {
      return state.devices.length;
    },
  },
 
  actions: {
    async scan() {
      this._scanAbortController?.abort();
      const controller = new AbortController();
      this._scanAbortController = controller;
 
      this.isLoading = true;
      this.error = null;
 
      const result =
        this.mode === "setup"
          ? await devicesApi.scanningSetup({ signal: controller.signal })
          : await devicesApi.scanningAll({ signal: controller.signal });
 
      this._scanAbortController = null;
      this.isLoading = false;
 
      Eif (!result.ok) {
        Iif (result.error?.type === "timeout") {
          return result;
        }
        this.error = result.error;
        return result;
      }
 
      this.devices = result.data?.data?.devices || [];
      return result;
    },
 
    setMode(mode) {
      this.mode = mode;
      this.devices = [];
      this.error = null;
    },
 
    async setupDevice(payload) {
      return devicesApi.setupNewDevice(payload);
    },
  },
});