### Start Local Development Server Source: https://github.com/ranile/gloo/blob/master/website/README.md Starts a local development server for live preview. Changes are reflected without server restart. ```bash yarn start ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/ranile/gloo/blob/master/website/README.md Installs project dependencies using yarn. ```bash yarn install ``` -------------------------------- ### Serve Project with Trunk Source: https://github.com/ranile/gloo/blob/master/examples/nested-worker-yew/README.md Use this command to serve the project locally. Ensure Trunk is installed. ```bash trunk serve ``` -------------------------------- ### Build and Run Gloo History WASI Example Source: https://github.com/ranile/gloo/blob/master/examples/history-wasi/README.md Use these commands to compile the WASI module and then execute it using Wasmtime. ```bash cargo build --manifest-path examples/history-wasi/Cargo.toml --target wasm32-wasip1 ``` ```bash wasmtime examples/history-wasi/target/wasm32-wasip1/debug/example-history-wasi.wasm ``` -------------------------------- ### Browser Dialog Examples Source: https://context7.com/ranile/gloo/llms.txt Demonstrates using `alert`, `confirm`, and `prompt` for basic browser dialog interactions. `confirm` returns a boolean, and `prompt` returns an `Option`. ```rust use gloo_dialogs::{alert, confirm, prompt}; fn dialog_examples() { // Show an alert message alert("Operation completed successfully!"); // Ask for confirmation (returns bool) let confirmed = confirm("Are you sure you want to delete this item?"); if confirmed { web_sys::console::log_1(&"User confirmed deletion".into()); } // Prompt for input (returns Option) let name = prompt("Enter your name:", None); match name { Some(n) => web_sys::console::log_1(&format!("Hello, {}!", n).into()), None => web_sys::console::log_1(&"User cancelled".into()), } // Prompt with default value let email = prompt("Enter your email:", Some("user@example.com")); } ``` -------------------------------- ### Make HTTP GET Requests with Gloo Net Source: https://context7.com/ranile/gloo/llms.txt Perform HTTP GET requests using a fluent builder API. Supports headers, query parameters, and JSON response parsing. Requires async context and spawn_local for execution. ```rust use gloo_net::http::Request; use serde::Deserialize; #[derive(Deserialize, Debug)] struct User { id: u32, name: String, email: String, } async fn fetch_user(user_id: u32) -> Result { let user: User = Request::get(&format!("https://api.example.com/users/{}", user_id)) .header("Authorization", "Bearer token123") .send() .await? .json() .await?; Ok(user) } // Usage with spawn_local use wasm_bindgen_futures::spawn_local; spawn_local(async { match fetch_user(1).await { Ok(user) => web_sys::console::log_1(&format!("User: {:?}", user).into()), Err(e) => web_sys::console::log_1(&format!("Error: {:?}", e).into()), } }); ``` -------------------------------- ### EventSource Stream Handling with Gloo-Net Source: https://github.com/ranile/gloo/blob/master/crates/net/README.md This example shows how to connect to an EventSource and subscribe to multiple event types. It uses `futures::stream::select` to multiplex events from different streams and logs received messages. Requires `gloo-net`, `wasm-bindgen-futures`, and `futures` crates. ```rust use gloo_net::eventsource::futures::EventSource; use wasm_bindgen_futures::spawn_local; use futures::{stream, StreamExt}; let mut es = EventSource::new("http://api.example.com/ssedemo.php").unwrap(); let stream_1 = es.subscribe("some-event-type").unwrap(); let stream_2 = es.subscribe("another-event-type").unwrap(); spawn_local(async move { let mut all_streams = stream::select(stream_1, stream_2); while let Some(Ok((event_type, msg))) = all_streams.next().await { gloo_console::log!("1. {}: {:?}", event_type, msg) } gloo_console::log!("EventSource Closed"); }) ``` -------------------------------- ### Build All Gloo Crates Source: https://github.com/ranile/gloo/blob/master/CONTRIBUTING.md Use this command to build all crates within the Gloo project. Ensure you have the Rust toolchain installed. ```shell cargo build --all ``` -------------------------------- ### HTTP POST Request with JSON Body using Gloo-Net Source: https://github.com/ranile/gloo/blob/master/crates/net/README.md This example shows how to send a POST request with a JSON payload. It serializes a struct to JSON using `serde` and sends it to a given URL. The response text is then retrieved. Requires `gloo-net` and `serde` crates. ```rust use gloo_net::http::Request; use serde::Serialize; #[derive(Serialize)] struct Post<'a> { title: &'a str, body: &'a str, #[serde(rename = "userId")] user_id: u32, } async fn run() -> Result<(), gloo_net::Error> { let response = Request::post("https://example.com/posts") .json(&Post { title: "hello", body: "world", user_id: 1 })? .send() .await?; let data = response.text().await?; Ok(()) } ``` -------------------------------- ### Basic HTTP GET Request with Gloo-Net Source: https://github.com/ranile/gloo/blob/master/crates/net/README.md Use this snippet for making a simple GET request to a specified path. It sends the request and asserts that the response status code is 200. Requires the `gloo-net` crate. ```rust use gloo_net::http::Request; async fn run() -> Result<(), gloo_net::Error> { let resp = Request::get("/path") .send() .await .unwrap(); assert_eq!(resp.status(), 200); Ok(()) } ``` -------------------------------- ### Add Event Listener and Timeout with Gloo Source: https://github.com/ranile/gloo/blob/master/README.md This example demonstrates using `gloo::events::EventListener` to attach a click handler to a button and `gloo::timers::callback::Timeout` to schedule an update after a delay. It requires importing necessary modules from `gloo` and `wasm_bindgen`. ```rust use gloo::{events::EventListener, timers::callback::Timeout}; use wasm_bindgen::prelude::*; pub struct DelayedHelloButton { button: web_sys::Element, on_click: events::EventListener, } impl DelayedHelloButton { pub fn new(document: &web_sys::Document) -> Result { // Create a `