Re: [wake-lock] WakeLock.request() returns a promise that never resolves (#226)

> AbortSignal wasn’t the right thing to use here.

Now that I'm back on firm soil, let me clarify... the `AbortController` usage was ambiguous because it was not clear if it applied to: 

 1. the asynchronous acquisition of the lock - requires IPC, but presumedly fairly quick.
 1. the actual lock itself - ongoing operation after. 
 1. both... which is complicated to implement IMO. 

> I don’t mind returning an object as the sentinel.

If we go down the object route again, which I think more clear, we can have a `WakeLockSentinel`  returned that just has `release()` method, a `type` attribute, and an event handler attribute to monitor state change, and a way to check current `state`. 

```JS
partial interface Navigator {
  [SameObject] readonly attribute WakeLock wakeLock;
};

partial interface WorkerNavigator {
  [SameObject] readonly attribute WakeLock wakeLock;
};

[Exposed=(Window,DedicatedWorker)]
interface WakeLock {
  Promise<WakeLockSentinel> request(WakeLockType type);
};

dictionary WakeLockEventInit {
  required unsigned WakeLockSentinel lock;
};

[Exposed=(Window,DedicatedWorker)]
interface WakeLockEvent : Event {
  constructor(DOMString type, WakeLockEventInit init);
  readonly attribute WakeLockSentinel lock;
};

enum WakeLockState {
  "active",
  "released",
}

interface WakeLockSentinel : EventTarget {
  // can only be called once
  Promise<void> release();
  readonly attribute WakeLockType type;
  attribute EventHandler onstatechange;
  readonly attribute WakeLockState state;
}
```

So roughly:

```JS
let lock = await navigator.wakeLock.request("screen");
lock.onstatechange = async ev => {
   // request a new lock, maybe...
   lock = await navigator.wakeLock.request(ev.lock.type); 
}

// ... stuff happens.... then...

if (lock.state === "released") return; 
try {
   await lock.release();
} catch (err) {
  // any unexpected errors
}

// Lock was released, so it's consumed/dead; this would reject:
await lock.release(); // InvalidStateError
```

I'd be happy with the above API.  

-- 
GitHub Notification of comment by marcoscaceres
Please view or discuss this issue at https://github.com/w3c/wake-lock/issues/226#issuecomment-533946734 using your GitHub account

Received on Monday, 23 September 2019 02:59:37 UTC