import * as _ from 'lodash'; import moment from 'moment'; export interface IAQueueOptions { batch?: number; } export abstract class AQueue { list: any = []; batch; constructor ({ batch = 1, }: IAQueueOptions) { this.batch = batch; } async add(items) { if (!_.isArray(items)) { items = [items]; } const fmtItems = items.map((item) => { return { ...item, scraperUpdatedAt: moment().format(), scraperUpdatedAtTimestamp: Date.now(), scraperUpdatedAtDate: moment().format('YYYY-MM-DD'), }; }); this.list = [...this.list, ...fmtItems]; if (this.list.length >= this.batch) { const toSend = [...this.list]; this.list = []; await this.send(toSend); } } abstract send(items); }