<template>
  <div style="display:none" />
</template>
<script>
import Shake from 'shake.js'
export default {
  props: {
    stop: Boolean,
    threshold: {
      type: Number,
      default: 15
    },
    timeout: {
      type: Number,
      default: 1000
    }
  },
  mounted() {
    const _this = this
    this._shake = new Shake({
      threshold: _this.threshold, // optional shake strength threshold
      timeout: _this.timeout // optional, determines the frequency of event generation
    })
    window.addEventListener('shake', this.handler, false)
    this._shake.start()
  },
  beforeDestroy() {
    window.removeEventListener('shake', this.handler, false)
  },
  methods: {
    handler() {
      const _this = this
      if (_this.stop) {
        this.$emit('on-shake')
      } else {
        this._shake.stop()
      }
    }
  }
}
</script>
