1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-28 19:06:12 +01:00

chore: wrapTimer function types (#8428)

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>
This commit is contained in:
Gastón Fournier 2024-10-15 11:18:22 +02:00 committed by GitHub
parent e4cfb29adc
commit e22f6a04ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 11 deletions

View File

@ -22,7 +22,7 @@ export default class ClientFeatureToggleReadModel
constructor(db: Db, eventBus: EventEmitter) {
this.db = db;
this.timer = (action) =>
this.timer = (action: string) =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'client-feature-toggle-read-model',
action,

View File

@ -49,7 +49,7 @@ export default class FrontendAPIController extends Controller {
this.logger = config.getLogger('frontend-api-controller.ts');
this.services = services;
this.timer = (functionName) =>
this.timer = (functionName: string) =>
metricsHelper.wrapTimer(config.eventBus, FUNCTION_TIME, {
className: 'FrontendAPIController',
functionName,

View File

@ -1,20 +1,31 @@
/* eslint-disable no-param-reassign */
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 be used as a .then(<func>) argument.
// It transparently passes the data to the following .then(<func>)
// the returned function is designed to stop the timer and emit the event
//
// usage: promise.then(wrapTimer(bus, type, { payload: 'ok' }))
const wrapTimer: (EventEmitter, string, object) => (any) => any = (
eventBus,
event,
args = {},
// 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) => {
return (data: unknown) => {
args.time = t();
eventBus.emit(event, args);
return data;