-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Consider using AbortController rather than a stop() method #225
Comments
Yes, which was due to consistency with Generic Sensors. It is probably also a much more long running task than a push. I would like to hear pros and cons for changing this |
Consistency definitely is a big plus, Wake Lock recently also switched over to |
Sketchy example, const reader = new NFCReader();
reader.onreading = e => {
};
const controller = new AbortController();
reader.start({ signal: controller.signal });
// Later...
controller.abort(); Pros:
Cons:
|
I think we should just do both. Then you get consistency with newer specs and generic sensors, and you can remove the "cons: verbosity" |
One thing about WakeLock.request(type, {signal})
.catch(err => {
console.error(err.name, err.message);
}); |
The promise should settle, one way or another. If fulfillment implies you got the wake lock, then you should definitely reject if you cancel and don't get the wake lock. |
I’m more concerned about “planned aborts”, check this contrived example (see console). When you click the window, the Wake Lock is released. All planned, yet an “error”. |
In that case, how do you suggest the promise settles? |
Coming back to the initial question of the present issue, I think a As @jakearchibald wrote on Web Fundamentals: "You don't often want to show an error message if the user aborted the operation, as it isn't an "error" if you successfully do that the user asked. To avoid this, use an if-statement […] to handle abort errors specifically." Anyway, +1 for consistency and |
I would strongly advocate for the use of the primitive const reader = new NFCReader();
reader.onreading = e => { /* ... */ };
reader.onerror = e => { /* ... */ };
const controller = new AbortController();
controller.signal.onabort = e => {
// We're done waiting for NFC tags.
});
reader.read({ signal: controller.signal });
// Stop reading NFC tags after 3s.
setTimeout(() => controller.abort(), 3000); |
Maybe nitpicking, but What is useful from NFC point of view is setting up the read and error handlers. On the other hand, I don't see that aborting a read would be a real use case: the user needs to hold the device close to an NFC device, and in the same time cancel the read, or do something else that results in losing focus/visibility that would suspend the read? and script would like to override that with rather canceling than suspending? Sounds a bit fuzzy to me. IIRC the But if you have a use case for explicitly aborting an NFC read (from a script), instead of just pulling away the device from the NFC device by the end user, please speak up :). Unless we have a compelling use case, could we try shipping without abort support for now and add it later when having use cases? |
I am fine with the idea. Makes sense especially for SPAs etc where you change view. We already use 'push' (single future write) for NFCWriter, so using 'scan' (multiple future reads) seems fine |
Using the name I would still like to get clarity on what use cases we might have for aborting NFC reads (or scans). Ideas still welcome there. |
OK, so to close this off and with my active editor hat on, I am making an executive decision that we move ahead with the rename to "scan" and integration with Some of my reasoning.
You could argue that that is not used by Generic Sensors, but I would argue that it would have been if they were designed today, and still might be for the For me personally, it also seem quite nice to be able to use one signal to cancel multiple things in a single-page application when switching views, without requiring having access to all kind of internal state of the views that are now being hidden. |
Somehow #147 became about
cancelPush()
rather thancancelWatch()
(nowstop()
).push()
now takes anAbortSignal
butNFCReader
still uses thestart()
andstop()
pattern.The text was updated successfully, but these errors were encountered: