Skip to content

Commit

Permalink
yew-services fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jstarry committed Jan 23, 2021
1 parent 2f40822 commit 148296a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 158 deletions.
22 changes: 4 additions & 18 deletions packages/yew-services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,19 @@ authors = ["Hamza <[email protected]>"]
edition = "2018"

[dependencies]

anyhow = "1"
cfg-if = "1.0"
cfg-match = "0.2"
gloo = { version = "0.2.1", optional = true }
gloo = "0.2.1"
http = "0.2"
js-sys = { version = "0.3", optional = true }
js-sys = "0.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
stdweb = { version = "0.4.20", optional = true }
thiserror = "1"
yew = { path = "../yew" }
wasm-bindgen = { version = "0.2.60" }
wasm-bindgen-futures = { version = "0.4", optional = true }
wasm-bindgen = "0.2.60"
wasm-bindgen-futures = "0.4"

[dependencies.web-sys]
version = "0.3"
optional = true
features = [
"AbortController",
"AbortSignal",
Expand Down Expand Up @@ -50,21 +45,12 @@ features = [
"WorkerOptions",
]


[dev-dependencies]
wasm-bindgen-test = "0.3.4"
base64 = "0.13.0"
ssri = "6.0.0"

[features]
default = ["web_sys"]
web_sys = [
"gloo",
"js-sys",
"web-sys",
"wasm-bindgen-futures"
]
std_web = ["stdweb"]
wasm_test = []
httpbin_test = []
echo_server_test = []
171 changes: 46 additions & 125 deletions packages/yew-services/src/callback_test_util.rs
Original file line number Diff line number Diff line change
@@ -1,147 +1,68 @@
#![cfg(test)]

#[cfg(feature = "web_sys")]
pub use self::web_sys::*;

#[cfg(feature = "std_web")]
pub use self::std_web::*;

#[cfg(feature = "web_sys")]
mod web_sys {
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll, Waker};
use yew::callback::*;

struct CallbackHandle<T> {
waker: Option<Waker>,
output: Option<T>,
}

impl<T> Default for CallbackHandle<T> {
fn default() -> Self {
CallbackHandle {
waker: None,
output: None,
}
}
}

pub struct CallbackFuture<T>(Rc<RefCell<CallbackHandle<T>>>);

impl<T> Clone for CallbackFuture<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<T> Default for CallbackFuture<T> {
fn default() -> Self {
Self(Rc::default())
}
}

impl<T: 'static> Into<Callback<T>> for CallbackFuture<T> {
fn into(self) -> Callback<T> {
Callback::from(move |r| self.finish(r))
}
}

impl<T> Future for CallbackFuture<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(output) = self.ready() {
Poll::Ready(output)
} else {
let handle = &self.0;
handle.borrow_mut().waker = Some(cx.waker().clone());
Poll::Pending
}
}
}

impl<T> CallbackFuture<T> {
pub fn ready(&self) -> Option<T> {
self.0.borrow_mut().output.take()
}
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll, Waker};
use yew::callback::*;

struct CallbackHandle<T> {
waker: Option<Waker>,
output: Option<T>,
}

