Skip to content

Commit

Permalink
Merge pull request #16 from MattiasBuelens/no-respond-on-cancel
Browse files Browse the repository at this point in the history
Stop calling byobRequest.respond(0) on cancel
  • Loading branch information
MattiasBuelens committed Nov 8, 2022
2 parents da32c9f + 18e48a3 commit c0b3ee7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 49 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased

* Stop calling `byobRequest.respond(0)` on cancel ([#16](https://github.com/MattiasBuelens/wasm-streams/pull/16))

## v0.3.0 (2022-10-16)

* Added support for web workers, by removing usage of [JavaScript snippets](https://rustwasm.github.io/docs/wasm-bindgen/reference/js-snippets.html). ([#13](https://github.com/MattiasBuelens/wasm-streams/issues/13), [#14](https://github.com/MattiasBuelens/wasm-streams/pull/14))
Expand Down
40 changes: 5 additions & 35 deletions src/readable/into_underlying_byte_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ impl Drop for IntoUnderlyingByteSource {
if let Some(handle) = self.pull_handle.take() {
handle.abort();
}
// Close the pending BYOB request, if any. This is necessary for cancellation.
if let Some(request) = self.controller.take().and_then(|c| c.byob_request()) {
request.respond(0);
}
}
}

Expand All @@ -106,9 +102,9 @@ impl Inner {
// after the stream has closed or encountered an error.
let async_read = self.async_read.as_mut().unwrap_throw();
// We set autoAllocateChunkSize, so there should always be a BYOB request.
let mut request = ByobRequestGuard::new(controller.byob_request().unwrap_throw());
let request = controller.byob_request().unwrap_throw();
// Resize the buffer to fit the BYOB request.
let request_view = request.view();
let request_view = request.view().unwrap_throw();
let request_len = clamp_to_usize(request_view.byte_length());
if self.buffer.len() < request_len {
self.buffer.resize(request_len, 0);
Expand All @@ -117,8 +113,8 @@ impl Inner {
Ok(0) => {
// The stream has closed, drop it.
self.discard();
controller.close();
request.respond(0);
controller.close()?;
request.respond(0)?;
}
Ok(bytes_read) => {
// Copy read bytes from buffer to BYOB request view
Expand All @@ -131,7 +127,7 @@ impl Inner {
);
dest.copy_from(&self.buffer[0..bytes_read]);
// Respond to BYOB request
request.respond(bytes_read_u32);
request.respond(bytes_read_u32)?;
}
Err(err) => {
// The stream encountered an error, drop it.
Expand All @@ -148,29 +144,3 @@ impl Inner {
self.buffer = Vec::new();
}
}

#[derive(Debug)]
struct ByobRequestGuard(Option<sys::ReadableStreamBYOBRequest>);

impl ByobRequestGuard {
fn new(request: sys::ReadableStreamBYOBRequest) -> Self {
Self(Some(request))
}

fn view(&mut self) -> sys::ArrayBufferView {
self.0.as_mut().unwrap_throw().view().unwrap_throw()
}

fn respond(mut self, bytes_read: u32) {
self.0.take().unwrap_throw().respond(bytes_read);
}
}

impl Drop for ByobRequestGuard {
fn drop(&mut self) {
// Close the BYOB request, if still pending. This is necessary for cancellation.
if let Some(request) = self.0.take() {
request.respond(0);
}
}
}
4 changes: 2 additions & 2 deletions src/readable/into_underlying_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,11 @@ impl Inner {
// after the stream has closed or encountered an error.
let stream = self.stream.as_mut().unwrap_throw();
match stream.try_next().await {
Ok(Some(chunk)) => controller.enqueue(&chunk),
Ok(Some(chunk)) => controller.enqueue(&chunk)?,
Ok(None) => {
// The stream has closed, drop it.
self.stream = None;
controller.close();
controller.close()?;
}
Err(err) => {
// The stream encountered an error, drop it.
Expand Down
30 changes: 18 additions & 12 deletions src/readable/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ extern "C" {
#[wasm_bindgen(method, getter, js_name = desiredSize)]
pub fn desired_size(this: &ReadableStreamDefaultController) -> Option<f64>;

#[wasm_bindgen(method, js_name = close)]
pub fn close(this: &ReadableStreamDefaultController);
#[wasm_bindgen(method, catch, js_name = close)]
pub fn close(this: &ReadableStreamDefaultController) -> Result<(), JsValue>;

#[wasm_bindgen(method, js_name = enqueue)]
pub fn enqueue(this: &ReadableStreamDefaultController, chunk: &JsValue);
#[wasm_bindgen(method, catch, js_name = enqueue)]
pub fn enqueue(this: &ReadableStreamDefaultController, chunk: &JsValue) -> Result<(), JsValue>;

#[wasm_bindgen(method, js_name = error)]
pub fn error(this: &ReadableStreamDefaultController, error: &JsValue);
Expand All @@ -94,11 +94,14 @@ extern "C" {
#[wasm_bindgen(method, getter, js_name = desiredSize)]
pub fn desired_size(this: &ReadableByteStreamController) -> Option<f64>;

#[wasm_bindgen(method, js_name = close)]
pub fn close(this: &ReadableByteStreamController);
#[wasm_bindgen(method, catch, js_name = close)]
pub fn close(this: &ReadableByteStreamController) -> Result<(), JsValue>;

#[wasm_bindgen(method, js_name = enqueue)]
pub fn enqueue(this: &ReadableByteStreamController, chunk: &ArrayBufferView);
#[wasm_bindgen(method, catch, js_name = enqueue)]
pub fn enqueue(
this: &ReadableByteStreamController,
chunk: &ArrayBufferView,
) -> Result<(), JsValue>;

#[wasm_bindgen(method, js_name = error)]
pub fn error(this: &ReadableByteStreamController, error: &JsValue);
Expand All @@ -113,11 +116,14 @@ extern "C" {
#[wasm_bindgen(method, getter, js_name = view)]
pub fn view(this: &ReadableStreamBYOBRequest) -> Option<ArrayBufferView>;

#[wasm_bindgen(method, js_name = respond)]
pub fn respond(this: &ReadableStreamBYOBRequest, bytes_written: u32);
#[wasm_bindgen(method, catch, js_name = respond)]
pub fn respond(this: &ReadableStreamBYOBRequest, bytes_written: u32) -> Result<(), JsValue>;

#[wasm_bindgen(method, js_name = respondWithNewView)]
pub fn respond_with_new_view(this: &ReadableStreamBYOBRequest, view: &ArrayBufferView);
#[wasm_bindgen(method, catch, js_name = respondWithNewView)]
pub fn respond_with_new_view(
this: &ReadableStreamBYOBRequest,
view: &ArrayBufferView,
) -> Result<(), JsValue>;
}

#[wasm_bindgen]
Expand Down

0 comments on commit c0b3ee7

Please sign in to comment.