UNPKG

react-screen-wake-lock

Version:

React implementation of the Screen Wake Lock API. It provides a way to prevent devices from dimming or locking the screen when an application needs to keep running.

3 lines (2 loc) 2.34 kB
function e(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach(function(n){if("default"!==n){var t=Object.getOwnPropertyDescriptor(e,n);Object.defineProperty(r,n,t.get?t:{enumerable:!0,get:function(){return e[n]}})}}),r.default=e,r}var r=/*#__PURE__*/e(require("react"));function n(e,r){try{var n=e()}catch(e){return r(e)}return n&&n.then?n.then(void 0,r):n}var t=function(e){return console.warn("[react-screen-wake-lock]: "+e)};exports.useWakeLock=function(e){var u=void 0===e?{}:e,i=u.onError,o=u.onRequest,c=u.onRelease,s=u.reacquireOnPageVisible,a=void 0!==s&&s,l=r.useState(),f=l[0],v=l[1],d=r.useRef(null),h="undefined"!=typeof window&&"wakeLock"in navigator,m=r.useCallback(function(e){void 0===e&&(e="screen");try{var r=null!=d.current;if(!h)return Promise.resolve(t("Calling the `request` function has no effect, Wake Lock Screen API isn't supported"));if(r)return Promise.resolve(t("Calling `request` multiple times without `release` has no effect"));var u=n(function(){return Promise.resolve(navigator.wakeLock.request(e)).then(function(e){d.current=e,d.current.onrelease=function(e){v(d.current&&d.current.released||!0),c&&c(e),d.current=null},o&&o(),v(d.current&&d.current.released||!1)})},function(e){i&&i(e)});return Promise.resolve(u&&u.then?u.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},[h,o,i,c]),P=r.useCallback(function(){try{return Promise.resolve(h?null===d.current?t("Calling `release` before `request` has no effect."):d.current?Promise.resolve(d.current.release()).then(function(e){}):void 0:t("Calling the `release` function has no effect, Wake Lock Screen API isn't supported"))}catch(e){return Promise.reject(e)}},[h]);return r.useEffect(function(){if(a){var e=function(){try{var e=function(){if(null===d.current&&"visible"===document.visibilityState){var e=n(function(){return Promise.resolve(m()).then(function(){})},function(e){null==i||i(e)});if(e&&e.then)return e.then(function(){})}}();return Promise.resolve(e&&e.then?e.then(function(){}):void 0)}catch(e){return Promise.reject(e)}};return document.addEventListener("visibilitychange",e),function(){document.removeEventListener("visibilitychange",e)}}},[a,m,i]),{isSupported:h,request:m,released:f,release:P,type:d.current&&d.current.type||void 0}}; //# sourceMappingURL=react-screen-wake-lock.cjs.map