Skip to content

Commit

Permalink
added use_web_lock
Browse files Browse the repository at this point in the history
  • Loading branch information
maccesch committed Sep 1, 2024
1 parent 851d209 commit c60d32b
Show file tree
Hide file tree
Showing 17 changed files with 605 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .idea/leptos-use.iml

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

5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased] -
## [0.13.1] - 2024-09-01

### New Function 🚀
### New Functions 🚀

- `use_web_lock`
- `use_window_size`

### Change 🔥
Expand Down
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leptos-use"
version = "0.13.0"
version = "0.13.1"
edition = "2021"
authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"]
Expand Down Expand Up @@ -48,6 +48,7 @@ unic-langid = { version = "0.9", features = ["macros"] }

[features]
default = [
"use_web_lock",
"use_window_size",
"is_err",
"is_none",
Expand Down Expand Up @@ -121,6 +122,14 @@ default = [
"watch_with_options",
"whenever"
]
use_web_lock = [
"web-sys/AbortSignal",
"web-sys/Lock",
"web-sys/LockManager",
"web-sys/LockMode",
"web-sys/LockOptions",
"web-sys/Navigator",
]
use_window_size = ["use_media_query"]
actix = ["dep:actix-web", "dep:leptos_actix", "dep:http0_2"]
axum = ["dep:leptos_axum", "dep:http1"]
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
- [use_prefers_reduced_motion](browser/use_prefers_reduced_motion.md)
- [use_service_worker](browser/use_service_worker.md)
- [use_user_media](browser/use_user_media.md)
- [use_web_lock](browser/use_web_lock.md)
- [use_web_notification](browser/use_web_notification.md)

# Sensors
Expand Down
3 changes: 3 additions & 0 deletions docs/book/src/browser/use_web_lock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# use_web_lock

<!-- cmdrun python3 ../extract_doc_comment.py use_web_lock use_web_lock -->
1 change: 1 addition & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ members = [
"use_timestamp",
"use_toggle",
"use_user_media",
"use_web_lock",
"use_web_notification",
"use_websocket",
"use_webtransport",
Expand Down
17 changes: 17 additions & 0 deletions examples/use_web_lock/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "use_web_lock"
version = "0.1.0"
edition = "2021"

[dependencies]
gloo-timers = { version = "0.3", features = ["futures"] }
leptos = { version = "0.6", features = ["nightly", "csr"] }
console_error_panic_hook = "0.1"
console_log = "1"
log = "0.4"
leptos-use = { path = "../..", features = ["use_web_lock", "docs"] }
web-sys = { version = "0.3", features = ["Lock"] }

[dev-dependencies]
wasm-bindgen = "0.2"
wasm-bindgen-test = "0.3.0"
23 changes: 23 additions & 0 deletions examples/use_web_lock/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
A simple example for `use_web_lock`.

If you don't have it installed already, install [Trunk](https://trunkrs.dev/) and [Tailwind](https://tailwindcss.com/docs/installation)
as well as the nightly toolchain for Rust and the wasm32-unknown-unknown target:

```bash
cargo install trunk
npm install -D tailwindcss @tailwindcss/forms
rustup toolchain install nightly
rustup target add wasm32-unknown-unknown
```

Then, open two terminals. In the first one, run:

```
npx tailwindcss -i ./input.css -o ./style/output.css --watch
```

In the second one, run:

```bash
trunk serve --open
```
2 changes: 2 additions & 0 deletions examples/use_web_lock/Trunk.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[build]
public_url = "/demo/"
7 changes: 7 additions & 0 deletions examples/use_web_lock/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<link data-trunk rel="css" href="style/output.css">
</head>
<body></body>
</html>
3 changes: 3 additions & 0 deletions examples/use_web_lock/input.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
2 changes: 2 additions & 0 deletions examples/use_web_lock/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "nightly"
47 changes: 47 additions & 0 deletions examples/use_web_lock/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use gloo_timers::future::sleep;
use leptos::*;
use leptos_use::docs::demo_or_body;
use leptos_use::use_web_lock;
use std::time::Duration;

async fn my_process(_lock: web_sys::Lock) -> i32 {
sleep(Duration::from_millis(2000)).await;

42
}

#[component]
fn Demo() -> impl IntoView {
let (res, set_res) = create_signal("Not started yet".to_string());

let on_click = move |_| {
set_res.set("Running...".to_string());

spawn_local(async move {
let res = use_web_lock("my_lock", my_process).await;

match res {
Ok(res) => {
set_res.set(format!("Result: {}", res));
}
Err(e) => {
set_res.set(format!("Error: {:?}", e));
}
}
});
};

view! {
<button on:click=on_click>Run locked task</button>
<p>{res}</p>
}
}

fn main() {
_ = console_log::init_with_level(log::Level::Debug);
console_error_panic_hook::set_once();

mount_to(demo_or_body(), || {
view! { <Demo/> }
})
}
Loading

0 comments on commit c60d32b

Please sign in to comment.