leptos_use_web_lock/options.rs
1use default_struct_builder::DefaultBuilder;
2use web_sys::LockMode;
3
4/// Options for [`fn@crate::use_web_lock_with_options`].
5#[allow(dead_code)]
6#[derive(DefaultBuilder)]
7pub struct UseWebLockOptions {
8 /// The default mode is `LockMode::Exclusive`, but `LockMode::Shared` can be specified.
9 /// There can be only one `Exclusive` holder of a lock, but multiple `Shared` requests can be
10 /// granted at the same time. This can be used to implement the
11 /// [readers-writer pattern](https://en.wikipedia.org/wiki/Readers%E2%80%93writer_lock).
12 mode: LockMode,
13
14 /// If `true`, the lock request will fail if the lock cannot be granted immediately without
15 /// waiting. The callback is invoked with `null`. Defaults to `false`.
16 if_available: bool,
17
18 /// If `true`, then any held locks with the same name will be released, and the request will
19 /// be granted, preempting any queued requests for it. Defaults to `false`.
20 steal: bool,
21 // TODO : add abort signal (this also requires to create a wrapper for AbortSignal similar to UseWindow)
22}
23
24#[cfg(not(feature = "ssr"))]
25impl UseWebLockOptions {
26 pub(crate) fn to_web_sys(&self) -> web_sys::LockOptions {
27 let options = web_sys::LockOptions::new();
28 options.set_mode(self.mode);
29 options.set_if_available(self.if_available);
30 options.set_steal(self.steal);
31
32 options
33 }
34}
35
36impl Default for UseWebLockOptions {
37 fn default() -> Self {
38 Self {
39 mode: LockMode::Exclusive,
40 if_available: false,
41 steal: false,
42 }
43 }
44}