-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Don't take self in close functions. It's a pain when Drop is used. * Fix some WASM stuff. * Some WASM improvements. * WIP * Some fixes to the read_buf API * Oops advance_mut * Remove some less useful changes. * Not needed either. * Clippy
- Loading branch information
Showing
11 changed files
with
286 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,61 @@ | ||
use std::{error, fmt}; | ||
use wasm_bindgen::prelude::*; | ||
|
||
use wasm_bindgen::JsValue; | ||
#[derive(Clone, Debug, thiserror::Error)] | ||
#[error("web error: {0:?}")] | ||
pub struct WebError(js_sys::Error); | ||
|
||
#[derive(Debug)] | ||
pub struct WebError { | ||
value: JsValue, | ||
impl From<js_sys::Error> for WebError { | ||
fn from(e: js_sys::Error) -> Self { | ||
Self(e) | ||
} | ||
} | ||
|
||
impl From<JsValue> for WebError { | ||
fn from(value: JsValue) -> Self { | ||
Self { value } | ||
impl From<wasm_bindgen::JsValue> for WebError { | ||
fn from(e: wasm_bindgen::JsValue) -> Self { | ||
Self(e.into()) | ||
} | ||
} | ||
|
||
impl error::Error for WebError {} | ||
pub trait WebErrorExt<T> { | ||
fn throw(self) -> Result<T, WebError>; | ||
} | ||
|
||
impl fmt::Display for WebError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
// Print out the JsValue as a string | ||
match self.value.as_string() { | ||
Some(s) => write!(f, "{}", s), | ||
None => write!(f, "{:?}", self.value), | ||
} | ||
impl<T, E: Into<WebError>> WebErrorExt<T> for Result<T, E> { | ||
fn throw(self) -> Result<T, WebError> { | ||
self.map_err(Into::into) | ||
} | ||
} | ||
|
||
impl From<&str> for WebError { | ||
fn from(value: &str) -> Self { | ||
Self { | ||
value: value.into(), | ||
} | ||
#[derive(Clone, Debug, thiserror::Error)] | ||
#[error("read error: {0:?}")] | ||
pub struct ReadError(#[from] WebError); | ||
|
||
#[derive(Clone, Debug, thiserror::Error)] | ||
#[error("write error: {0:?}")] | ||
pub struct WriteError(#[from] WebError); | ||
|
||
#[derive(Clone, Debug, thiserror::Error)] | ||
pub enum SessionError { | ||
// TODO distinguish between different kinds of errors | ||
#[error("read error: {0}")] | ||
Read(#[from] ReadError), | ||
|
||
#[error("write error: {0}")] | ||
Write(#[from] WriteError), | ||
|
||
#[error("web error: {0}")] | ||
Web(#[from] WebError), | ||
} | ||
|
||
pub(crate) trait PromiseExt { | ||
fn ignore(self); | ||
} | ||
|
||
impl PromiseExt for js_sys::Promise { | ||
// Ignore the result of the promise by using an empty catch. | ||
fn ignore(self) { | ||
let closure = Closure::wrap(Box::new(|_: JsValue| {}) as Box<dyn FnMut(JsValue)>); | ||
let _ = self.catch(&closure); | ||
closure.forget(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,45 @@ | ||
use js_sys::Reflect; | ||
use wasm_bindgen::{JsCast, JsValue}; | ||
use wasm_bindgen::prelude::*; | ||
use wasm_bindgen_futures::JsFuture; | ||
use web_sys::{ReadableStream, ReadableStreamDefaultReader, ReadableStreamReadResult}; | ||
|
||
use crate::WebError; | ||
use crate::{PromiseExt, ReadError, WebErrorExt}; | ||
|
||
// Wrapper around ReadableStream | ||
pub struct Reader { | ||
inner: ReadableStreamDefaultReader, | ||
} | ||
|
||
impl Reader { | ||
pub fn new(stream: &ReadableStream) -> Result<Self, WebError> { | ||
pub fn new(stream: &ReadableStream) -> Result<Self, ReadError> { | ||
let inner = stream.get_reader().unchecked_into(); | ||
Ok(Self { inner }) | ||
} | ||
|
||
pub async fn read<T: JsCast>(&mut self) -> Result<Option<T>, WebError> { | ||
let result: ReadableStreamReadResult = JsFuture::from(self.inner.read()).await?.into(); | ||
pub async fn read<T: JsCast>(&mut self) -> Result<Option<T>, ReadError> { | ||
let result: ReadableStreamReadResult = | ||
JsFuture::from(self.inner.read()).await.throw()?.into(); | ||
|
||
if Reflect::get(&result, &"done".into())?.is_truthy() { | ||
if Reflect::get(&result, &"done".into()).throw()?.is_truthy() { | ||
return Ok(None); | ||
} | ||
|
||
let res = Reflect::get(&result, &"value".into())?.dyn_into()?; | ||
let res = Reflect::get(&result, &"value".into()) | ||
.throw()? | ||
.unchecked_into(); | ||
|
||
Ok(Some(res)) | ||
} | ||
|
||
pub fn close(self, reason: &str) { | ||
pub fn close(&mut self, reason: &str) { | ||
let str = JsValue::from_str(reason); | ||
let _ = self.inner.cancel_with_reason(&str); // ignore the promise | ||
self.inner.cancel_with_reason(&str).ignore(); | ||
} | ||
} | ||
|
||
impl Drop for Reader { | ||
fn drop(&mut self) { | ||
let _ = self.inner.cancel(); // ignore the promise | ||
self.inner.cancel().ignore(); | ||
self.inner.release_lock(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.