mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-31 00:16:47 +01:00
e22f6a04ba
This gives us better types for our wrapTimer function. Maybe the type `(args: any) => any` could also be improved --------- Co-authored-by: Nuno Góis <nuno@getunleash.io>
42 lines
1.0 KiB
TypeScript
42 lines
1.0 KiB
TypeScript
import type EventEmitter from 'events';
|
|
import timer from './timer';
|
|
|
|
// wrapTimer keeps track of the timing of a async operation and emits
|
|
// a event on the given eventBus once the operation is complete
|
|
//
|
|
// the returned function is designed to stop the timer and emit the event
|
|
//
|
|
// Usage:
|
|
// Define the timer function. It can be done once per class.
|
|
// this.timer = (action: string) =>
|
|
// metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
|
// store: 'client-feature-toggle-read-model',
|
|
// action,
|
|
// });
|
|
//
|
|
// Before performing an operation, start the timer:
|
|
// const stopTimer = this.timer(`timer-name`);
|
|
// // perform operation and then stop timer
|
|
// stopTimer();
|
|
|
|
const wrapTimer = (
|
|
eventBus: EventEmitter,
|
|
event: string,
|
|
args: Record<string, unknown> = {},
|
|
) => {
|
|
const t = timer.new();
|
|
return (data: unknown) => {
|
|
args.time = t();
|
|
eventBus.emit(event, args);
|
|
return data;
|
|
};
|
|
};
|
|
|
|
const metricsHelper = {
|
|
wrapTimer,
|
|
};
|
|
export default metricsHelper;
|
|
module.exports = {
|
|
wrapTimer,
|
|
};
|