import { Button } from 'element-ui';
import { Subject } from 'rxjs';
import { debounceTime } from 'rxjs/operators';

export default {
  name: 'el-button',
  extends: Button,
  props: {
    debounce: { type: Number },
  },
  data() {
    return {
      subject: null,
    };
  },
  created() {
    if (this.debounce) {
      const subject = new Subject();
      subject.pipe(debounceTime(this.debounce)).subscribe({
        next: e => this.$emit('click', e),
      });
      this.subject = subject;
    }
  },
  methods: {
    handleClick(e) {
      if (this.debounce) {
        this.subject.next(e);
      } else {
        this.$emit('click', e);
      }
    },
  },
};
