|
| 1 | +import { EventEmitter } from 'events'; |
| 2 | + |
| 3 | +export interface PythagoreanCacheOpts { |
| 4 | + /** |
| 5 | + * Dump cache when queue reaches this size |
| 6 | + */ |
| 7 | + size?: number; |
| 8 | + |
| 9 | + /** |
| 10 | + * Dump cache every X milliseconds |
| 11 | + */ |
| 12 | + interval?: number; |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * @link https://en.wikipedia.org/wiki/Pythagorean_cup |
| 17 | + * |
| 18 | + * Creates a cache that will emit its items on: |
| 19 | + * - size: When the cache grows to this size, it will emit the items and reset |
| 20 | + * - interval: Every X milliseconds it will emit the items (if any) and reset |
| 21 | + * |
| 22 | + * @emits dump When the size/interval is met, it will emit the items in the cache and reset |
| 23 | + * |
| 24 | + * @class PythagoreanCache |
| 25 | + * @template T |
| 26 | + */ |
| 27 | +export class PythagoreanCache<T = any> extends EventEmitter { |
| 28 | + private queue: T[] = []; |
| 29 | + private interval: NodeJS.Timer; |
| 30 | + |
| 31 | + get length() { |
| 32 | + return this.queue.length; |
| 33 | + } |
| 34 | + |
| 35 | + constructor(private opts: PythagoreanCacheOpts) { |
| 36 | + super(); |
| 37 | + |
| 38 | + if (!opts.size && !opts.interval) { |
| 39 | + throw new Error('You must specify either a size or interval (or both)'); |
| 40 | + } |
| 41 | + |
| 42 | + this.startInterval(); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Push an item onto the cache |
| 47 | + * |
| 48 | + * @param items The items to push onto the cache |
| 49 | + * @returns the new size of the cache |
| 50 | + */ |
| 51 | + push(...items: T[]): number { |
| 52 | + this.queue.push(...items); |
| 53 | + if (this.checkLimit()) this.dump(); |
| 54 | + return this.length; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Check of the cache has reached its size limit |
| 59 | + * |
| 60 | + * @returns true if limit is reached |
| 61 | + */ |
| 62 | + checkLimit(): boolean { |
| 63 | + return this.opts.size ? this.length >= this.opts.size : false; |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Emit the items in the cache, it will do nothing if there are no items in cache |
| 68 | + */ |
| 69 | + dump(): void { |
| 70 | + if (this.length > 0) { |
| 71 | + const items = this.queue.splice(0, this.opts.size || this.length); |
| 72 | + this.emit('dump', items); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Change the size. This will trigger a dump event if newSize is smaller than the current length |
| 78 | + * |
| 79 | + * @param newSize The new size |
| 80 | + */ |
| 81 | + setSize(newSize: number): void { |
| 82 | + this.opts.size = newSize; |
| 83 | + if (this.checkLimit()) this.dump(); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Change the interval. This will cause the interval job to restart at zero. |
| 88 | + * |
| 89 | + * @param newInterval The new interval |
| 90 | + */ |
| 91 | + setInterval(newInterval: number): void { |
| 92 | + this.opts.interval = newInterval; |
| 93 | + this.startInterval(); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Start the interval dump (if set) |
| 98 | + */ |
| 99 | + startInterval() { |
| 100 | + this.stopInterval(); |
| 101 | + |
| 102 | + if (this.opts.interval) { |
| 103 | + this.interval = setInterval(() => { |
| 104 | + this.dump(); |
| 105 | + }, this.opts.interval); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Stop the interval check |
| 111 | + */ |
| 112 | + stopInterval(): void { |
| 113 | + if (this.interval) { |
| 114 | + clearInterval(this.interval); |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments