All files / src/Logger BaseErrorAlert.vue.js

88.88% Statements 32/36
62.5% Branches 10/16
100% Functions 13/13
88.88% Lines 32/36

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                1x   1x                 1x                             6x                     9x 9x           3x     3x 3x                       9x           6x 6x   6x 2x     6x     6x     6x               16x 9x   7x 7x             3x 3x 8x   3x 3x 1x       2x       2x 2x 2x       2x          
// @ts-check
 
import { filter, first, noop, now, orderBy, some, sortedLastIndexBy,
  takeRight } from "lodash";
import Vue from "vue";
import logger from "./BaseLogger";
import { BaseStoreMixin } from "../store/utils";
 
const TIMER_EXTRA_TIME = 100;
 
const options = {
  /** @type  JQuery<Element> | null */
  _elt: null,
  /** @type {{(...args: any[]): void}} */
  _errorCB: noop,
  /** @type  NodeJS.Timeout | null */
  _timer: null
};
 
const component = /** @type {V.Constructor<typeof options, any>} */ (Vue).extend({
  name: "BaseErrorAlert",
  ...options,
  mixins: [ BaseStoreMixin ],
  props: {
    timeout: { type: Number, default: 5000 },
    maxErrors: { type: Number, default: 5 }
  },
  /**
   * @return {{
   *  errors: Array<BaseLogger.Error>,
   *  loggerErrors: Array<BaseLogger.Error>
   * }}
   */
  data() {
    return {
      errors: [],
      loggerErrors: []
    };
  },
  computed: {
    /**
     * @type {() => Array<BaseLogger.Error>}
     */
    visibleErrors: function() {
      /** @type Array<BaseLogger.Error> */
      const lastErrors = takeRight(this.errors, this.maxErrors);
      return orderBy(lastErrors, [ "timestamp" ], [ "desc" ]);
    },
    /**
     * @returns {number}
     */
    nbAlerts() {
      return this.loggerErrors?.length ?? 0;
    },
    muteBtnClass() {
      if (some(this.loggerErrors, [ "type", "error" ])) {
        return [ "btn-danger" ];
      }
      else Eif (some(this.loggerErrors, [ "type", "warning" ])) {
        return [ "btn-warning" ];
      }
      else {
        return [ "btn-secondary" ];
      }
    },
    /**
     * @return {boolean}
     */
    muteAlerts() { return this.getBaseState([ "ui", "muteAlerts" ]); }
  },
  mounted() {
    /**
     * @param {BaseLogger.Error} error
     */
    this.$options._errorCB = (error) => {
      this.loggerErrors = logger.getErrors();
 
      if (!this.$options._timer) {
        this.$options._timer = setTimeout(this.expireErrors.bind(this),
          this.timeout + TIMER_EXTRA_TIME);
      }
      this.errors.splice(sortedLastIndexBy(this.errors, error, "timestamp"),
        0, error);
    };
    logger.on("error", this.$options._errorCB);
  },
  destroyed() {
    logger.removeListener("error", this.$options._errorCB);
  },
  methods: {
    /**
     * @param {BaseLogger.Error} error
     * @returns {Array<string>}
     */
    alertClass(error) {
      if (error.type === "error") {
        return [ "alert-danger" ];
      }
      else if (error.type === "warning") {
        return [ "alert-warning" ];
      }
      else E{
        return [ "alert-secondary" ];
      }
    },
    expireErrors() {
      const _now = now();
      this.errors = filter(this.errors,
        (error) => ((error.timestamp + this.timeout) > _now));
 
      const next = first(this.errors);
      if (next) {
        this.$options._timer = setTimeout(this.expireErrors.bind(this),
          Math.max(next.timestamp + this.timeout - _now, 100));
      }
      else {
        this.clearTimer();
      }
    },
    clearTimer() {
      Eif (this.$options._timer) {
        clearTimeout(this.$options._timer);
        this.$options._timer = null;
      }
    },
    toggleMuteAlerts() {
      this.commitBase("ui/update", { muteAlerts: !this.muteAlerts });
    }
  }
});
export default component;