Skip to content

Commit 87b2512

Browse files
committed
Improve typing
1 parent 8831b1f commit 87b2512

File tree

2 files changed

+44
-22
lines changed

2 files changed

+44
-22
lines changed

plugins/optimization-detective/detect-loader.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
* JavaScript file must contain a single top-level function which is not exported. The file is inlined as part of
66
* another module which wraps the function in an IIFE.
77
*
8-
* @param {string} detectSrc
9-
* @param {Object} detectArgs
8+
* @since 1.0.0
9+
*
10+
* @param {string} detectSrc - The URL to detect.js.
11+
* @param {import("./detect.js").DetectFunctionArgs} detectArgs - The arguments exported from PHP.
1012
*/
1113
// eslint-disable-next-line no-unused-vars
1214
async function load( detectSrc, detectArgs ) {
@@ -26,6 +28,8 @@ async function load( detectSrc, detectArgs ) {
2628
} );
2729
}
2830

29-
const { default: detect } = await import( detectSrc );
31+
const detect = /** @type {import("./detect.js").DetectFunction} */ (
32+
( await import( detectSrc ) ).default
33+
);
3034
await detect( detectArgs );
3135
}

plugins/optimization-detective/detect.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -508,28 +508,46 @@ function debounceCompressUrlMetric() {
508508
* @typedef {{groups: Array<{url_metrics: Array<UrlMetricDebugData>}>}} CollectionDebugData
509509
*/
510510

511+
/**
512+
* Args for the detect function.
513+
*
514+
* @since 1.0.0
515+
*
516+
* @typedef {Object} DetectFunctionArgs
517+
* @property {string[]} extensionModuleUrls - URLs for extension script modules to import.
518+
* @property {number} minViewportAspectRatio - Minimum aspect ratio allowed for the viewport.
519+
* @property {number} maxViewportAspectRatio - Maximum aspect ratio allowed for the viewport.
520+
* @property {boolean} isDebug - Whether to show debug messages.
521+
* @property {string} restApiEndpoint - URL for where to send the detection data.
522+
* @property {string} [restApiNonce] - Nonce for the REST API when the user is logged-in.
523+
* @property {boolean} gzdecodeAvailable - Whether application/gzip can be sent to the REST API.
524+
* @property {number} maxUrlMetricSize - Maximum size of the URL Metric to send.
525+
* @property {string} currentETag - Current ETag.
526+
* @property {string} currentUrl - Current URL.
527+
* @property {string} urlMetricSlug - Slug for URL Metric.
528+
* @property {number|null} cachePurgePostId - Cache purge post ID.
529+
* @property {string} urlMetricHMAC - HMAC for URL Metric storage.
530+
* @property {URLMetricGroupStatus[]} urlMetricGroupStatuses - URL Metric group statuses.
531+
* @property {number} storageLockTTL - The TTL (in seconds) for the URL Metric storage lock.
532+
* @property {number} freshnessTTL - The freshness age (TTL) for a given URL Metric.
533+
* @property {string} webVitalsLibrarySrc - The URL for the web-vitals library.
534+
* @property {CollectionDebugData} [urlMetricGroupCollection] - URL Metric group collection, when in debug mode.
535+
*/
536+
537+
/**
538+
* The detect function.
539+
*
540+
* @since 1.0.0
541+
* @callback DetectFunction
542+
* @param {DetectFunctionArgs} args - The arguments for the function.
543+
* @return {Promise<void>}
544+
*/
545+
511546
/**
512547
* Detects the LCP element, loaded images, client viewport, and store for future optimizations.
513548
*
514-
* @param {Object} args - Args.
515-
* @param {string[]} args.extensionModuleUrls - URLs for extension script modules to import.
516-
* @param {number} args.minViewportAspectRatio - Minimum aspect ratio allowed for the viewport.
517-
* @param {number} args.maxViewportAspectRatio - Maximum aspect ratio allowed for the viewport.
518-
* @param {boolean} args.isDebug - Whether to show debug messages.
519-
* @param {string} args.restApiEndpoint - URL for where to send the detection data.
520-
* @param {string} [args.restApiNonce] - Nonce for the REST API when the user is logged-in.
521-
* @param {boolean} args.gzdecodeAvailable - Whether application/gzip can be sent to the REST API.
522-
* @param {number} args.maxUrlMetricSize - Maximum size of the URL Metric to send.
523-
* @param {string} args.currentETag - Current ETag.
524-
* @param {string} args.currentUrl - Current URL.
525-
* @param {string} args.urlMetricSlug - Slug for URL Metric.
526-
* @param {number|null} args.cachePurgePostId - Cache purge post ID.
527-
* @param {string} args.urlMetricHMAC - HMAC for URL Metric storage.
528-
* @param {URLMetricGroupStatus[]} args.urlMetricGroupStatuses - URL Metric group statuses.
529-
* @param {number} args.storageLockTTL - The TTL (in seconds) for the URL Metric storage lock.
530-
* @param {number} args.freshnessTTL - The freshness age (TTL) for a given URL Metric.
531-
* @param {string} args.webVitalsLibrarySrc - The URL for the web-vitals library.
532-
* @param {CollectionDebugData} [args.urlMetricGroupCollection] - URL Metric group collection, when in debug mode.
549+
* @type {DetectFunction}
550+
* @param {DetectFunctionArgs} args - Args.
533551
*/
534552
export default async function detect( {
535553
minViewportAspectRatio,

0 commit comments

Comments
 (0)