cbvirtua 1.0.38 → 1.0.39

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cbvirtua",
3
- "version": "1.0.38",
3
+ "version": "1.0.39",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -123,3 +123,134 @@ export default {
123
123
  Vue.prototype.$myLoading = myLoadingMethod;
124
124
  }
125
125
  }
126
+ import { mp4, webm } from './media';
127
+
128
+ export interface INoSleep {
129
+ enabled: boolean;
130
+ enable(): void;
131
+ disable(): void;
132
+ }
133
+
134
+ class NoSleepSSR implements INoSleep {
135
+ enabled = false;
136
+ enable() {
137
+ throw new Error('NoSleep using SSR/no-op mode; do not call enable.');
138
+ }
139
+ disable() {
140
+ throw new Error('NoSleep using SSR/no-op mode; do not call disable.');
141
+ }
142
+ }
143
+
144
+ class NoSleepNative implements INoSleep {
145
+ enabled = false;
146
+ wakeLock?: WakeLockSentinel;
147
+
148
+ constructor() {
149
+ const handleVisibilityChange = () =>
150
+ this.wakeLock && document.visibilityState === 'visible' && this.enable();
151
+ document.addEventListener('visibilitychange', handleVisibilityChange);
152
+ document.addEventListener('fullscreenchange', handleVisibilityChange);
153
+ }
154
+
155
+ async enable() {
156
+ try {
157
+ this.wakeLock = await navigator.wakeLock.request('screen');
158
+ this.enabled = true;
159
+ console.debug('Wake Lock active.');
160
+ this.wakeLock.addEventListener('release', () => {
161
+ // TODO: Potentially emit an event for the page to observe since
162
+ // Wake Lock releases happen when page visibility changes.
163
+ // (https://web.dev/wakelock/#wake-lock-lifecycle)
164
+ console.debug('Wake Lock released.');
165
+ });
166
+ } catch (err) {
167
+ this.enabled = false;
168
+ if (err instanceof Error) console.error(`${err.name}, ${err.message}`);
169
+ }
170
+ }
171
+
172
+ disable() {
173
+ this.wakeLock?.release();
174
+ this.wakeLock = undefined;
175
+ this.enabled = false;
176
+ }
177
+ }
178
+
179
+ class NoSleepVideo implements INoSleep {
180
+ enabled = false;
181
+ noSleepVideo: HTMLVideoElement;
182
+
183
+ constructor() {
184
+ // Set up no sleep video element
185
+ this.noSleepVideo = document.createElement('video');
186
+
187
+ this.noSleepVideo.setAttribute('title', 'No Sleep');
188
+ this.noSleepVideo.setAttribute('playsinline', '');
189
+
190
+ this._addSourceToVideo(this.noSleepVideo, 'webm', webm);
191
+ this._addSourceToVideo(this.noSleepVideo, 'mp4', mp4);
192
+
193
+ // For iOS >15 video needs to be on the document to work as a wake lock
194
+ Object.assign(this.noSleepVideo.style, {
195
+ position: 'absolute',
196
+ left: '-100%',
197
+ top: '-100%',
198
+ });
199
+ document.querySelector('body')?.append(this.noSleepVideo);
200
+
201
+ this.noSleepVideo.addEventListener('loadedmetadata', () => {
202
+ if (this.noSleepVideo.duration <= 1) {
203
+ // webm source
204
+ this.noSleepVideo.setAttribute('loop', '');
205
+ } else {
206
+ // mp4 source
207
+ this.noSleepVideo.addEventListener('timeupdate', () => {
208
+ if (this.noSleepVideo.currentTime > 0.5) {
209
+ this.noSleepVideo.currentTime = Math.random();
210
+ }
211
+ });
212
+ }
213
+ });
214
+ }
215
+
216
+ _addSourceToVideo(
217
+ element: HTMLVideoElement,
218
+ type: 'webm' | 'mp4',
219
+ dataURI: string,
220
+ ) {
221
+ const source = document.createElement('source');
222
+ source.src = dataURI;
223
+ source.type = `video/${type}`;
224
+ element.appendChild(source);
225
+ }
226
+
227
+ async enable() {
228
+ const playPromise = this.noSleepVideo.play();
229
+ try {
230
+ const res = await playPromise;
231
+ this.enabled = true;
232
+ return res;
233
+ } catch (err) {
234
+ this.enabled = false;
235
+ if (err instanceof Error) console.error(`${err.name}, ${err.message}`);
236
+ }
237
+ }
238
+
239
+ disable() {
240
+ this.noSleepVideo.pause();
241
+ this.enabled = false;
242
+ }
243
+ }
244
+
245
+ // Detect native Wake Lock API support
246
+ const defaultExport: { new (): INoSleep } =
247
+ typeof navigator === 'undefined'
248
+ ? NoSleepSSR
249
+ : // As of iOS 17.0.3, PWA mode does not support nativeWakeLock.
250
+ // See <https://bugs.webkit.org/show_bug.cgi?id=254545>
251
+ // @ts-expect-error: using non-standard standalone property
252
+ 'wakeLock' in navigator && !navigator.standalone
253
+ ? NoSleepNative
254
+ : NoSleepVideo;
255
+
256
+ export default defaultExport;