export default {
  data() {
    return {
      pollingInterval: 300000,
      pending: false,
      pause: false,
      timer: null
    }
  },
  activated() {
    if (this.pending) {
      this.startPolling()
    }
  },
  deactivated() {
    if (this.pending) {
      this.pause = true
    }
  },
  methods: {
    startPolling() {
      if (this.pending && !this.pause) return
      this.pending = true
      this.pause = false
      this.callPolling({ immediate: true })
    },
    stopPolling() {
      this.pending = false
      if (this.timer) {
        clearTimeout(this.timer)
        this.timer = null
      }
    },
    callPolling(options = {}) {
      // 轮询中止跳出
      if (!this.pending || this.pause) return
      const { immediate } = options
      if (immediate) this.getData()
      const pollingInterval = this.pollingInterval
      this.timer = setTimeout(() => {
        this.callPolling()
        /*轮询体 start,需要组件内部实现*/
        this.getData()
        /*轮询体 end*/
      }, pollingInterval)
    }
  }
}