Skip to content

Commit

Permalink
Closed #281
Browse files Browse the repository at this point in the history
Can't work on the background.
  • Loading branch information
mantou132 committed Oct 5, 2023
1 parent 4c82d49 commit e7ec5bd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/webapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@graphql-codegen/typescript-operations": "^2.2.1",
"@nesbox/config": "^0.0.1",
"@tauri-apps/api": "1.0.0-rc.7",
"@types/dom-screen-wake-lock": "^1.0.1",
"@types/dom-speech-recognition": "^0.0.1",
"@types/marked": "^4.0.3",
"dotenv": "^16.0.1",
Expand Down
29 changes: 29 additions & 0 deletions packages/webapp/src/effects/wake-lock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { logger } from 'src/logger';

export function wakeLock() {
let wakeLockPromise: Promise<WakeLockSentinel> | null = null;

const listener = () => {
if (document.visibilityState === 'visible') {
wakeLockPromise = navigator.wakeLock?.request('screen');

// log
wakeLockPromise?.then((wakeLock) => {
logger.info('wake lock created!');
wakeLock.addEventListener('release', () => {
logger.info('wake lock released!');
});
});
}
};

listener();

// 当页面处于非活动状态时该锁自动失效
document.addEventListener('visibilitychange', listener);
return async () => {
document.removeEventListener('visibilitychange', listener);

(await wakeLockPromise)?.release();
};
}
2 changes: 2 additions & 0 deletions packages/webapp/src/pages/mt-room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { leaveRoom, updateRoomScreenshot } from 'src/services/api';
import { configure } from 'src/configure';
import { theme } from 'src/theme';
import { updateMtApp } from 'src/mt-app';
import { wakeLock } from 'src/effects/wake-lock';

import type { MStageElement } from 'src/modules/stage';
import type { MVoiceRoomElement } from 'src/modules/room-voice';
Expand Down Expand Up @@ -95,6 +96,7 @@ export class PMtRoomElement extends GemElement {
};

mounted = () => {
this.effect(wakeLock, () => []);
this.effect(
() => {
if (configure.user && !this.#playing) {
Expand Down
2 changes: 2 additions & 0 deletions packages/webapp/src/pages/room.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { logger } from 'src/logger';
import { ScUserStatus } from 'src/generated/graphql';
import { theme } from 'src/theme';
import { mountedRoom, unmountedRoom } from 'src/modules/nav';
import { wakeLock } from 'src/effects/wake-lock';

import type { MStageElement } from 'src/modules/stage';

Expand Down Expand Up @@ -352,6 +353,7 @@ export class PRoomElement extends GemElement {
});

mounted = () => {
this.effect(wakeLock, () => []);
this.effect(
() => {
if (configure.user && !this.#playing) {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1793,6 +1793,11 @@
dependencies:
"@types/node" "*"

"@types/dom-screen-wake-lock@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@types/dom-screen-wake-lock/-/dom-screen-wake-lock-1.0.1.tgz#8f125f91821d49f51feee95e6db128eb6974aea9"
integrity sha512-WJQas3OFGcC8AeMzaa7FwzzbNNfanuV2R12kQYNp4BkUMghsRz5JxJ5RgVhJifhw7t0s6LvRSWZArmKbMDZ+5g==

"@types/dom-speech-recognition@^0.0.1":
version "0.0.1"
resolved "https://registry.yarnpkg.com/@types/dom-speech-recognition/-/dom-speech-recognition-0.0.1.tgz#e326761a04b4a49c0eec2ac7948afc1c6aa12baa"
Expand Down

0 comments on commit e7ec5bd

Please sign in to comment.