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

Make bindReporter generic over metric type #359

Merged
merged 2 commits into from Jul 7, 2023

Conversation

brendankenny
Copy link
Member

Can land after #356

Parameterizes the bindReporter type on the metric passed in so that the callback can now be typed specifically for that metric (e.g. a result for CLSMetric must be using a CLSReportCallback).

This required initMetric to be parameterized similarly, so the returned metric type is now based on the metric name passed in.

let metric = initMetric('FCP');
//  ^ FCPMetric

The main change required was using a union of the metric types wherever the base Metric interface is currently used. This leaves most uses of it unchanged, but allows narrowing to which specific metric is being used based on the name property.

I renamed the existing Metric to be MetricBase and named the union Metric to keep the uses of it unchanged, but if someone has better naming suggestions, changing is no problem :)

@@ -27,8 +26,8 @@ let interactionCountEstimate = 0;
let minKnownInteractionId = Infinity;
let maxKnownInteractionId = 0;

const updateEstimate = (entries: Metric['entries']) => {
(entries as PerformanceEventTiming[]).forEach((e) => {
const updateEstimate = (entries: PerformanceEventTiming[]) => {
Copy link
Member Author

Choose a reason for hiding this comment

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

observe() checks the callback type, might as well trust it :)

@@ -66,9 +65,8 @@ export const onCLS = (onReport: CLSReportCallback, opts?: ReportOpts) => {
let report: ReturnType<typeof bindReporter>;

let sessionValue = 0;
let sessionEntries: PerformanceEntry[] = [];
let sessionEntries: LayoutShift[] = [];
Copy link
Member Author

Choose a reason for hiding this comment

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

minor type bug that's an explicit error now with these changes (on sessionEntries.push(entry) below), but no functional change

import type {FIDMetric} from './fid.js';
import type {INPMetric} from './inp.js';
import type {LCPMetric} from './lcp.js';
import type {TTFBMetric} from './ttfb.js';
Copy link
Member Author

Choose a reason for hiding this comment

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

import type vs import here doesn't change anything in the output since these files (currently) have only types in them, but generally good to be explicit that the circular dependency is purely in type-space and so won't ever be an issue

src/types/base.ts Outdated Show resolved Hide resolved
Copy link
Member

@tunetheweb tunetheweb left a comment

Choose a reason for hiding this comment

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

LGTM, but with one question.

src/lib/initMetric.ts Show resolved Hide resolved
src/lib/initMetric.ts Outdated Show resolved Hide resolved
src/types/base.ts Outdated Show resolved Hide resolved
src/types/base.ts Outdated Show resolved Hide resolved
@brendankenny brendankenny changed the base branch from another-type-fix to main June 27, 2023 17:31
@brendankenny brendankenny changed the base branch from main to another-type-fix June 27, 2023 17:45
@brendankenny brendankenny changed the base branch from another-type-fix to main June 27, 2023 17:52
@tunetheweb tunetheweb merged commit e35816e into GoogleChrome:main Jul 7, 2023
2 checks passed
@tunetheweb tunetheweb changed the title make bindReporter generic over metric type Make bindReporter generic over metric type Jul 10, 2023
@brendankenny brendankenny deleted the generic-bind-reporter branch March 11, 2024 16:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants