UNPKG

no-sleep-app

Version:

NoSleepApp is a lightweight JavaScript library that prevents devices from going to sleep during critical activities. It uses the Screen Wake Lock API or a fallback video playback method to keep the screen active, ensuring uninterrupted user experiences ac

115 lines (89 loc) 3.88 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: visibility-listener.js</title> <script src="scripts/prettify/prettify.js"> </script> <script src="scripts/prettify/lang-css.js"> </script> <!--[if lt IE 9]> <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css"> <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css"> </head> <body> <div id="main"> <h1 class="page-title">Source: visibility-listener.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** * @fileoverview VisibilityListener is a utility class that listens for changes in * document visibility and fullscreen state, invoking a callback when these states change. */ /** * Class to listen for visibility and fullscreen changes in the document. * It triggers a callback when the document becomes visible or enters fullscreen mode. */ export default class VisibilityListener { /** * @param {Function} callback The callback function to invoke on visibility or fullscreen changes. */ constructor(callback) { /** * @type {Function} The callback function to execute when the state changes. */ this.callback = callback; // Bind methods to the instance. this.handleVisibilityChange = this.handleVisibilityChange.bind(this); this.handleFullscreenChange = this.handleFullscreenChange.bind(this); this._addListeners(); } /** * Adds event listeners for visibility and fullscreen changes. * @private */ _addListeners() { document.addEventListener("visibilitychange", this.handleVisibilityChange); document.addEventListener("fullscreenchange", this.handleFullscreenChange); } /** * Handles the `visibilitychange` event. * Invokes the callback if the document is visible. */ handleVisibilityChange() { if (document.visibilityState === "visible") { this.callback(); } } /** * Handles the `fullscreenchange` event. * Invokes the callback if the document enters fullscreen mode. */ handleFullscreenChange() { if (document.fullscreenElement) { this.callback(); } } /** * Removes the event listeners for visibility and fullscreen changes. */ removeListeners() { document.removeEventListener("visibilitychange", this.handleVisibilityChange); document.removeEventListener("fullscreenchange", this.handleFullscreenChange); } } </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="module.exports.html">exports</a></li></ul><h3>Global</h3><ul><li><a href="global.html#disable">disable</a></li><li><a href="global.html#enable">enable</a></li><li><a href="global.html#handleFullscreenChange">handleFullscreenChange</a></li><li><a href="global.html#handleVisibilityChange">handleVisibilityChange</a></li><li><a href="global.html#isEnabled">isEnabled</a></li><li><a href="global.html#isNativeWakeLockSupported">isNativeWakeLockSupported</a></li><li><a href="global.html#isOldIOS">isOldIOS</a></li><li><a href="global.html#pause">pause</a></li><li><a href="global.html#play">play</a></li><li><a href="global.html#removeListeners">removeListeners</a></li><li><a href="global.html#setMetadataListener">setMetadataListener</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 4.0.4</a> on Fri Nov 22 2024 23:19:12 GMT+0000 (Greenwich Mean Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>