All files / redis-smq-monitor-app/src/plugins/message-rate/common message-rate-writer.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 5/5
100% Lines 17/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 4257x   57x     57x       55x     55x 55x     55x 319x 319x 281x 281x 281x   38x                 55x 317x       55x 55x      
import { events } from 'redis-smq';
import { TMessageRateFields } from '../../../../types';
import { Ticker } from 'redis-smq-common';
import { ICallback } from 'redis-smq-common/dist/types';
 
export abstract class MessageRateWriter<
  TRateFields extends TMessageRateFields,
> {
  protected writerTicker: Ticker;
  protected rateStack: [number, TMessageRateFields][] = [];
 
  constructor() {
    this.writerTicker = new Ticker(this.updateTimeSeries);
    this.writerTicker.nextTick();
  }
 
  protected updateTimeSeries = (): void => {
    const item = this.rateStack.shift();
    if (item) {
      const [ts, rates] = item;
      this.onUpdate(ts, rates, () => {
        this.writerTicker.nextTick();
      });
    } else this.writerTicker.nextTick();
  };
 
  abstract onUpdate(
    ts: number,
    rates: TMessageRateFields,
    cb: ICallback<void>,
  ): void;
 
  onRateTick = (ts: number, rates: TRateFields): void => {
    this.rateStack.push([ts, rates]);
  };
 
  quit(cb: ICallback<void>): void {
    this.writerTicker.on(events.DOWN, cb);
    this.writerTicker.quit();
  }
}