Select Git revision

Volker Schukai authored
ready.mjs 2.15 KiB
/**
* Copyright schukai GmbH and contributors 2023. All Rights Reserved.
* Node module: @schukai/monster
* This file is licensed under the AGPLv3 License.
* License text available at https://www.gnu.org/licenses/agpl-3.0.en.html
*/
import { getDocument, getWindow } from "./util.mjs";
export { domReady, windowReady };
/**
* This variable is a promise that is fulfilled as soon as the dom is available.
*
* The DOMContentLoaded event is fired when the original HTML document is fully loaded and parsed
* without waiting for stylesheets, images, and subframes to finish loading.
*
* document.readyState changes to interactive
* window's DOMContentLoaded event gets fired
* document.readyState changes to complete
* window's load event gets fired load
*
* @license AGPLv3
* @since 1.31.0
* @memberOf Monster.DOM
* @summary variable to check if dom is ready
* @type {Promise}
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/DOMContentLoaded_event
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
*/
const domReady = new Promise((resolve) => {
const document = getDocument();
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", resolve);
} else {
resolve();
}
});
/**
* This variable is a promise that is fulfilled as soon as the windows is available.
*
* The load event fires when the entire page is loaded, including all dependent resources such as stylesheets,
* assets, and images. Unlike DOMContentLoaded, which fires as soon as the DOM of the page is loaded,
* without waiting for the resources to finish loading.
*
* @license AGPLv3
* @since 1.31.0
* @memberOf Monster.DOM
* @summary variable to check if window is ready
* @type {Promise}
* @see https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
*/
const windowReady = new Promise((resolve) => {
const document = getDocument();
const window = getWindow();
if (document.readyState === "complete") {
resolve();
} else {
window.addEventListener("load", resolve);
}
});