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.99 kB
JavaScript
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],r):r((e||self).reactScreenWakeLock={},e.react)}(this,function(e,r){function t(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 n=/*#__PURE__*/t(r),o=function(e){return console.warn("[react-screen-wake-lock]: "+e)};e.useWakeLock=function(e){var r=void 0===e?{}:e,t=r.onError,u=r.onRequest,c=r.onRelease,i=n.useState(),a=i[0],s=i[1],l=n.useRef(null),f="undefined"!=typeof window&&"wakeLock"in navigator,d=n.useCallback(function(e){void 0===e&&(e="screen");try{var r=null!=l.current;if(!f)return Promise.resolve(o("Calling the `request` function has no effect, Wake Lock Screen API isn't supported"));if(r)return Promise.resolve(o("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){s(l.current&&l.current.released||!0),c&&c(e),l.current=null},u&&u(),s(l.current&&l.current.released||!1)})}catch(e){return t(e)}return n&&n.then?n.then(void 0,t):n}(0,function(e){t&&t(e)});return Promise.resolve(n&&n.then?n.then(function(){}):void 0)}catch(e){return Promise.reject(e)}},[f,u,t,c]),v=n.useCallback(function(){try{return Promise.resolve(f?null==l.current?o("Calling `release` before `request` has no effect."):l.current?Promise.resolve(l.current.release()).then(function(e){}):void 0:o("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:d,released:a,release:v,type:l.current&&l.current.type||void 0}}});
//# sourceMappingURL=react-screen-wake-lock.umd.js.map