fn finish(&self, output: T) {
self.0.borrow_mut().output = Some(output);
if let Some(waker) = self.0.borrow_mut().waker.take() {
waker.wake();
}
impl<T> Default for CallbackHandle<T> {
fn default() -> Self {
CallbackHandle {
waker: None,
output: None,
}
}
}

#[cfg(feature = "std_web")]
mod std_web {
use std::cell::RefCell;
use std::future::Future;
use std::pin::Pin;
use std::rc::Rc;
use std::task::{Context, Poll, Waker};
use yew::callback::*;
pub struct CallbackFuture<T>(Rc<RefCell<CallbackHandle<T>>>);

struct CallbackHandle<T> {
waker: Option<Waker>,
output: Option<T>,
impl<T> Clone for CallbackFuture<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<T> Default for CallbackHandle<T> {
fn default() -> Self {
CallbackHandle {
waker: None,
output: None,
}
}
}

pub struct CallbackFuture<T>(Rc<RefCell<CallbackHandle<T>>>);

impl<T> Clone for CallbackFuture<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
impl<T> Default for CallbackFuture<T> {
fn default() -> Self {
Self(Rc::default())
}
}

impl<T> Default for CallbackFuture<T> {
fn default() -> Self {
Self(Rc::default())
}
impl<T: 'static> Into<Callback<T>> for CallbackFuture<T> {
fn into(self) -> Callback<T> {
Callback::from(move |r| self.finish(r))
}
}

impl<T: 'static> Into<Callback<T>> for CallbackFuture<T> {
fn into(self) -> Callback<T> {
Callback::from(move |r| self.finish(r))
impl<T> Future for CallbackFuture<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(output) = self.ready() {
Poll::Ready(output)
} else {
let handle = &self.0;
handle.borrow_mut().waker = Some(cx.waker().clone());
Poll::Pending
}
}
}

impl<T> Future for CallbackFuture<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
if let Some(output) = self.ready() {
Poll::Ready(output)
} else {
let handle = &self.0;
handle.borrow_mut().waker = Some(cx.waker().clone());
Poll::Pending
}
}
impl<T> CallbackFuture<T> {
pub fn ready(&self) -> Option<T> {
self.0.borrow_mut().output.take()
}

impl<T> CallbackFuture<T> {
pub fn ready(&self) -> Option<T> {
self.0.borrow_mut().output.take()
}

fn finish(&self, output: T) {
self.0.borrow_mut().output = Some(output);
if let Some(waker) = self.0.borrow_mut().waker.take() {
waker.wake();
}
fn finish(&self, output: T) {
self.0.borrow_mut().output = Some(output);
if let Some(waker) = self.0.borrow_mut().waker.take() {
waker.wake();
}
}
}
29 changes: 14 additions & 15 deletions packages/yew-services/src/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,9 @@ impl FetchService {
/// following examples:
///
/// ```
///# use yew::format::{Nothing, Json};
///# use yew::services::fetch::Request;
///# use serde_json::json;
///# use yew::format::{Nothing, Json};
///# use yew_services::fetch::Request;
/// let post_request = Request::post("https://my.api/v1/resource")
/// .header("Content-Type", "application/json")
/// .body(Json(&json!({"foo": "bar"})))
Expand All @@ -186,10 +186,10 @@ impl FetchService {
/// response body and metadata.
///
/// ```
///# use yew::{Component, ComponentLink, Html};
///# use yew::services::FetchService;
///# use yew::services::fetch::{Response, Request};
///# use anyhow::Error;
///# use yew::{Component, ComponentLink, Html};
///# use yew_services::FetchService;
///# use yew_services::fetch::{Response, Request};
///# struct Comp;
///# impl Component for Comp {
///# type Message = Msg;type Properties = ();
Expand Down Expand Up @@ -223,13 +223,13 @@ impl FetchService {
/// data type, then you will get an error message.
///
/// ```
///# use yew::format::{Json, Nothing, Format};
///# use yew::services::FetchService;
///# use anyhow::Error;
///# use http::Request;
///# use yew::services::fetch::Response;
///# use serde::Deserialize;
///# use yew::{Component, ComponentLink, Html};
///# use serde_derive::Deserialize;
///# use anyhow::Error;
///# use yew::format::{Json, Nothing, Format};
///# use yew_services::fetch::Response;
///# use yew_services::FetchService;
///# struct Comp;
///# impl Component for Comp {
///# type Message = Msg;type Properties = ();
Expand Down Expand Up @@ -277,12 +277,11 @@ impl FetchService {
/// `fetch` with provided `FetchOptions` object.
/// Use it if you need to send cookies with a request:
/// ```
///# use anyhow::Error;
///# use http::Response;
///# use yew::format::Nothing;
///# use yew::services::fetch::{self, FetchOptions, Credentials};
///# use yew::{Html, Component, ComponentLink};
///# use yew::services::FetchService;
///# use http::Response;
///# use anyhow::Error;
///# use yew_services::fetch::{self, FetchOptions, FetchService Credentials};
///# struct Comp;
///# impl Component for Comp {
///# type Message = Msg;
Expand Down Expand Up @@ -586,11 +585,11 @@ pub enum Referrer {
mod tests {
use super::*;
use crate::callback_test_util::CallbackFuture;
use web_sys::ReferrerPolicy;
use serde::Deserialize;
use ssri::Integrity;
use std::collections::HashMap;
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
use web_sys::ReferrerPolicy;
use yew::callback::Callback;
use yew::format::{Json, Nothing};
use yew::utils;
Expand Down

0 comments on commit 148296a

Please sign in to comment.