Skip to content

Commit

Permalink
Metric type fixes (#356)
Browse files Browse the repository at this point in the history
* Another type fix

* Easy way to set metric specific ReportCallback
  • Loading branch information
tunetheweb authored Jun 22, 2023
1 parent 1d2c191 commit 5973d51
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 66 deletions.
5 changes: 3 additions & 2 deletions src/onCLS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
CLSMetric,
CLSReportCallback,
MetricRatingThresholds,
ReportCallback,
ReportOpts,
} from './types.js';

Expand Down Expand Up @@ -105,7 +106,7 @@ export const onCLS = (onReport: CLSReportCallback, opts?: ReportOpts) => {
const po = observe('layout-shift', handleEntries);
if (po) {
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
CLSThresholds,
opts!.reportAllChanges
Expand All @@ -122,7 +123,7 @@ export const onCLS = (onReport: CLSReportCallback, opts?: ReportOpts) => {
sessionValue = 0;
metric = initMetric('CLS', 0);
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
CLSThresholds,
opts!.reportAllChanges
Expand Down
5 changes: 3 additions & 2 deletions src/onFCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
FCPMetric,
FCPReportCallback,
MetricRatingThresholds,
ReportCallback,
ReportOpts,
} from './types.js';

Expand Down Expand Up @@ -70,7 +71,7 @@ export const onFCP = (onReport: FCPReportCallback, opts?: ReportOpts) => {

if (po) {
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
FCPThresholds,
opts!.reportAllChanges
Expand All @@ -81,7 +82,7 @@ export const onFCP = (onReport: FCPReportCallback, opts?: ReportOpts) => {
onBFCacheRestore((event) => {
metric = initMetric('FCP');
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
FCPThresholds,
opts!.reportAllChanges
Expand Down
9 changes: 5 additions & 4 deletions src/onFID.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {runOnce} from './lib/runOnce.js';
import {whenActivated} from './lib/whenActivated.js';
import {
FIDMetric,
FIDReportCallback,
FirstInputPolyfillCallback,
MetricRatingThresholds,
ReportCallback,
Expand All @@ -46,7 +47,7 @@ export const FIDThresholds: MetricRatingThresholds = [100, 300];
* _**Important:** since FID is only reported after the user interacts with the
* page, it's possible that it will not be reported for some page loads._
*/
export const onFID = (onReport: ReportCallback, opts?: ReportOpts) => {
export const onFID = (onReport: FIDReportCallback, opts?: ReportOpts) => {
// Set defaults
opts = opts || {};

Expand All @@ -70,7 +71,7 @@ export const onFID = (onReport: ReportCallback, opts?: ReportOpts) => {

const po = observe('first-input', handleEntries);
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
FIDThresholds,
opts!.reportAllChanges
Expand Down Expand Up @@ -99,7 +100,7 @@ export const onFID = (onReport: ReportCallback, opts?: ReportOpts) => {
onBFCacheRestore(() => {
metric = initMetric('FID');
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
FIDThresholds,
opts!.reportAllChanges
Expand All @@ -116,7 +117,7 @@ export const onFID = (onReport: ReportCallback, opts?: ReportOpts) => {
onBFCacheRestore(() => {
metric = initMetric('FID');
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
FIDThresholds,
opts!.reportAllChanges
Expand Down
7 changes: 4 additions & 3 deletions src/onINP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
import {whenActivated} from './lib/whenActivated.js';
import {
INPMetric,
INPReportCallback,
MetricRatingThresholds,
ReportCallback,
ReportOpts,
Expand Down Expand Up @@ -149,7 +150,7 @@ const estimateP98LongestInteraction = () => {
* hidden. As a result, the `callback` function might be called multiple times
* during the same page load._
*/
export const onINP = (onReport: ReportCallback, opts?: ReportOpts) => {
export const onINP = (onReport: INPReportCallback, opts?: ReportOpts) => {
// Set defaults
opts = opts || {};

Expand Down Expand Up @@ -211,7 +212,7 @@ export const onINP = (onReport: ReportCallback, opts?: ReportOpts) => {
} as PerformanceObserverInit);

report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
INPThresholds,
opts!.reportAllChanges
Expand Down Expand Up @@ -245,7 +246,7 @@ export const onINP = (onReport: ReportCallback, opts?: ReportOpts) => {

metric = initMetric('INP');
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
INPThresholds,
opts!.reportAllChanges
Expand Down
7 changes: 4 additions & 3 deletions src/onLCP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {whenActivated} from './lib/whenActivated.js';
import {
LCPMetric,
MetricRatingThresholds,
LCPReportCallback,
ReportCallback,
ReportOpts,
} from './types.js';
Expand All @@ -47,7 +48,7 @@ const reportedMetricIDs: Record<string, boolean> = {};
* performance entry is dispatched, or once the final value of the metric has
* been determined.
*/
export const onLCP = (onReport: ReportCallback, opts?: ReportOpts) => {
export const onLCP = (onReport: LCPReportCallback, opts?: ReportOpts) => {
// Set defaults
opts = opts || {};

Expand Down Expand Up @@ -81,7 +82,7 @@ export const onLCP = (onReport: ReportCallback, opts?: ReportOpts) => {

if (po) {
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
LCPThresholds,
opts!.reportAllChanges
Expand Down Expand Up @@ -110,7 +111,7 @@ export const onLCP = (onReport: ReportCallback, opts?: ReportOpts) => {
onBFCacheRestore((event) => {
metric = initMetric('LCP');
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
LCPThresholds,
opts!.reportAllChanges
Expand Down
13 changes: 9 additions & 4 deletions src/onTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ import {bindReporter} from './lib/bindReporter.js';
import {initMetric} from './lib/initMetric.js';
import {onBFCacheRestore} from './lib/bfcache.js';
import {getNavigationEntry} from './lib/getNavigationEntry.js';
import {MetricRatingThresholds, ReportCallback, ReportOpts} from './types.js';
import {
MetricRatingThresholds,
ReportCallback,
ReportOpts,
TTFBReportCallback,
} from './types.js';
import {getActivationStart} from './lib/getActivationStart.js';
import {whenActivated} from './lib/whenActivated.js';

Expand Down Expand Up @@ -55,13 +60,13 @@ const whenReady = (callback: () => void) => {
* includes time spent on DNS lookup, connection negotiation, network latency,
* and server processing time.
*/
export const onTTFB = (onReport: ReportCallback, opts?: ReportOpts) => {
export const onTTFB = (onReport: TTFBReportCallback, opts?: ReportOpts) => {
// Set defaults
opts = opts || {};

let metric = initMetric('TTFB');
let report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
TTFBThresholds,
opts.reportAllChanges
Expand Down Expand Up @@ -95,7 +100,7 @@ export const onTTFB = (onReport: ReportCallback, opts?: ReportOpts) => {
onBFCacheRestore(() => {
metric = initMetric('TTFB', 0);
report = bindReporter(
onReport,
onReport as ReportCallback,
metric,
TTFBThresholds,
opts!.reportAllChanges
Expand Down
4 changes: 0 additions & 4 deletions src/types/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ export interface ReportCallback {
(metric: Metric): void;
}

export interface ReportCallbackWithAttribution {
(metric: MetricWithAttribution): void;
}

export interface ReportOpts {
reportAllChanges?: boolean;
durationThreshold?: number;
Expand Down
12 changes: 3 additions & 9 deletions src/types/cls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {
LoadState,
Metric,
ReportCallback,
ReportCallbackWithAttribution,
} from './base.js';
import {LoadState, Metric} from './base.js';

/**
* A CLS-specific version of the Metric object.
Expand Down Expand Up @@ -81,14 +76,13 @@ export interface CLSMetricWithAttribution extends CLSMetric {
/**
* A CLS-specific version of the ReportCallback function.
*/
export interface CLSReportCallback extends ReportCallback {
export interface CLSReportCallback {
(metric: CLSMetric): void;
}

/**
* A CLS-specific version of the ReportCallback function with attribution.
*/
export interface CLSReportCallbackWithAttribution
extends ReportCallbackWithAttribution {
export interface CLSReportCallbackWithAttribution {
(metric: CLSMetricWithAttribution): void;
}
12 changes: 3 additions & 9 deletions src/types/fcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {
LoadState,
Metric,
ReportCallback,
ReportCallbackWithAttribution,
} from './base.js';
import {LoadState, Metric} from './base.js';
import {NavigationTimingPolyfillEntry} from './polyfills.js';

/**
Expand Down Expand Up @@ -73,14 +68,13 @@ export interface FCPMetricWithAttribution extends FCPMetric {
/**
* An FCP-specific version of the ReportCallback function.
*/
export interface FCPReportCallback extends ReportCallback {
export interface FCPReportCallback {
(metric: FCPMetric): void;
}

/**
* An FCP-specific version of the ReportCallback function with attribution.
*/
export interface FCPReportCallbackWithAttribution
extends ReportCallbackWithAttribution {
export interface FCPReportCallbackWithAttribution {
(metric: FCPMetricWithAttribution): void;
}
12 changes: 3 additions & 9 deletions src/types/fid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {
LoadState,
Metric,
ReportCallback,
ReportCallbackWithAttribution,
} from './base.js';
import {LoadState, Metric} from './base.js';
import {FirstInputPolyfillEntry} from './polyfills.js';

/**
Expand Down Expand Up @@ -74,14 +69,13 @@ export interface FIDMetricWithAttribution extends FIDMetric {
/**
* An FID-specific version of the ReportCallback function.
*/
export interface FIDReportCallback extends ReportCallback {
export interface FIDReportCallback {
(metric: FIDMetric): void;
}

/**
* An FID-specific version of the ReportCallback function with attribution.
*/
export interface FIDReportCallbackWithAttribution
extends ReportCallbackWithAttribution {
export interface FIDReportCallbackWithAttribution {
(metric: FIDMetricWithAttribution): void;
}
12 changes: 3 additions & 9 deletions src/types/inp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,7 @@
* limitations under the License.
*/

import {
LoadState,
Metric,
ReportCallback,
ReportCallbackWithAttribution,
} from './base.js';
import {LoadState, Metric} from './base.js';

/**
* An INP-specific version of the Metric object.
Expand Down Expand Up @@ -73,14 +68,13 @@ export interface INPMetricWithAttribution extends INPMetric {
/**
* An INP-specific version of the ReportCallback function.
*/
export interface INPReportCallback extends ReportCallback {
export interface INPReportCallback {
(metric: INPMetric): void;
}

/**
* An INP-specific version of the ReportCallback function with attribution.
*/
export interface INPReportCallbackWithAttribution
extends ReportCallbackWithAttribution {
export interface INPReportCallbackWithAttribution {
(metric: INPMetricWithAttribution): void;
}
7 changes: 3 additions & 4 deletions src/types/lcp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Metric, ReportCallback, ReportCallbackWithAttribution} from './base.js';
import {Metric} from './base.js';
import {NavigationTimingPolyfillEntry} from './polyfills.js';

/**
Expand Down Expand Up @@ -91,14 +91,13 @@ export interface LCPMetricWithAttribution extends LCPMetric {
/**
* An LCP-specific version of the ReportCallback function.
*/
export interface LCPReportCallback extends ReportCallback {
export interface LCPReportCallback {
(metric: LCPMetric): void;
}

/**
* An LCP-specific version of the ReportCallback function with attribution.
*/
export interface LCPReportCallbackWithAttribution
extends ReportCallbackWithAttribution {
export interface LCPReportCallbackWithAttribution {
(metric: LCPMetricWithAttribution): void;
}
7 changes: 3 additions & 4 deletions src/types/ttfb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import {Metric, ReportCallback, ReportCallbackWithAttribution} from './base.js';
import {Metric} from './base.js';
import {NavigationTimingPolyfillEntry} from './polyfills.js';

/**
Expand Down Expand Up @@ -69,14 +69,13 @@ export interface TTFBMetricWithAttribution extends TTFBMetric {
/**
* A TTFB-specific version of the ReportCallback function.
*/
export interface TTFBReportCallback extends ReportCallback {
export interface TTFBReportCallback {
(metric: TTFBMetric): void;
}

/**
* A TTFB-specific version of the ReportCallback function with attribution.
*/
export interface TTFBReportCallbackWithAttribution
extends ReportCallbackWithAttribution {
export interface TTFBReportCallbackWithAttribution {
(metric: TTFBMetricWithAttribution): void;
}

0 comments on commit 5973d51

Please sign in to comment.