# All files / core breadcrumb.ts

97.87% Statements 46/47
75% Branches 27/36
100% Functions 10/10
97.67% Lines 42/43

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
    891x
    1x
    1x

    1x
    1x
    1x
    1x

    22x
    22x
    2x

    2x
    2x
    2x

    2x
    1x
    1x

    20x

    1x
    21x
    21x
    4x

    21x
    21x

    1x
    4x

    1x
    3x

    1x
    3x

    1x
    12x

    2x

    2x

    2x

    6x

    2x
    4x
    2x
    2x

    1x
    1x
    1x
     import { BREADCRUMBTYPES, BREADCRUMBCATEGORYS } from '../common/constant'
    import { logger, validateOption, getTimestamp, slientConsoleScope } from '../utils/index'
    import { _support } from '../utils/global'
    import { BreadcrumbPushData } from '../types/breadcrumb'
    import { InitOptions } from '../types/options'

    export class Breadcrumb {
      private maxBreadcrumbs = 10
      private beforePushBreadcrumb: unknown = null
      private stack: BreadcrumbPushData[] = []
      constructor() {}
      /**
       * 添加用户行为栈
       *
       * ../param {BreadcrumbPushData} data
       * ../memberof Breadcrumb
       */
      push(data: BreadcrumbPushData): void {
        if (typeof this.beforePushBreadcrumb === 'function') {
          let result: BreadcrumbPushData = null
          // 如果用户输入console，并且logger是打开的会造成无限递归，
          // 应该加入一个开关，执行这个函数前，把监听console的行为关掉
          const beforePushBreadcrumb = this.beforePushBreadcrumb
          slientConsoleScope(() => {
            result = beforePushBreadcrumb(this, data)
          })
          if (!result) return
          this.immediatePush(result)
          return
        }
        this.immediatePush(data)
      }
      immediatePush(data: BreadcrumbPushData): void {
        data.time = getTimestamp()
        if (this.stack.length >= this.maxBreadcrumbs) {
          this.shift()
        }
        this.stack.push(data)
        logger.log(this.stack)
      }
      shift(): boolean {
        return this.stack.shift() !== undefined
      }
      clear(): void {
        this.stack = []
      }
      getStack(): BreadcrumbPushData[] {
        return this.stack
      }
      getCategory(type: BREADCRUMBTYPES) {
        switch (type) {
          case BREADCRUMBTYPES.XHR:
          case BREADCRUMBTYPES.FETCH:
            return BREADCRUMBCATEGORYS.HTTP
          case BREADCRUMBTYPES.CLICK:
          case BREADCRUMBTYPES.ROUTE:
          case BREADCRUMBTYPES.TAP:
          case BREADCRUMBTYPES.TOUCHMOVE:
            return BREADCRUMBCATEGORYS.USER
          case BREADCRUMBTYPES.CUSTOMER:
          case BREADCRUMBTYPES.CONSOLE:
            return BREADCRUMBCATEGORYS.DEBUG
          case BREADCRUMBTYPES.APP_ON_LAUNCH:
          case BREADCRUMBTYPES.APP_ON_SHOW:
          case BREADCRUMBTYPES.APP_ON_HIDE:
          case BREADCRUMBTYPES.PAGE_ON_SHOW:
          case BREADCRUMBTYPES.PAGE_ON_HIDE:
          case BREADCRUMBTYPES.PAGE_ON_SHARE_APP_MESSAGE:
          case BREADCRUMBTYPES.PAGE_ON_SHARE_TIMELINE:
          case BREADCRUMBTYPES.PAGE_ON_TAB_ITEM_TAP:
            return BREADCRUMBCATEGORYS.LIFECYCLE
          case BREADCRUMBTYPES.UNHANDLEDREJECTION:
          case BREADCRUMBTYPES.CODE_ERROR:
          case BREADCRUMBTYPES.RESOURCE:
          case BREADCRUMBTYPES.VUE:
          case BREADCRUMBTYPES.REACT:
          default:
            return BREADCRUMBCATEGORYS.EXCEPTION
        }
      }
      bindOptions(Ioptions: InitOptions = {}): void {
        const { maxBreadcrumbs, beforePushBreadcrumb } = options
        validateOption(maxBreadcrumbs, 'maxBreadcrumbs', 'number') && (this.maxBreadcrumbs = maxBreadcrumbs)
        validateOption(beforePushBreadcrumb, 'beforePushBreadcrumb', 'function') && (this.beforePushBreadcrumb = beforePushBreadcrumb)
      }
    }
    const breadcrumb = _support.breadcrumb || (_support.breadcrumb = new Breadcrumb())
    export { breadcrumb }