Skip to content

Commit

Permalink
Separate Yew Platform (#2893)
Browse files Browse the repository at this point in the history
* Remove pinned module.

* Create Yew Platform.

* Remove some additional dependencies.

* Fix clippy.

* Restore wasm-bindgen-futures.

* Fix docs.

* Migrate to prokio.

* Fix docs warnings.

* Fix dependencies.
  • Loading branch information
futursolo authored Oct 20, 2022
1 parent 71408e2 commit da09755
Show file tree
Hide file tree
Showing 29 changed files with 86 additions and 1,796 deletions.
12 changes: 0 additions & 12 deletions ci/feature-soundness-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,3 @@ cargo clippy --release --no-default-features --features default,ssr -- --deny=wa
cargo clippy --release --no-default-features --features csr,default,ssr -- --deny=warnings
cargo clippy --release --no-default-features --features hydration,ssr -- --deny=warnings
cargo clippy --release --no-default-features --features default,hydration,ssr -- --deny=warnings
cargo clippy --release --no-default-features --features tokio -- --deny=warnings
cargo clippy --release --no-default-features --features csr,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features default,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features csr,default,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features hydration,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features default,hydration,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features ssr,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features csr,ssr,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features default,ssr,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features csr,default,ssr,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features hydration,ssr,tokio -- --deny=warnings
cargo clippy --release --no-default-features --features default,hydration,ssr,tokio -- --deny=warnings
12 changes: 0 additions & 12 deletions ci/feature-soundness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,3 @@ cargo clippy --no-default-features --features default,ssr -- --deny=warnings
cargo clippy --no-default-features --features csr,default,ssr -- --deny=warnings
cargo clippy --no-default-features --features hydration,ssr -- --deny=warnings
cargo clippy --no-default-features --features default,hydration,ssr -- --deny=warnings
cargo clippy --no-default-features --features tokio -- --deny=warnings
cargo clippy --no-default-features --features csr,tokio -- --deny=warnings
cargo clippy --no-default-features --features default,tokio -- --deny=warnings
cargo clippy --no-default-features --features csr,default,tokio -- --deny=warnings
cargo clippy --no-default-features --features hydration,tokio -- --deny=warnings
cargo clippy --no-default-features --features default,hydration,tokio -- --deny=warnings
cargo clippy --no-default-features --features ssr,tokio -- --deny=warnings
cargo clippy --no-default-features --features csr,ssr,tokio -- --deny=warnings
cargo clippy --no-default-features --features default,ssr,tokio -- --deny=warnings
cargo clippy --no-default-features --features csr,default,ssr,tokio -- --deny=warnings
cargo clippy --no-default-features --features hydration,ssr,tokio -- --deny=warnings
cargo clippy --no-default-features --features default,hydration,ssr,tokio -- --deny=warnings
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.

2 changes: 1 addition & 1 deletion examples/futures/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT OR Apache-2.0"
pulldown-cmark = { version = "0.9", default-features = false }
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
yew = { path = "../../packages/yew", features = ["tokio", "csr"] }
yew = { path = "../../packages/yew", features = ["csr"] }
gloo = "0.8"

[dependencies.web-sys]
Expand Down
2 changes: 1 addition & 1 deletion examples/js_callback/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT OR Apache-2.0"

[dependencies]
wasm-bindgen = "0.2"
yew = { path = "../../packages/yew", features = ["csr", "tokio"] }
yew = { path = "../../packages/yew", features = ["csr"] }
wasm-bindgen-futures = "0.4"
js-sys = "0.3"
once_cell = "1"
2 changes: 1 addition & 1 deletion examples/simple_ssr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ clap = { version = "3.1.7", features = ["derive"] }

[features]
hydration = ["yew/hydration"]
ssr = ["yew/ssr", "yew/tokio"]
ssr = ["yew/ssr"]
2 changes: 1 addition & 1 deletion examples/ssr_router/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ hyper = { version = "0.14", features = ["server", "http1"] }
jemallocator = "0.5"

[features]
ssr = ["yew/ssr", "yew/tokio"]
ssr = ["yew/ssr"]
hydration = ["yew/hydration"]
27 changes: 11 additions & 16 deletions packages/yew/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,23 @@ 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"
prokio = "0.1.0"
rustversion = "1"

[target.'cfg(target_arch = "wasm32")'.dependencies]
wasm-bindgen-futures = "0.4"

[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"
features = [
Expand Down Expand Up @@ -70,16 +77,8 @@ 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"
Expand All @@ -95,15 +94,11 @@ features = [
]

[features]
tokio = ["dep:tokio", "dep:num_cpus", "dep:tokio-stream"]
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"]
2 changes: 1 addition & 1 deletion packages/yew/Makefile.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tasks.native-test]
command = "cargo"
args = ["test", "--features", "csr,ssr,hydration,tokio"]
args = ["test", "--features", "csr,ssr,hydration"]

[tasks.wasm-test]
command = "wasm-pack"
Expand Down
6 changes: 3 additions & 3 deletions packages/yew/src/html/component/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ impl<COMP: BaseComponent> Scope<COMP> {
///
/// This method will not notify the component when the stream has been fully exhausted. If
/// you want this feature, you can add an EOF message variant for your component and use
/// [`StreamExt::chain`] and [`stream::once`] to chain an EOF message to the original stream.
/// If your stream is produced by another crate, you can use [`StreamExt::map`] to transform
/// the stream's item type to the component message type.
/// [`StreamExt::chain`] and [`stream::once`](futures::stream::once) to chain an EOF message to
/// the original stream. If your stream is produced by another crate, you can use
/// [`StreamExt::map`] to transform the stream's item type to the component message type.
pub fn send_stream<S, M>(&self, stream: S)
where
M: Into<COMP::Message>,
Expand Down
1 change: 0 additions & 1 deletion packages/yew/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
//! - `csr`: Enables Client-side Rendering support and [`Renderer`]. Only enable this feature if you
//! are making a Yew application (not a library).
//! - `ssr`: Enables Server-side Rendering support and [`ServerRenderer`].
//! - `tokio`: Enables future-based APIs on non-wasm32 targets with tokio runtime.
//! - `hydration`: Enables Hydration support.
//!
//! ## Example
Expand Down
48 changes: 48 additions & 0 deletions packages/yew/src/platform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//! Yew's compatibility between JavaScript Runtime and Native Runtimes.
//!
//! This module is also published under the name [prokio] on crates.io.
//!
//! # Rationale
//!
//! 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
//! challenges that requires applying multiple feature flags throughout their application:
//!
//! 1. Select I/O and timers that works with the target runtime.
//! 2. Native Runtimes usually require `Send` futures and WebAssembly types are usually `!Send`.
//!
//! # Implementation
//!
//! To alleviate these issues, Yew implements a single-threaded runtime that executes `?Send`
//! (`Send` or `!Send`) futures.
//!
//! On platforms with multi-threading support, Yew spawns multiple independent runtimes
//! proportional to the CPU core number. When tasks are spawned with a runtime handle, it will
//! randomly select a worker thread from the internal pool. All tasks spawned with `spawn_local`
//! will run on the same thread as the thread the task was running. When the runtime runs in a
//! WebAssembly target, all tasks will be scheduled on the main thread.
//!
//! This runtime is designed in favour of IO-bounded workload with similar runtime cost.
//! When running I/O workloads, it would produce a slightly better performance as tasks are
//! never moved to another thread. However, If a worker thread is busy,
//! other threads will not be able to steal tasks scheduled on the busy thread.
//! When you have a CPU-bounded task where CPU time is significantly
//! more expensive, it should be spawned with a dedicated thread (or Web Worker) and communicates
//! with the application using channels.
//!
//! Yew platform provides the following components:
//!
//! 1. A Task Scheduler that is capable of running non-Send tasks.
//! 2. A Timer that is compatible with the scheduler backend.
//! 3. Task Synchronisation Mechanisms.
//!
//! # Runtime Backend
//!
//! The Yew runtime is implemented with different runtimes depending on the target platform and can
//! use all features (timers / IO / task synchronisation) from the selected native runtime:
//!
//! - `wasm-bindgen-futures` (WebAssembly targets)
//! - `tokio` (non-WebAssembly targets)
#[doc(inline)]
pub use prokio::*;
Loading

0 comments on commit da09755

Please sign in to comment.