-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
158 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", | ||
|
@@ -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 = [] |
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,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(); | ||
} | ||
} | ||
} |
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