Skip to content

Commit

Permalink
Merge pull request #8 from jorisre/reduce-bundle-size
Browse files Browse the repository at this point in the history
perf: reduce bundle size
  • Loading branch information
jorisre authored Nov 22, 2020
2 parents 2c18fdd + a6a52d9 commit 18d9863
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 26 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@
"size-limit": [
{
"path": "dist/react-screen-wake-lock.production.js",
"limit": "420 B"
"limit": "389 B"
},
{
"path": "dist/react-screen-wake-lock.module.js",
"limit": "450 B"
"limit": "411 B"
},
{
"path": "dist/react-screen-wake-lock.umd.js",
"limit": "435 B"
"limit": "395 B"
}
],
"devDependencies": {
Expand Down Expand Up @@ -107,7 +107,6 @@
"size-limit": "^4.9.0",
"tiny-warning": "^1.0.3",
"ts-jest": "^26.4.4",
"tslib": "^2.0.3",
"typescript": "^4.1.2"
},
"lint-staged": {
Expand Down
29 changes: 12 additions & 17 deletions src/use-wake-lock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@ export const useWakeLock = ({

const request = React.useCallback(
async (type: WakeLockType = 'screen') => {
if (!isSupported) {
const isWakeLockAlreadyDefined = wakeLock.current != null;
if (!isSupported || isWakeLockAlreadyDefined) {
warning(
!isSupported,
"Calling the `request` function has no effect, Wake Lock Screen API isn't supported"
);
return;
}

if (wakeLock.current != null) {
warning(
wakeLock.current != null,
isWakeLockAlreadyDefined,
'Calling `request` multiple times without `release` has no effect'
);
return;
Expand All @@ -41,45 +38,43 @@ export const useWakeLock = ({

wakeLock.current.onrelease = (e: Event) => {
// Default to `true` - `released` API is experimental: https://caniuse.com/mdn-api_wakelocksentinel_released
setReleased(wakeLock.current?.released ?? true);
onRelease?.(e);
setReleased((wakeLock.current && wakeLock.current.released) ?? true);
onRelease && onRelease(e);
wakeLock.current = null;
};

onRequest?.();
setReleased(wakeLock.current.released ?? false);
setReleased((wakeLock.current && wakeLock.current.released) ?? false);
} catch (error) {
onError?.(error);
onError && onError(error);
}
},
[isSupported, onRequest, onError, onRelease]
);

const release = React.useCallback(async () => {
if (!isSupported) {
const isWakeLockUndefined = wakeLock.current == null;
if (!isSupported || isWakeLockUndefined) {
warning(
!isSupported,
"Calling the `release` function has no effect, Wake Lock Screen API isn't supported"
);
return;
}

if (wakeLock.current == null) {
warning(
wakeLock.current == null,
isWakeLockUndefined,
'Calling `release` before `request` has no effect.'
);
return;
}

await wakeLock.current?.release();
wakeLock.current && (await wakeLock.current.release());
}, [isSupported]);

return {
isSupported,
request,
released,
release,
type: wakeLock?.current?.type,
type: (wakeLock.current && wakeLock.current.type) || undefined,
};
};
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11478,11 +11478,6 @@ tslib@^1.13.0, tslib@^1.8.1, tslib@^1.9.0:
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg==

tslib@^2.0.3:
version "2.0.3"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.0.3.tgz#8e0741ac45fc0c226e58a17bfc3e64b9bc6ca61c"
integrity sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==

tsutils@^3.17.1:
version "3.17.1"
resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.17.1.tgz#ed719917f11ca0dee586272b2ac49e015a2dd759"
Expand Down

1 comment on commit 18d9863

@vercel
Copy link

@vercel vercel bot commented on 18d9863 Nov 22, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.