Skip to content
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

Join NDEFReader and NDEFWriter #601

Merged
merged 13 commits into from
Nov 20, 2020
45 changes: 21 additions & 24 deletions EXPLAINER.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Authors:
- Francois Beaufort

Participate
- https://github.com/w3c/web-nfc/issues/
- https://github.com/w3c/web-nfc/issues/
- https://lists.w3.org/Archives/Public/public-web-nfc/

## Introduction
Expand Down Expand Up @@ -92,17 +92,17 @@ when required, about granting access to Web NFC. It means sending and receiving
info when users tap NFC devices can be done smoothly once permission is granted.

```js
const reader = new NDEFReader();
const ndef = new NDEFReader();

async function startScanning() {
await reader.scan();
reader.onreading = event => {
await ndef.scan();
ndef.onreading = event => {
/* handle NDEF messages */
};
}

const nfcPermissionStatus = await navigator.permissions.query({ name: "nfc" });
if (permissionStatus.state === "granted") {
if (nfcPermissionStatus.state === "granted") {
// NFC access was previously granted, so we can start NFC scanning now.
startScanning();
} else {
Expand All @@ -123,13 +123,13 @@ USB](https://wicg.github.io/webusb/) in that it offers secure low level access
with no magic and instead exposes data as a DataView as well as requires text to
be encoded and decoded using TextEncoder/TextDecoder, which allows handling
cases where the text might even be in UTF-16 do to existing real life
implementations.
implementations.

```js
const reader = new NDEFReader();
const ndef = new NDEFReader();

await reader.scan({ recordType: "example.com:smart-poster" });
reader.onreading = event => {
await ndef.scan();
ndef.onreading = event => {
const externalRecord = event.message.records.find(
record => record.type == "example.com:smart-poster"
);
Expand Down Expand Up @@ -162,13 +162,12 @@ const abortController = new AbortController();
abortController.signal.onabort = event => {
// All NFC operations have been aborted.
};

const reader = new NDEFReader();
await reader.scan({ signal: abortController.signal });

const writer = new NDEFWriter();
await writer.push("foo", { signal: abortController.signal });


const ndef = new NDEFReader();
await ndef.scan({ signal: abortController.signal });

await ndef.write("foo", { signal: abortController.signal });

document.querySelector("#abortButton").onclick = event => {
abortController.abort();
};
Expand Down Expand Up @@ -228,13 +227,11 @@ same API, and have it loaded as a separate module. People working on Web
Assembly are also advocating for this patterns as it might be able to turn such
globals into modules in the future, at least when accessed from WASM.

### Separate objects for reader/writer
### Common vs separate objects for reader/writer

The reader and writer objects existed separately when scan filters were supported,
but after filters have been abandoned, they have been merged into one single object.

The reader and writer objects could have been merged into one single object, but
as we allow multiple scans with filters to be active at the same time (maybe in
multiple places/view of the app/site) then it makes more sense to be able to use
separate objects.

## Considered alternatives
beaufortfrancois marked this conversation as resolved.
Show resolved Hide resolved

### Restrict to NDEF and name accordingly
Expand Down
Loading