-
-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: wrapTimer function types #8428
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.OpenSSF Scorecard
Scanned Manifest Files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this could be a simpler approach, where we type our parameters and rely on the implicit type instead of a declared one:
const wrapTimer = (
eventBus: EventEmitter,
eventType: string,
args: Record<string, unknown> = {},
) => {
const t = timer.new();
return (data: unknown) => {
eventBus.emit(eventType, { ...args, time: t() });
return data;
};
};
If you prefer to have a type declaration, I would do something like this:
type WrapTimerFunction = (
eventBus: EventEmitter,
eventType: string,
args?: Record<string, unknown>,
) => (data: unknown) => unknown;
const wrapTimer: WrapTimerFunction = (eventBus, eventType, args = {}) => {
const t = timer.new();
return (data) => {
eventBus.emit(eventType, { ...args, time: t() });
return data;
};
};
Or maybe type it like this, if you think it's simpler to read:
type WrapTimerFunctionCallback = (data: unknown) => unknown;
type WrapTimerFunction = (
eventBus: EventEmitter,
eventType: string,
args?: Record<string, unknown>,
) => WrapTimerFunctionCallback;
src/lib/util/metrics-helper.ts
Outdated
@@ -8,11 +8,14 @@ import timer from './timer'; | |||
// It transparently passes the data to the following .then(<func>) | |||
// | |||
// usage: promise.then(wrapTimer(bus, type, { payload: 'ok' })) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we're touching this file, I also think these comments are outdated.
Co-authored-by: Nuno Góis <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice!
This gives us better types for our wrapTimer function.
Maybe the type
(args: any) => any
could also be improved