Skip to content

Commit

Permalink
feat: use wasi crate and new executor
Browse files Browse the repository at this point in the history
Signed-off-by: Enzo "raskyld" Nocera <[email protected]>
  • Loading branch information
raskyld committed Oct 20, 2024
1 parent 1051018 commit a683304
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bytes = "1.7.2"
cfg-if = "1"
console_error_panic_hook = "0.1.7"
futures = "0.3.30"
wasi = { version = "0.13.1+wasi-0.2.0", optional = true }
hydration_context = "0.2.0-beta5"
leptos = { path = "../leptos/leptos" }
leptos_meta = { path = "../leptos/meta" }
Expand All @@ -31,6 +32,7 @@ ssr = [
"leptos_router/ssr",
"dep:wit-bindgen-rt",
"dep:leptos_wasi",
"dep:wasi",
]

[profile.wasm-release]
Expand Down
4 changes: 0 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ mod routes;
#[cfg(feature = "ssr")]
mod server;

#[cfg(feature = "ssr")]
#[allow(warnings)]
mod bindings;

/// This is the entrypoint called by the JS "igniter" script.
#[cfg(feature = "hydrate")]
#[wasm_bindgen::prelude::wasm_bindgen]
Expand Down
10 changes: 6 additions & 4 deletions src/pages/home.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use futures::future;
use leptos::prelude::*;

#[component]
Expand Down Expand Up @@ -35,10 +34,9 @@ pub fn Home() -> impl IntoView {

#[server]
pub async fn update_count() -> Result<(), ServerFnError> {
use crate::bindings::wasi::filesystem::{preopens::get_directories, types::{DescriptorFlags, OpenFlags, PathFlags}};
use wasi::filesystem::{preopens::get_directories, types::{DescriptorFlags, OpenFlags, PathFlags}};

println!("User requested an update to the store");

let updated_value = get_count().await? + 1;
let directories = get_directories();
let (fd, _) = directories.first().expect("no directory given");
Expand All @@ -57,7 +55,11 @@ pub async fn update_count() -> Result<(), ServerFnError> {

#[server]
pub async fn get_count() -> Result<u64, ServerFnError> {
use crate::bindings::wasi::filesystem::{preopens::get_directories, types::{DescriptorFlags, OpenFlags, PathFlags}};
use wasi::filesystem::{preopens::get_directories, types::{DescriptorFlags, OpenFlags, PathFlags}};
use leptos_wasi::executor::sleep;

println!("Sleeping 3s before getting store to test Suspense");
sleep(3_000_000_000).await;

println!("Getting the store");
let directories = get_directories();
Expand Down
15 changes: 8 additions & 7 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use std::task::Poll;
use bytes::Bytes;
use futures::stream;
use leptos::{config::get_configuration, error::Error, task::Executor};
use leptos_wasi::{bindings::{export, exports::wasi::http::incoming_handler::Guest}, prelude::{Body, IncomingRequest, ResponseOutparam}};
use leptos_wasi::prelude::{Body, WasiExecutor};
use wasi::{exports::http::incoming_handler::Guest, filesystem::{preopens::get_directories, types::{DescriptorFlags, OpenFlags, PathFlags}}, http::{proxy::export, types::{IncomingRequest, ResponseOutparam}}};

use crate::{bindings::wasi::filesystem::{preopens::get_directories, types::{DescriptorFlags, OpenFlags, PathFlags}}, pages::home::{GetCount, UpdateCount}, routes::{shell, App}};
use crate::{pages::home::{GetCount, UpdateCount}, routes::{shell, App}};

struct LeptosServer;

Expand All @@ -15,11 +16,11 @@ impl Guest for LeptosServer {
fn handle(request: IncomingRequest, response_out: ResponseOutparam) {
// Initiate a single-threaded [`Future`] Executor so we can run the
// rendering system and take advantage of bodies streaming.
Executor::init_futures_local_executor().expect("cannot init future executor");
Executor::spawn(async {
let executor = WasiExecutor::new(leptos_wasi::executor::Mode::Stalled);
Executor::init_local_custom_executor(executor.clone()).expect("cannot init future executor");
executor.run_until(async {
handle_request(request, response_out).await;
});
Executor::run();
})
}
}

Expand Down Expand Up @@ -86,4 +87,4 @@ fn serve_static_files(path: String)
}
}

export!(LeptosServer with_types_in leptos_wasi::bindings);
export!(LeptosServer with_types_in wasi);

0 comments on commit a683304

Please sign in to comment.