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.78 kB
function e(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach(function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,n.get?n:{enumerable:!0,get:function(){return e[t]}})}}),r.default=e,r}var r=/*#__PURE__*/e(require("react")),t=function(e){return console.warn("[react-screen-wake-lock]: "+e)};exports.useWakeLock=function(e){var n=void 0===e?{}:e,u=n.onError,o=n.onRequest,c=n.onRelease,s=r.useState(),a=s[0],i=s[1],l=r.useRef(null),f="undefined"!=typeof window&&"wakeLock"in navigator,v=r.useCallback(function(e){void 0===e&&(e="screen");try{var r=null!=l.current;if(!f)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 n=function(r,t){try{var n=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},o&&o(),i(l.current&&l.current.released||!1)})}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}(0,function(e){u&&u(e)});return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},[f,o,u,c]),d=r.useCallback(function(){try{return Promise.resolve(f?null==l.current?t("Calling `release` before `request` has no effect."):l.current?Promise.resolve(l.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)}},[f]);return{isSupported:f,request:v,released:a,release:d,type:l.current&&l.current.type||void 0}}; //# sourceMappingURL=react-screen-wake-lock.cjs.map