Skip to content

Commit

Permalink
Enable tower-buffer to be able to run in browser environment.
Browse files Browse the repository at this point in the history
This replaces tokio::spawn by wasm_bindgen_futures::spawn_local
for wasm32 target arch.
  • Loading branch information
boxdot committed Nov 12, 2020
1 parent 0f9eb64 commit 1091340
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions tower-buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ tower-service = "0.3"
tower-layer = "0.3"
tokio = { version = "0.2", features = ["rt-core", "sync"] }
tracing = "0.1.2"
cfg-if = "1.0.0"

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

[dev-dependencies]
tower-test = { version = "0.3", path = "../tower-test" }
Expand Down
10 changes: 9 additions & 1 deletion tower-buffer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ where
{
let (tx, rx) = mpsc::channel(bound);
let (handle, worker) = Worker::new(service, rx);
tokio::spawn(worker);

cfg_if::cfg_if! {
if #[cfg(not(target_arch = "wasm32"))] {
tokio::spawn(worker);
} else {
wasm_bindgen_futures::spawn_local(worker);
}
}

Buffer { tx, handle }
}

Expand Down

0 comments on commit 1091340

Please sign in to comment.