-
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
Use addEventListener instead of watch #152
Comments
We could add a filter to |
https://dom.spec.whatwg.org/#dictdef-addeventlisteneroptions |
But not those NFC options. Is it better to extend a standard dictionary with NFC-specific properties (making it apparently aligned), or have a separate call with its own, admittedly different semantics? |
I think it is better extending. Creating something that looks similar but has edge cases that works differently isn't really good. Other option would be (feel free to bikeshed names) const filter = navigator.nfc.createReadFilter({
url: document.baseURI,
recordType: "json"
});
const onMessage = message => {
for (let record of message.records) {
let article =/[aeio]/.test(record.data.title) ? "an" : "a";
console.log(`${record.data.name} is ${article} ${record.data.title}`);
}
};
filter.addEventListener('exchange', onMessage, {capture: false});
filter.startNotifications(); |
Maybe so, but what is the general consensus or mechanism on extending standard dictionaries in the platform? Do we need to register those somewhere? (imagine multiple specs adding properties which may clash)
Extending a standard dictionary looks exactly like this. So it's a counter-argument.
So we'd need to specify a new NFCFilter interface that contains the One way we could do it is define NfcWatchOptions and NfcPushOptions as interfaces (strip the Options from the name), add the |
It acts exactly the same. It should honor all the options from AddEventListenerOptions
Sure, and it's more similar to WebBluetooth which has
Would be really interesting hearing opinions from @domenic @reillyeon |
On the other hand we can't really expect to handle NFC interactions with Bluetooth interactions. |
I think in general APIs are migrating away from explicitly passing callbacks in favor of either Promises for one-shot events or event listeners for repeating events. I like the idea of creating an |
Yes, something like this would do: [Constructor(NFCWatchOptions options), SecureContext]
interface NFCReadFilter: EventTarget {
readonly attribute USVString url;
readonly attribute NFCRecordType recordType;
readonly attribute USVString mediaType;
readonly attribute NFCWatchMode mode;
attribute EventHandler onread;
};
interface NFCReadEvent: Event {
readonly attribute NFCMessage message;
}; or if we want to bind it to the |
I still have the question from above:
|
Yes, that is a fear, so we should be careful when doing so, and work with the people who specced the upstream feature. Luckily, we also pass these specs through the TAG review which should catch most of these. With the For factory function, I think that would make more sense for the NFC object if we expected to have multiple of those, and would be in line with Generic Sensors. But it is not really a sensor and we don't expect to have multiple (if so a default could be chosen by the browser, I guess, like camera) |
I am fine with factory methods, but if we wanted constructors it's well possible. Filters are independent of NFC adapters, so no real need to bind to the nfc object syntactically (only in runtime: when when filters are constructed, implementations/algorithms should do the standard checks for support, secure context/permission etc and throw if there is an issue). Even if there are multiple NFC readers, we have agreed to have an additional API in the future for enumerating/selecting them for write (currently all of them can read, and any of them is allowed to write, so if they are too close, it might happen to attempt writing a tag from two adapters, which is anyway a deployment problem and should be avoided). So at any point implementations will know how to handle the NFC adapters. However, the API would be perhaps cleaner with the filter creation under the nfc object, since after this change it would only contain the [SecureContext]
interface NFC {
Promise<void> push(NFCPushMessage message, optional NFCPushOptions options);
Promise<NFCReadFilter> createReadFilter(optional NFCFilterOptions options);
}; Note that |
You shouldn't modify a foundational method like that of the platform for NFC-specific use cases. So the "Other option" from #152 (comment) makes more sense to me. (Although I'm unsure why you'd need to do Otherwise, I strongly agree that inventing your own event listener-like infrastructure is not good, and following the precedent set by other specs makes more sense. Especially if we already have such a strong parallel with Web Bluetooth.
Constructor is preferable if possible! I wonder why Web Bluetooth went with something else. Does creating it have to be async? Your last IDL block has it as promise-returning, and it's not clear to me why. |
|
The watch() algorithm was asynchronous, but indeed the user intent to create a read filter does not need to wait until NFC is configured, so we could create the object right away. We could handle security/permissions/support errors with exceptions. We could eventually include an |
I agree with @reillyeon we should just go for a version with a constructor. Maybe it is fine just calling it [Constructor(NFCReaderOptions options), SecureContext]
interface NFCReader : EventTarget {
readonly attribute USVString scope;
readonly attribute NFCRecordType recordType;
readonly attribute USVString mediaType;
readonly attribute NFCReaderMode mode;
attribute EventHandler onread;
};
interface NFCReadEvent: Event {
readonly attribute NFCMessage message;
};
interface NFCWriter {
Promise<void> push(NFCMessageSource message, optional NFCWriterOptions options);
} |
The name |
It is a reader, but it only reads a subset... If we get rid of the namespace and make these constructable (current recommendation) then we need to find good names for both. It might also name sense to name these more specialized like |
We have one reader and multiple filters. |
Sure, in hardware, but you can create as many instances as you want, same with the writer. I don't have better suggestions. A file reader is also only reading one file :-) We could of course call it an Observer. |
My interpretation of the name is that when the author wants to read from NFC tags they create an There's a similar pattern in other APIs, for example, you can create multiple |
From the outside, NFCReader and NFCWriter sound like good names to me, and the EventTarget "filter" pattern makes sense. I agree with @reillyeon that instances of a class don't need to correspond 1:1 with pieces of hardware. Thinking of a filter that outputs an EventTarget reminds me of patterns with Observables, which might be nice here from a perspective making an interface which works in Node.js as well, though WebNFC is a bit too tied to origins to make sense unchanged there. (I'm happy to see that the origin model is working out here!) |
I like the Observables pattern and |
I don't recall why we invented the watch, but it feels a bit like its own thing, instead of being aligned with the platform. We could go with a
startNotifications()
approach like WebBluetooth.This means that all of our values in the options dict needs to qualify as a separate handler (ie if you want to remove you need the same options dict and same function
The text was updated successfully, but these errors were encountered: