Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate Yew Platform #2893

Merged
merged 10 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions packages/yew-platform/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[package]
name = "yew-platform"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we dropped the yew- prefix from here? It can be generic crate that can be used to pick an async runtime between tokio and wasm-bindgen-futures (JS promises)

Name idea: promise + tokio = prokio

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good. I have published the crate under name prokio.

version = "0.1.0"
authors = ["Kaede Hoshikawa <futursolo@icloud.com>"]
edition = "2021"
license = "MIT OR Apache-2.0"
readme = "README.md"
keywords = ["yew", "futures", "async", "io"]
categories = ["asynchronous"]
description = "Yew's asynchronous runtime."
repository = "https://github.com/yewstack/yew"
rust-version = "1.60.0"

[dependencies]
gloo = "0.8"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
pin-project = "1.0.11"
pinned = "0.1.0"

[target.'cfg(target_arch = "wasm32")'.dependencies]
# we move wasm-bindgen here so no promise-based spawn_local can present for
# non-wasm32 targets.
wasm-bindgen-futures = "0.4"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
num_cpus = { version = "1.13", optional = true }
once_cell = "1"
tokio = { version = "1.21.1", features = ["rt", "time"], optional = true }
tokio-stream = { version = "0.1", features = ["time"], optional = true }

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.19", features = ["full"] }

[features]
tokio = ["dep:tokio", "dep:num_cpus", "dep:tokio-stream"]
default = []

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "documenting"]
3 changes: 3 additions & 0 deletions packages/yew-platform/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# yew-platform

