Skip to content

Commit

Permalink
switch type names back, restore MetricWithAttribution
Browse files Browse the repository at this point in the history
  • Loading branch information
brendankenny committed Jun 27, 2023
1 parent d818bcd commit 591e059
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 25 deletions.
10 changes: 5 additions & 5 deletions src/lib/bindReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import {Metric, MetricRatingThresholds} from '../types.js';
import {MetricType, MetricRatingThresholds} from '../types.js';

const getRating = (
value: number,
thresholds: MetricRatingThresholds
): Metric['rating'] => {
): MetricType['rating'] => {
if (value > thresholds[1]) {
return 'poor';
}
Expand All @@ -29,9 +29,9 @@ const getRating = (
return 'good';
};

export const bindReporter = <MetricName extends Metric['name']>(
callback: (metric: Extract<Metric, {name: MetricName}>) => void,
metric: Extract<Metric, {name: MetricName}>,
export const bindReporter = <MetricName extends MetricType['name']>(
callback: (metric: Extract<MetricType, {name: MetricName}>) => void,
metric: Extract<MetricType, {name: MetricName}>,
thresholds: MetricRatingThresholds,
reportAllChanges?: boolean
) => {
Expand Down
10 changes: 5 additions & 5 deletions src/lib/initMetric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ import {getBFCacheRestoreTime} from './bfcache.js';
import {generateUniqueID} from './generateUniqueID.js';
import {getActivationStart} from './getActivationStart.js';
import {getNavigationEntry} from './getNavigationEntry.js';
import {Metric} from '../types.js';
import {MetricType} from '../types.js';

export const initMetric = <MetricName extends Metric['name']>(
export const initMetric = <MetricName extends MetricType['name']>(
name: MetricName,
value?: number
) => {
const navEntry = getNavigationEntry();
let navigationType: Metric['navigationType'] = 'navigate';
let navigationType: MetricType['navigationType'] = 'navigate';

if (getBFCacheRestoreTime() >= 0) {
navigationType = 'back-forward-cache';
Expand All @@ -38,12 +38,12 @@ export const initMetric = <MetricName extends Metric['name']>(
navigationType = navEntry.type.replace(
/_/g,
'-'
) as Metric['navigationType'];
) as MetricType['navigationType'];
}
}

// Use `entries` type specific for the metric.
const entries: Extract<Metric, {name: MetricName}>['entries'] = [];
const entries: Extract<MetricType, {name: MetricName}>['entries'] = [];

return {
name,
Expand Down
18 changes: 15 additions & 3 deletions src/types/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import type {INPMetric} from './inp.js';
import type {LCPMetric} from './lcp.js';
import type {TTFBMetric} from './ttfb.js';

export interface MetricBase {
export interface Metric {
/**
* The name of the metric (in acronym form).
*/
Expand Down Expand Up @@ -92,14 +92,26 @@ export interface MetricBase {
}

/** The union of supported metric types. */
export type Metric =
export type MetricType =
| CLSMetric
| FCPMetric
| FIDMetric
| INPMetric
| LCPMetric
| TTFBMetric;

/**
* A version of the `Metric` that is used with the attribution build.
*/
export interface MetricWithAttribution extends Metric {
/**
* An object containing potentially-helpful debugging information that
* can be sent along with the metric value for the current page visit in
* order to help identify issues happening to real-users in the field.
*/
attribution: {[key: string]: unknown};
}

/**
* The thresholds of metric's "good", "needs improvement", and "poor" ratings.
*
Expand All @@ -116,7 +128,7 @@ export type Metric =
export type MetricRatingThresholds = [number, number];

export interface ReportCallback {
(metric: Metric): void;
(metric: MetricType): void;
}

export interface ReportOpts {
Expand Down
4 changes: 2 additions & 2 deletions src/types/cls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import type {LoadState, MetricBase} from './base.js';
import type {LoadState, Metric} from './base.js';

/**
* A CLS-specific version of the Metric object.
*/
export interface CLSMetric extends MetricBase {
export interface CLSMetric extends Metric {
name: 'CLS';
entries: LayoutShift[];
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/fcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import type {LoadState, MetricBase} from './base.js';
import type {LoadState, Metric} from './base.js';
import {NavigationTimingPolyfillEntry} from './polyfills.js';

/**
* An FCP-specific version of the Metric object.
*/
export interface FCPMetric extends MetricBase {
export interface FCPMetric extends Metric {
name: 'FCP';
entries: PerformancePaintTiming[];
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/fid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import type {LoadState, MetricBase} from './base.js';
import type {LoadState, Metric} from './base.js';
import {FirstInputPolyfillEntry} from './polyfills.js';

/**
* An FID-specific version of the Metric object.
*/
export interface FIDMetric extends MetricBase {
export interface FIDMetric extends Metric {
name: 'FID';
entries: (PerformanceEventTiming | FirstInputPolyfillEntry)[];
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/inp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

import type {LoadState, MetricBase} from './base.js';
import type {LoadState, Metric} from './base.js';

/**
* An INP-specific version of the Metric object.
*/
export interface INPMetric extends MetricBase {
export interface INPMetric extends Metric {
name: 'INP';
entries: PerformanceEventTiming[];
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/lcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import type {MetricBase} from './base.js';
import type {Metric} from './base.js';
import {NavigationTimingPolyfillEntry} from './polyfills.js';

/**
* An LCP-specific version of the Metric object.
*/
export interface LCPMetric extends MetricBase {
export interface LCPMetric extends Metric {
name: 'LCP';
entries: LargestContentfulPaint[];
}
Expand Down
4 changes: 2 additions & 2 deletions src/types/ttfb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
* limitations under the License.
*/

import type {MetricBase} from './base.js';
import type {Metric} from './base.js';
import {NavigationTimingPolyfillEntry} from './polyfills.js';

/**
* A TTFB-specific version of the Metric object.
*/
export interface TTFBMetric extends MetricBase {
export interface TTFBMetric extends Metric {
name: 'TTFB';
entries: PerformanceNavigationTiming[] | NavigationTimingPolyfillEntry[];
}
Expand Down

0 comments on commit 591e059

Please sign in to comment.