Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dom-ready/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ domReady( function() {

_Parameters_

- _callback_ `Function`: A function to execute after the DOM is ready.
- _callback_ `Callback`: A function to execute after the DOM is ready.

_Returns_

Expand Down
24 changes: 18 additions & 6 deletions packages/dom-ready/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
/**
* @typedef {() => void} Callback
*
* TODO: Remove this typedef and inline `() => void` type.
*
* This typedef is used so that a descriptive type is provided in our
* automatically generated documentation.
*
* An in-line type `() => void` would be preferable, but the generated
* documentation is `null` in that case.
*
* @see https://github.com/WordPress/gutenberg/issues/18045
*/

/**
* Specify a function to execute when the DOM is fully loaded.
*
* @param {Function} callback A function to execute after the DOM is ready.
* @param {Callback} callback A function to execute after the DOM is ready.
*
* @example
* ```js
Expand All @@ -14,16 +28,14 @@
*
* @return {void}
*/
const domReady = function( callback ) {
export default function domReady( callback ) {
if (
document.readyState === 'complete' || // DOMContentLoaded + Images/Styles/etc loaded, so we call directly.
document.readyState === 'interactive' // DOMContentLoaded fires at this point, so we call directly.
) {
return callback();
return void callback();
Copy link
Member Author

Choose a reason for hiding this comment

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

Subtle behavioral change - this function did not always return void as its type suggested. In this branch, tt would return whatever the callback returned.

Adding the void here so the function always returns undefined.

Copy link
Member

Choose a reason for hiding this comment

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

Subtle behavioral change - this function did not always return void as its type suggested. In this branch, tt would return whatever the callback returned.

Adding the void here so the function always returns undefined.

I think this syntax can be a little unexpected for some people, or at least it was for me the first times I had seen it, but I think it's a nice simple way to guarantee the return type as undefined, as you suggest.

}

// DOMContentLoaded has not fired yet, delay callback until then.
document.addEventListener( 'DOMContentLoaded', callback );
};

export default domReady;
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
"noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */

},
"include": [
"./packages/dom-ready/**/*.js",
"./packages/i18n/**/*.js",
"./packages/is-shallow-equal/**/*.js",
"./packages/priority-queue/**/*.js",
Expand Down