Skip to content

Commit f8fe617

Browse files
committed
release wakelock on blur
1 parent af9d5d1 commit f8fe617

5 files changed

Lines changed: 64 additions & 9 deletions

File tree

‎.fat‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
dist/demo/atlas.webp 1432 1457
22
dist/demo/index.css 69 94
3-
dist/demo/index.html 133312 64769
4-
dist/demo/index.js 128231 60585
5-
dist/demo/index.wasm 70041 37786
3+
dist/demo/index.html 133448 64788
4+
dist/demo/index.js 128367 60606
5+
dist/demo/index.wasm 70045 37797

‎src/void/vweb/eng/eng.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,11 +353,11 @@ export class Eng {
353353
}
354354

355355
#onVisibility = (): void => {
356-
this.#wakelock.update()
357356
this.#onFocus()
358357
}
359358

360359
#onFocus = (): void => {
360+
this.#wakelock.update()
361361
this.#input.reset()
362362
this.#cancelFrame()
363363
clearTimeout(this.#updateTimeoutId)

‎src/void/vweb/eng/wakelock.test.ts‎

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {assert} from '../test/assert.ts'
33
import {WakelockMock} from '../test/wakelock-mock.ts'
44
import {Wakelock} from './wakelock.ts'
55

6-
test('Wakelock acquires and reacquires after browser release', async () => {
6+
test('wakelock acquires and reacquires after browser release', async () => {
77
using mock = new WakelockMock()
88
const wakelock = new Wakelock()
99
wakelock.update()
@@ -15,7 +15,7 @@ test('Wakelock acquires and reacquires after browser release', async () => {
1515
assert.equal(wakelock.locked, true)
1616
})
1717

18-
test('Wakelock stays unlocked when the browser rejects it', async () => {
18+
test('wakelock stays unlocked when the browser rejects it', async () => {
1919
using mock = new WakelockMock()
2020
mock.rejection = Error('rejected')
2121
const wakelock = new Wakelock()
@@ -27,3 +27,53 @@ test('Wakelock stays unlocked when the browser rejects it', async () => {
2727
await Promise.resolve()
2828
assert.equal(mock.reqs, 2)
2929
})
30+
31+
test('wakelock follows document focus', async () => {
32+
using mock = new WakelockMock()
33+
mock.focused = false
34+
const wakelock = new Wakelock()
35+
wakelock.update()
36+
await Promise.resolve()
37+
assert.equal(mock.reqs, 0)
38+
assert.equal(wakelock.locked, false)
39+
40+
mock.focused = true
41+
wakelock.update()
42+
await Promise.resolve()
43+
assert.equal(wakelock.locked, true)
44+
45+
mock.focused = false
46+
wakelock.update()
47+
assert.equal(mock.releases, 1)
48+
assert.equal(wakelock.locked, false)
49+
50+
mock.focused = true
51+
wakelock.update()
52+
await Promise.resolve()
53+
assert.equal(mock.reqs, 2)
54+
assert.equal(wakelock.locked, true)
55+
})
56+
57+
test('wakelock releases a pending acquisition after focus loss', async () => {
58+
using mock = new WakelockMock()
59+
const wakelock = new Wakelock()
60+
wakelock.update()
61+
mock.focused = false
62+
wakelock.update()
63+
await Promise.resolve()
64+
assert.equal(mock.reqs, 1)
65+
assert.equal(mock.releases, 1)
66+
assert.equal(wakelock.locked, false)
67+
})
68+
69+
test('wakelock stays unlocked after browser release without focus', async () => {
70+
using mock = new WakelockMock()
71+
const wakelock = new Wakelock()
72+
wakelock.update()
73+
await Promise.resolve()
74+
mock.focused = false
75+
mock.sentinel?.dispatchEvent(new Event('release'))
76+
await Promise.resolve()
77+
assert.equal(mock.reqs, 1)
78+
assert.equal(wakelock.locked, false)
79+
})

‎src/void/vweb/eng/wakelock.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class Wakelock {
77
}
88

99
update(): void {
10-
if (document.visibilityState !== 'visible') {
10+
if (document.visibilityState !== 'visible' || !document.hasFocus()) {
1111
this.#unlock()
1212
return
1313
}
@@ -25,7 +25,7 @@ export class Wakelock {
2525
return
2626
}
2727
this.#locking = false
28-
if (document.visibilityState !== 'visible') {
28+
if (document.visibilityState !== 'visible' || !document.hasFocus()) {
2929
await this.#releaseSentinel(sentinel)
3030
return
3131
}

‎src/void/vweb/test/wakelock-mock.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export type WakelockSentinelMock = EventTarget & {
33
}
44

55
export class WakelockMock {
6+
focused: boolean = true
67
reqs: number = 0
78
releases: number = 0
89
rejection?: Error
@@ -11,14 +12,18 @@ export class WakelockMock {
1112
readonly #wakeLock = navigator.wakeLock
1213

1314
constructor() {
14-
globalThis.document = {visibilityState: 'visible'} as Document
15+
globalThis.document = {
16+
visibilityState: 'visible',
17+
hasFocus: () => this.focused
18+
} as Document
1519
;(navigator as {wakeLock: WakeLock}).wakeLock = {
1620
request: async (): Promise<WakeLockSentinel> => {
1721
this.reqs++
1822
if (this.rejection) throw this.rejection
1923
const sentinel = Object.assign(new EventTarget(), {
2024
release: async (): Promise<void> => {
2125
this.releases++
26+
sentinel.dispatchEvent(new Event('release'))
2227
}
2328
})
2429
this.sentinel = sentinel

0 commit comments

Comments
 (0)