Runtime for the [Yew](https://github.com/yewstack/yew) framework.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ impl Write for Inner {
/// An asynchronous [`String`] writer.
///
/// This type implements [`fmt::Write`] and can be used with [`write!`] and [`writeln!`].
pub(crate) struct BufWriter {
#[derive(Debug)]
pub struct BufWriter {
inner: Rc<UnsafeCell<Inner>>,
}

Expand Down Expand Up @@ -145,7 +146,8 @@ impl Drop for BufWriter {
}

/// An asynchronous [`String`] reader.
pub(crate) struct BufReader {
#[derive(Debug)]
pub struct BufReader {
inner: Rc<UnsafeCell<Inner>>,
}

Expand Down Expand Up @@ -198,7 +200,7 @@ impl FusedStream for BufReader {
}

/// Creates an asynchronous buffer that operates over String.
pub(crate) fn buffer() -> (BufWriter, BufReader) {
pub fn buffer() -> (BufWriter, BufReader) {
let inner = Rc::new(UnsafeCell::new(Inner::new()));

let w = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ use pin_project::pin_project;

mod buffer;

pub(crate) use buffer::{buffer, BufReader, BufWriter};
pub use buffer::{buffer, BufReader, BufWriter};

/// A buffered asynchronous [`String`] [`Stream`].
///
/// A BufStream combines a BufWriter - BufReader pair and a resolving future that writes to the
/// buffer and polls the future alongside the buffer.
#[derive(Debug)]
#[pin_project]
pub(crate) struct BufStream<F>
pub struct BufStream<F>
where
F: Future<Output = ()>,
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Compatibility between JavaScript Runtime and Native Runtimes.
//! Yew's compatibility between JavaScript Runtime and Native Runtimes.
//!
//! When designing components and libraries that works on both WebAssembly targets backed by
//! JavaScript Runtime and non-WebAssembly targets with Native Runtimes. Developers usually face
Expand Down Expand Up @@ -40,13 +40,21 @@
//! transparent to the future that executes the renderer. The Yew application still needs to use
//! `tokio`'s timer, IO and task synchronisation primitives.

#![cfg_attr(documenting, feature(doc_cfg))]
#![cfg_attr(documenting, feature(doc_auto_cfg))]
#![deny(
missing_docs,
missing_debug_implementations,
bare_trait_objects,
anonymous_parameters,
elided_lifetimes_in_paths
)]

use std::future::Future;
use std::io::Result;
use std::marker::PhantomData;

#[cfg(feature = "ssr")]
pub(crate) mod fmt;

pub mod fmt;
pub mod pinned;
pub mod time;

Expand Down
14 changes: 14 additions & 0 deletions packages/yew-platform/src/pinned.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//! Task synchronisation primitives for pinned tasks.
//!
//! This module provides the following task synchronisation mechanisms for `!Send` futures:
futursolo marked this conversation as resolved.
Show resolved Hide resolved
//!
//! - [`Barrier`]: Ensures multiple tasks to wait until all tasks have reached a point in the
//! program before continuing execution of all together.
//! - [`RwLock`]: Provides a mutual exclusion mechanism which allows multiple readers at the same
//! time, while allowing only one writer at a time.
//! - [`mpsc`]: A channel that supports sending multiple values from multiple producers to a single
//! receiver.
//! - [`oneshot`]: A channel to send one single value from a producer to a receiver.

#[doc(inline)]
pub use pinned::*;
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ mod tests {
use futures::channel::oneshot;
use tokio::test;
use tokio::time::timeout;
use yew::platform::Runtime;

use super::*;
use crate::Runtime;

#[test]
async fn test_local_handle_exists() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;

use futures::stream::Stream;

use crate::platform::imp::time as imp;
use crate::imp::time as imp;

/// Waits until duration has elapsed.
#[inline(always)]
Expand Down
26 changes: 9 additions & 17 deletions packages/yew/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,18 @@ slab = "0.4"
wasm-bindgen = "0.2"
yew-macro = { version = "^0.19.0", path = "../yew-macro" }
thiserror = "1.0"
futures = { version = "0.3", default-features = false, features = ["std", "async-await"] }
futures = { version = "0.3", default-features = false, features = ["std"] }
html-escape = { version = "0.2.9", optional = true }
implicit-clone = { version = "0.3", features = ["map"] }
base64ct = { version = "1.5.0", features = ["std"], optional = true }
bincode = { version = "1.3.3", optional = true }
serde = { version = "1", features = ["derive"] }
tracing = "0.1.36"
pin-project = "1.0.11"
yew-platform = { version = "0.1.0", path = "../yew-platform" }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# We still need tokio as we have docs linked to it.
tokio = { version = "1.19", features = ["rt"] }

[dependencies.web-sys]
version = "^0.3.59"
Expand Down Expand Up @@ -69,21 +73,12 @@ features = [
"SubmitEvent"
]

[target.'cfg(target_arch = "wasm32")'.dependencies]
# we move it here so no promise-based spawn_local can present for
# non-wasm32 targets.
wasm-bindgen-futures = "0.4"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
num_cpus = { version = "1.13", optional = true }
once_cell = "1"
tokio = { version = "1.21.1", features = ["rt", "time"], optional = true }
tokio-stream = { version = "0.1", features = ["time"], optional = true }
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.19", features = ["full"] }

[dev-dependencies]
wasm-bindgen-test = "0.3"
gloo = { version = "0.8", features = ["futures"] }
wasm-bindgen-futures = "0.4"
rustversion = "1"
trybuild = "1"

Expand All @@ -95,15 +90,12 @@ features = [
]

[features]
tokio = ["dep:tokio", "dep:num_cpus", "dep:tokio-stream"]
tokio = ["yew-platform/tokio"]
ssr = ["dep:html-escape", "dep:base64ct", "dep:bincode"]
csr = []
hydration = ["csr", "dep:bincode"]
default = []

[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.19", features = ["full"] }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "documenting"]
3 changes: 2 additions & 1 deletion packages/yew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ pub mod context;
mod dom_bundle;
pub mod functional;
pub mod html;
pub mod platform;
#[doc(inline)]
pub use yew_platform as platform;
pub mod scheduler;
mod sealed;
#[cfg(feature = "ssr")]
Expand Down
6 changes: 0 additions & 6 deletions packages/yew/src/platform/pinned/mod.rs

This file was deleted.

Loading