queue.js 3.78 KB
/*
 * @Author: limin
 * @Date: 2019-12-06 14:58:35
 * @Last Modified by: limin
 * @Last Modified time: 2020-01-08 14:08:08
 */

// const items = new WeakMap(); //WeakMap对象是密钥/值对的集合,其中密钥被弱引用。键必须是对象,值可以是任意值。
// module.exports = class Queue {
//   constructor() {
//     items.set(this, []);
//   }

//   enqueue(...element) {
//     //向队列尾部添加一个(或多个)新的项
//     let q = items.get(this);
//     q.push(...element);
//   }

//   dequeue() {
//     //移除队列的第一个(排在队列最前面的)项,并返回被移除的元素。
//     let q = items.get(this);
//     let r = q.shift();
//     return r;
//   }

//   front() {
//     //返回队列中第一个元素——最先被添加,也将是最先被移除的元素。队列不做任何变动(不移除元素,只返回元素信息)
//     let q = items.get(this);
//     return q[0];
//   }

//   isEmpty() {
//     //如果队列中不包含任何元素,返回true,否则返回false。
//     return items.get(this).length == 0;
//   }

//   size() {
//     //返回队列包含的元素个数,与数组的length属性类似。
//     let q = items.get(this);
//     return q.length;
//   }

//   clear() {
//     //清空队列里面的元素。
//     items.set(this, []);
//   }

//   print() {
//     //打印队列为String到控制台
//     console.log(this.toString());
//   }

//   toString() {
//     //输出队列以String模式。
//     return items.get(this).toString();
//   }
// }

module.exports = class Queue {
  constructor({size,data = [],cache,fetch,const_id}) {
    this._elements = data;
    this._size = size;
    this._last_dequeue = data;
    this._cache = cache;
    this._fetch = fetch;
    this._const_id = const_id;
   cache.set(const_id,(data && data.length && data[data.length-1].id) || 0 )
  }
  isArray(arr) {
    return Object.prototype.toString.call(arr) === "[object Array]";
  }
  isEmpty() {
    return this._elements.length === 0;
  }
  q() {
    return this._elements;
  }
  size() {
    return this._size;
  }
  enqueue(...element) {
    if (this.isArray(...element)) {
      this._elements = this._elements.concat(...element);
      return;
    }
    this._elements.push(...element);
  }
  /**
   * 出列 前 N个对象 并返回 出列的对象
   * @param {*} n 出列 前N 个
   */
  dequeue(n) {
    if (this.isEmpty()) throw new Error("Queue is empty");
    if (n) {
      var r = [];
      this._elements = this._elements.filter((item, index) => {
        index < n && r.push(item);
        return index >= n;
      });
      return r;
    }
    return this._elements.shift();
  }
  peek() {
    this._last_dequeue = this._elements;
    this._elements = [];
    this.fill()
    return this._last_dequeue;
  }
  async fill() { 
    let  _last_id = await this._cache.get(this._const_id); //获取缓存的最后一个ID
    const result = await this._fetch.call(this._cache,{limit: this._size, last_id:_last_id}); //数据库获取数据
    const _result_size = result.length;
    if (!_result_size) {
      this.enqueue(this._last_dequeue);
    } else if (_result_size < this._size) {
      // 从上一次返回的数据中 获取相差的几条数据 补充到最新数据的末尾 保证队列数据的数量 公式为: [data][_result_size]
      const _last_data_list = this._last_dequeue.filter((item, index) => index >= _result_size);
      this.enqueue(...result, ..._last_data_list);
    } else {
      this.enqueue(result);
    }
    _last_id = _result_size? result.reduce((pre, next) => Math.max(pre, next.id), 0): _last_id;
    this._cache.set(this._const_id,_last_id) // redis 缓存最后一个ID
    return _last_id
  }
  first() {
    if (this.isEmpty()) throw new Error("Queue is empty");
    return this._elements[0];
  }
};