bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/wake-lock/service.js
Arthurk12 5d1611811a fix(wake lock): change request return value
Wake lock request function returns a boolean indicating whether there was an
error or not.
2023-06-14 13:38:06 -03:00

76 lines
1.8 KiB
JavaScript

import logger from '/imports/startup/client/logger';
import deviceInfo from '/imports/utils/deviceInfo';
const WAKELOCK_ENABLED = Meteor.settings.public.app.wakeLock.enabled;
class WakeLock {
constructor() {
this.sentinel = null;
this.apiSupport = 'wakeLock' in navigator;
}
static isEnabled() {
return WAKELOCK_ENABLED;
}
isSupported() {
const { isMobile } = deviceInfo;
return WakeLock.isEnabled() && this.apiSupport && isMobile;
}
isActive() {
return this.sentinel !== null;
}
handleVisibilityChanged() {
if (document.visibilityState === 'visible') {
this.request();
}
}
handleRelease() {
document.removeEventListener('visibilitychange', this.handleVisibilityChanged);
this.sentinel = null;
}
async request() {
if (!this.isSupported()) {
logger.warn({
logCode: 'wake_lock_request_error',
}, 'Wake lock API not supported');
return true;
}
try {
this.sentinel = await navigator.wakeLock.request('screen');
this.sentinel.addEventListener('release', this.handleRelease);
document.addEventListener('visibilitychange', this.handleVisibilityChanged.bind(this));
document.addEventListener('fullscreenchange', this.handleVisibilityChanged.bind(this));
} catch (err) {
logger.warn({
logCode: 'wake_lock_request_error',
extraInfo: {
errorName: err.name,
errorMessage: err.message,
},
}, 'Error requesting wake lock.');
return true;
}
return false;
}
release() {
if (this.isActive()) this.sentinel.release();
}
}
const wakeLock = new WakeLock();
export default {
isEnabled: () => wakeLock.isEnabled(),
isSupported: () => wakeLock.isSupported(),
isActive: () => wakeLock.isActive(),
request: () => wakeLock.request(),
release: () => wakeLock.release(),
};