Skip to content
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

Merged
merged 3 commits into from
Oct 15, 2024
Merged

chore: wrapTimer function types #8428

merged 3 commits into from
Oct 15, 2024

Conversation

gastonfournier
Copy link
Contributor

This gives us better types for our wrapTimer function.

Maybe the type (args: any) => any could also be improved

Copy link

vercel bot commented Oct 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-monorepo-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 14, 2024 1:28pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
unleash-docs ⬜️ Ignored (Inspect) Visit Preview Oct 14, 2024 1:28pm

Copy link
Contributor

github-actions bot commented Oct 11, 2024

Dependency Review

✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.

OpenSSF Scorecard

PackageVersionScoreDetails

Scanned Manifest Files

Copy link
Member

@nunogois nunogois left a 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;

@@ -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' }))
Copy link
Member

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]>
Copy link
Member

@nunogois nunogois left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@gastonfournier gastonfournier merged commit e22f6a0 into main Oct 15, 2024
11 checks passed
@gastonfournier gastonfournier deleted the wrap-timer-types branch October 15, 2024 09:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants