Skip to content

Commit e5c126a

Browse files
committed
Add error state for wakelock testing.
Add an error state to the WakeLockManager state machine. This adds a transition that can be detected from tests (it otherwise serves no purpose, and the system immediatly transitions back into the released state).
1 parent c1620e6 commit e5c126a

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

core/util/wakelock.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ const _STATES = {
1919
* Can transition to:
2020
* - AWAITING_VISIBLE: `acquire` called when document is hidden.
2121
* - ACQUIRING: `acquire` called.
22-
* - RELEASED: `acquired` called when the api is not available.
22+
* - ERROR: `acquired` called when the api is not available.
2323
*/
2424
RELEASED: 'released',
2525
/* Wake lock requested, waiting for browser.
2626
*
2727
* Can transition to:
2828
* - ACQUIRED: success
2929
* - ACQUIRING_WANT_RELEASE: `release` called while waiting
30-
* - RELEASED: On error
30+
* - ERROR
3131
*/
3232
ACQUIRING: 'acquiring',
3333
/* Wake lock requested, release called, still waiting for browser.
@@ -51,6 +51,12 @@ const _STATES = {
5151
* - RELEASED: when release is called.
5252
*/
5353
AWAITING_VISIBLE: 'awaiting_visible',
54+
/* An error has occurred.
55+
*
56+
* Can transition to:
57+
* - RELEASED: will happen immediately.
58+
*/
59+
ERROR: 'error',
5460
};
5561

5662
export default class WakeLockManager {
@@ -77,6 +83,7 @@ export default class WakeLockManager {
7783
case _STATES.ACQUIRING:
7884
case _STATES.ACQUIRED:
7985
break;
86+
case _STATES.ERROR:
8087
case _STATES.RELEASED:
8188
if (document.hidden) {
8289
// We can not acquire the wakelock while the document is
@@ -92,6 +99,7 @@ export default class WakeLockManager {
9299

93100
release() {
94101
switch (this._state) {
102+
case _STATES.ERROR:
95103
case _STATES.RELEASED:
96104
case _STATES.ACQUIRING_WANT_RELEASE:
97105
break;
@@ -132,13 +140,15 @@ export default class WakeLockManager {
132140
_acquireWakelockNow() {
133141
if (!("wakeLock" in navigator)) {
134142
Log.Warn("Unable to request wakeLock, Browser does not have wakeLock api");
143+
this._transitionTo(_STATES.ERROR);
135144
this._transitionTo(_STATES.RELEASED);
136145
return;
137146
}
138147
navigator.wakeLock.request("screen")
139148
.then(this._eventHandlers.wakelockAcquired)
140149
.catch((err) => {
141150
Log.Warn("Error occurred while acquiring wakelock: " + err);
151+
this._transitionTo(_STATES.ERROR);
142152
this._transitionTo(_STATES.RELEASED);
143153
});
144154
this._transitionTo(_STATES.ACQUIRING);
@@ -148,7 +158,7 @@ export default class WakeLockManager {
148158
_wakelockAcquired(wakelock) {
149159
if (this._state === _STATES.ACQUIRING_WANT_RELEASE) {
150160
// We were requested to release the wakelock while we were trying to
151-
// acquire it. Now that we have acquired it, immediatly release it.
161+
// acquire it. Now that we have acquired it, immediately release it.
152162
wakelock.release();
153163
this._transitionTo(_STATES.RELEASED);
154164
return;

0 commit comments

Comments
 (0)