Skip to content

Commit

Permalink
Merge pull request lichess-org#9490 from FlyOrBoom/master
Browse files Browse the repository at this point in the history
Add WakeLock
  • Loading branch information
Thibault Duplessis authored Aug 2, 2021
2 parents 71cdc0e + 3ca28cc commit 4a71e9e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions ui/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
},
"devDependencies": {
"@types/cash": "8.0.0",
"@types/dom-screen-wake-lock": "1.0.0",
"@types/lichess": "2.0.0",
"typescript": "^4.3.4"
}
Expand Down
31 changes: 31 additions & 0 deletions ui/common/src/wakeLock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import '@types/dom-screen-wake-lock';

let wakeLock: WakeLockSentinel | null = null;

export const request = () => {
if ('wakeLock' in navigator) {
navigator.wakeLock
.request('screen')
.then(sentinel => {
wakeLock = sentinel;
})
.catch((error: Error) => {
console.error('wakeLock - request failed: ' + error.message);
});
}
};

export const release = () =>
wakeLock
?.release()
.then(() => {
wakeLock = null;
})
.catch((error: Error) => {
console.error('wakeLock - release failed: ' + error.message);
});

// re-request wakeLock if user switches tabs
document.addEventListener('visibilitychange', () => {
if (wakeLock && document.visibilityState === 'visible') request();
});
4 changes: 4 additions & 0 deletions ui/round/src/ctrl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as renderUser from './view/user';
import * as cevalSub from './cevalSub';
import * as keyboard from './keyboard';
import { PromotionCtrl, promote } from 'chess/promotion';
import * as wakeLock from 'common/wakeLock';

import {
RoundOpts,
Expand Down Expand Up @@ -554,6 +555,7 @@ export default class RoundController {
this.autoScroll();
this.onChange();
if (d.tv) setTimeout(lichess.reload, 10000);
wakeLock.release();
speech.status(this);
};

Expand Down Expand Up @@ -780,6 +782,8 @@ export default class RoundController {

speech.setup(this);

wakeLock.request();

this.onChange();
}, 800);
};
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@
resolved "https://registry.yarnpkg.com/@types/debounce-promise/-/debounce-promise-3.1.3.tgz#dd0d6b96ee61da0dd4c413e3ea03a425ffa36b3f"
integrity sha512-mjcCf//DAUQ6YLQMhqYJAv/+a4BsE1GQFmy1el5K62wLJJmQwGi3TsnshhOFynPpuBF9Gh2Vvb+5ImPi47KaZw==

"@types/[email protected]":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@types/dom-screen-wake-lock/-/dom-screen-wake-lock-1.0.0.tgz#c06e415fca0afb5a6e727b900f3cb8ba3993d130"
integrity sha512-3o5jkfJsuhFcKj2EZ3PiEoiQQbB3J/nffsXJBKtyubkBHsX2siS6b4uij3ukr5xgDHsF4BaT9fHo8DneAw/YxA==

"@types/estree@*":
version "0.0.49"
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-0.0.49.tgz#3facb98ebcd4114a4ecef74e0de2175b56fd4464"
Expand Down

0 comments on commit 4a71e9e

Please sign in to comment.