import { Queue,QueueItem } from '$utils/tool/queue';
export default <LibMixins>{

    data(){
        return {
            useQueueItem:null,
            queueTime:3000,
            triggerStatus:false
        }
    },

    methods:{
      pushQueue(data){

          // @ts-ignore
          data._id = new Date() * 1 + Math.floor(Math.random() * 1000);

          this.queue.push(new QueueItem(data));

          return this.triggerQueue();
      },

      // 执行队列
      triggerQueue(){

          if (!this.triggerStatus) {

              clearTimeout(this.triggerTime);

              this.triggerStatus = true;

              return this.triggerQueueItem();
          }

      },

      // 执行队列到具体的item
      triggerQueueItem(){
          // 获取第一个
          let item = this.queue.shift();
          if (item){
              this.useQueueItem = item.data;
              this.triggerChangeQueue && this.triggerChangeQueue();
          } else {
              this.triggerStatus = false;
              clearTimeout(this.triggerTime);
          }

          if (this.triggerStatus && this.queueTime) {
              // 获取下一个
              this.triggerTime = setTimeout(()=> this.triggerQueueItem(),this.queueTime);
          }
      }

    },

    created() {
        // 创建队列
        this.queue = new Queue();
    },

    beforeUnmount() {
        clearTimeout(this.triggerTime);
    }

}