Skip to content

Commit

Permalink
Editorial: fix readable/writable file stream examples
Browse files Browse the repository at this point in the history
* Use `fileHandle.close()` to close a FileHandle
* Pass a view to `fileHandle.read()`, since it doesn't accept an ArrayBuffer
* Pass `position` to `fileHandle.read()` instead of relying on implicit file positioning
  • Loading branch information
dubiousjim authored Nov 29, 2021
1 parent 5885d94 commit 44190d1
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions index.bs
Original file line number Diff line number Diff line change
Expand Up @@ -7302,15 +7302,15 @@ function makeReadableFileStream(filename) {
},

async pull(controller) {
const buffer = new ArrayBuffer(CHUNK_SIZE);
const buffer = new Uint8Array(CHUNK_SIZE);

const { bytesRead } = await fileHandle.read(buffer, 0, CHUNK_SIZE, position);
if (bytesRead === 0) {
await fileHandle.close();
controller.close();
} else {
position += bytesRead;
controller.enqueue(new Uint8Array(buffer, 0, bytesRead));
controller.enqueue(buffer.subarray(0, bytesRead));
}
},

Expand Down Expand Up @@ -7350,7 +7350,7 @@ function makeReadableByteFileStream(filename) {
// feature allocates a buffer and passes it to us via byobRequest.
const v = controller.byobRequest.view;

const { bytesRead } = await fileHandle.read(v.buffer, v.byteOffset, v.byteLength);
const { bytesRead } = await fileHandle.read(v, 0, v.byteLength, position);
if (bytesRead === 0) {
await fileHandle.close();
controller.close();
Expand All @@ -7362,7 +7362,7 @@ function makeReadableByteFileStream(filename) {
},

cancel() {
return fs.close(fd);
return fileHandle.close();
},

autoAllocateChunkSize: DEFAULT_CHUNK_SIZE
Expand Down Expand Up @@ -7470,11 +7470,11 @@ function makeWritableFileStream(filename) {
},

close() {
return fs.close(fd);
return fileHandle.close();
},

abort() {
return fs.close(fd);
return fileHandle.close();
}
});
}
Expand Down Expand Up @@ -7834,6 +7834,7 @@ Isaac Schlueter,
isonmad,
Jake Archibald,
Jake Verbaten,
James Pryor,
Janessa Det,
Jason Orendorff,
Jeffrey Yasskin,
Expand Down

0 comments on commit 44190d1

Please sign in to comment.