feat(Collector): add lastCollectedTimestamp (#9044)

* feat(Collector): add lastCollectedTimestamp

* docs: mark props as nullable

Co-authored-by: Synbulat Biishev <syjalo.dev@gmail.com>

* refactor: assign property in constructor

---------

Co-authored-by: Synbulat Biishev <syjalo.dev@gmail.com>
Co-authored-by: Vlad Frangu <kingdgrizzle@gmail.com>
This commit is contained in:
Rodry
2023-03-12 19:59:09 +00:00
committed by GitHub
parent 334a51240a
commit 4458a13925
2 changed files with 17 additions and 0 deletions

View File

@@ -95,6 +95,20 @@ class Collector extends EventEmitter {
if (options.time) this._timeout = setTimeout(() => this.stop('time'), options.time).unref();
if (options.idle) this._idletimeout = setTimeout(() => this.stop('idle'), options.idle).unref();
/**
* The timestamp at which this collector last collected an item
* @type {?number}
*/
this.lastCollectedTimestamp = null;
}
/**
* The Date at which this collector last collected an item
* @type {?Date}
*/
get lastCollectedAt() {
return this.lastCollectedTimestamp && new Date(this.lastCollectedTimestamp);
}
/**
@@ -118,6 +132,7 @@ class Collector extends EventEmitter {
*/
this.emit('collect', ...args);
this.lastCollectedTimestamp = Date.now();
if (this._idletimeout) {
clearTimeout(this._idletimeout);
this._idletimeout = setTimeout(() => this.stop('idle'), this.options.idle).unref();

View File

@@ -1060,6 +1060,8 @@ export abstract class Collector<K, V, F extends unknown[] = []> extends EventEmi
public readonly client: Client;
public collected: Collection<K, V>;
public lastCollectedTimestamp: number | null;
public get lastCollectedAt(): Date | null;
public ended: boolean;
public get endReason(): string | null;
public filter: CollectorFilter<[V, ...F]>;