From 19088f7dabf51d8bc406f67e2cf28fc561ef1ec9 Mon Sep 17 00:00:00 2001 From: Mojtaba Ghasemzadeh Tehrany Date: Sun, 4 Aug 2024 13:58:42 +0330 Subject: [PATCH 1/2] feat: handle visibility change in web --- src/web.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/web.ts b/src/web.ts index ebc2ee8..c067fbb 100644 --- a/src/web.ts +++ b/src/web.ts @@ -9,6 +9,10 @@ import type { export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { private wakeLock: WakeLockSentinel | null = null; private readonly _isSupported = 'wakeLock' in navigator; + + private handleVisibilityChange = () => { + if (document.visibilityState === 'visible') this.keepAwake(); + } public async keepAwake(): Promise { if (!this._isSupported) { @@ -18,6 +22,8 @@ export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { await this.allowSleep(); } this.wakeLock = await navigator.wakeLock.request('screen'); + document.addEventListener('visibilitychange', this.handleVisibilityChange); + document.addEventListener('fullscreenchange', this.handleVisibilityChange); } public async allowSleep(): Promise { @@ -26,6 +32,8 @@ export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { } this.wakeLock?.release(); this.wakeLock = null; + document.removeEventListener('visibilitychange', this.handleVisibilityChange); + document.removeEventListener('fullscreenchange', this.handleVisibilityChange); } public async isSupported(): Promise { From f96422cc72fada6630f8af3df4bca4292c9433d8 Mon Sep 17 00:00:00 2001 From: sabereen Date: Mon, 12 Aug 2024 21:01:23 +0330 Subject: [PATCH 2/2] chore: format the code --- src/web.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/web.ts b/src/web.ts index c067fbb..a0af422 100644 --- a/src/web.ts +++ b/src/web.ts @@ -9,10 +9,10 @@ import type { export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { private wakeLock: WakeLockSentinel | null = null; private readonly _isSupported = 'wakeLock' in navigator; - + private handleVisibilityChange = () => { if (document.visibilityState === 'visible') this.keepAwake(); - } + }; public async keepAwake(): Promise { if (!this._isSupported) { @@ -23,7 +23,7 @@ export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { } this.wakeLock = await navigator.wakeLock.request('screen'); document.addEventListener('visibilitychange', this.handleVisibilityChange); - document.addEventListener('fullscreenchange', this.handleVisibilityChange); + document.addEventListener('fullscreenchange', this.handleVisibilityChange); } public async allowSleep(): Promise { @@ -32,8 +32,14 @@ export class KeepAwakeWeb extends WebPlugin implements KeepAwakePlugin { } this.wakeLock?.release(); this.wakeLock = null; - document.removeEventListener('visibilitychange', this.handleVisibilityChange); - document.removeEventListener('fullscreenchange', this.handleVisibilityChange); + document.removeEventListener( + 'visibilitychange', + this.handleVisibilityChange, + ); + document.removeEventListener( + 'fullscreenchange', + this.handleVisibilityChange, + ); } public async isSupported(): Promise {