Skip to content

Commit 40d402d

Browse files
authored
Merge pull request #601 from zolkis/merge-reader-writer
Join NDEFReader and NDEFWriter
2 parents 00ddf47 + e82d4b2 commit 40d402d

File tree

3 files changed

+150
-211
lines changed

3 files changed

+150
-211
lines changed

EXPLAINER.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Authors:
66
- Francois Beaufort
77

88
Participate
9-
- https://github.com/w3c/web-nfc/issues/
9+
- https://github.com/w3c/web-nfc/issues/
1010
- https://lists.w3.org/Archives/Public/public-web-nfc/
1111

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

9494
```js
95-
const reader = new NDEFReader();
96-
95+
const ndef = new NDEFReader();
96+
9797
async function startScanning() {
98-
await reader.scan();
99-
reader.onreading = event => {
98+
await ndef.scan();
99+
ndef.onreading = event => {
100100
/* handle NDEF messages */
101101
};
102102
}
103-
103+
104104
const nfcPermissionStatus = await navigator.permissions.query({ name: "nfc" });
105-
if (permissionStatus.state === "granted") {
105+
if (nfcPermissionStatus.state === "granted") {
106106
// NFC access was previously granted, so we can start NFC scanning now.
107107
startScanning();
108108
} else {
@@ -123,13 +123,13 @@ USB](https://wicg.github.io/webusb/) in that it offers secure low level access
123123
with no magic and instead exposes data as a DataView as well as requires text to
124124
be encoded and decoded using TextEncoder/TextDecoder, which allows handling
125125
cases where the text might even be in UTF-16 do to existing real life
126-
implementations.
126+
implementations.
127127

128128
```js
129-
const reader = new NDEFReader();
129+
const ndef = new NDEFReader();
130130

131-
await reader.scan({ recordType: "example.com:smart-poster" });
132-
reader.onreading = event => {
131+
await ndef.scan();
132+
ndef.onreading = event => {
133133
const externalRecord = event.message.records.find(
134134
record => record.type == "example.com:smart-poster"
135135
);
@@ -162,13 +162,12 @@ const abortController = new AbortController();
162162
abortController.signal.onabort = event => {
163163
// All NFC operations have been aborted.
164164
};
165-
166-
const reader = new NDEFReader();
167-
await reader.scan({ signal: abortController.signal });
168-
169-
const writer = new NDEFWriter();
170-
await writer.push("foo", { signal: abortController.signal });
171-
165+
166+
const ndef = new NDEFReader();
167+
await ndef.scan({ signal: abortController.signal });
168+
169+
await ndef.write("foo", { signal: abortController.signal });
170+
172171
document.querySelector("#abortButton").onclick = event => {
173172
abortController.abort();
174173
};
@@ -228,13 +227,11 @@ same API, and have it loaded as a separate module. People working on Web
228227
Assembly are also advocating for this patterns as it might be able to turn such
229228
globals into modules in the future, at least when accessed from WASM.
230229

231-
### Separate objects for reader/writer
230+
### Common vs separate objects for reader/writer
231+
232+
The reader and writer objects existed separately when scan filters were supported,
233+
but after filters have been abandoned, they have been merged into one single object.
232234

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

240237
### Restrict to NDEF and name accordingly

0 commit comments

Comments
 (0)