bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/wake-lock/service.js
Arthurk12 f880e56a1e feat(wake-lock): prototype
Adds wake lock feature, which is available only for mobile users using
browser with support to the given API.
When an user using a supported device joins the meeting, a toast is
displayed offering to activate the feature.
Adds a toggle under settings menu to activate/deactivate it.
2023-06-13 16:22:02 -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.enableWakeLock;
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 false;
}
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 false;
}
return true;
}
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(),
};