1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +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) { constructor(db: Db, eventBus: EventEmitter) {
this.db = db; this.db = db;
this.timer = (action) => this.timer = (action: string) =>
metricsHelper.wrapTimer(eventBus, DB_TIME, { metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'client-feature-toggle-read-model', store: 'client-feature-toggle-read-model',
action, action,

View File

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

View File

@ -1,20 +1,31 @@
/* eslint-disable no-param-reassign */ import type EventEmitter from 'events';
import timer from './timer'; import timer from './timer';
// wrapTimer keeps track of the timing of a async operation and emits // wrapTimer keeps track of the timing of a async operation and emits
// a event on the given eventBus once the operation is complete // a event on the given eventBus once the operation is complete
// //
// the returned function is designed to be used as a .then(<func>) argument. // the returned function is designed to stop the timer and emit the event
// It transparently passes the data to the following .then(<func>)
// //
// usage: promise.then(wrapTimer(bus, type, { payload: 'ok' })) // Usage:
const wrapTimer: (EventEmitter, string, object) => (any) => any = ( // Define the timer function. It can be done once per class.
eventBus, // this.timer = (action: string) =>
event, // metricsHelper.wrapTimer(eventBus, DB_TIME, {
args = {}, // 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(); const t = timer.new();
return (data) => { return (data: unknown) => {
args.time = t(); args.time = t();
eventBus.emit(event, args); eventBus.emit(event, args);
return data; return data;