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) 1.51 kB
import*as e from"react";var r=function(e){return console.warn("[react-screen-wake-lock]: "+e)},n=function(n){var t=void 0===n?{}:n,o=t.onError,u=t.onRequest,c=t.onRelease,s=e.useState(),a=s[0],i=s[1],l=e.useRef(null),f="undefined"!=typeof window&&"wakeLock"in navigator,v=e.useCallback(function(e){void 0===e&&(e="screen");try{var n=null!=l.current;if(!f)return Promise.resolve(r("Calling the `request` function has no effect, Wake Lock Screen API isn't supported"));if(n)return Promise.resolve(r("Calling `request` multiple times without `release` has no effect"));var t=function(r,n){try{var t=Promise.resolve(navigator.wakeLock.request(e)).then(function(e){l.current=e,l.current.onrelease=function(e){i(l.current&&l.current.released||!0),c&&c(e),l.current=null},u&&u(),i(l.current&&l.current.released||!1)})}catch(e){return n(e)}return t&&t.then?t.then(void 0,n):t}(0,function(e){o&&o(e)});return Promise.resolve(t&&t.then?t.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},[f,u,o,c]),h=e.useCallback(function(){try{return Promise.resolve(f?null==l.current?r("Calling `release` before `request` has no effect."):l.current?Promise.resolve(l.current.release()).then(function(e){}):void 0:r("Calling the `release` function has no effect, Wake Lock Screen API isn't supported"))}catch(e){return Promise.reject(e)}},[f]);return{isSupported:f,request:v,released:a,release:h,type:l.current&&l.current.type||void 0}};export{n as useWakeLock}; //# sourceMappingURL=react-screen-wake-lock.esm.js.map