Skip to content

Commit

Permalink
fix dioxus storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmloff committed Mar 28, 2024
1 parent e3ad85b commit a8e4f99
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 294 deletions.
12 changes: 12 additions & 0 deletions examples/storage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "storage-desktop"
version = "0.1.0"
edition = "2021"

[dependencies]
dioxus-std = { workspace = true, features = ["storage"] }
dioxus = { version = "0.5.0-alpha.2", features = ["router"] }

[features]
web = ["dioxus/web"]
desktop = ["dioxus/desktop"]
File renamed without changes.
54 changes: 39 additions & 15 deletions examples/storage_web/src/main.rs → examples/storage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@ use dioxus_router::prelude::*;
use dioxus_std::storage::*;

fn main() {
// init debug tool for WebAssembly
wasm_logger::init(wasm_logger::Config::default());
console_error_panic_hook::set_once();
dioxus_web::launch(App);
dioxus_std::storage::set_dir!();
launch(app);
}

#[component]
fn App(cx: Scope) -> Element {
render! {
fn app() -> Element {
rsx! {
Router::<Route> {}
}
}
Expand All @@ -27,15 +24,42 @@ enum Route {
}

#[component]
fn Footer(cx: Scope) -> Element {
render! {
fn Footer() -> Element {

let new_window = {
#[cfg(feature = "desktop")]
{
let window = dioxus::desktop::use_window();
rsx! {
div {
button {
onclick: move |_| {
let dom = VirtualDom::new(app);
window.new_window(dom, Default::default());
},
"New Window"
}
}
}
}
#[cfg(not(feature = "desktop"))]
{
rsx! {
div {}
}
}
};

rsx! {
div {
Outlet::<Route> { }

p {
"----"
}

{new_window}

nav {
ul {
li { Link { to: Route::Page1 {}, "Page1" } }
Expand All @@ -47,16 +71,16 @@ fn Footer(cx: Scope) -> Element {
}

#[component]
fn Page1(cx: Scope) -> Element {
render!("Home")
fn Page1() -> Element {
rsx!("Home")
}

#[component]
fn Page2(cx: Scope) -> Element {
let count_session = use_singleton_persistent(cx, || 0);
let count_local = use_synced_storage::<LocalStorage, i32>(cx, "synced".to_string(), || 0);
fn Page2() -> Element {
let mut count_session = use_singleton_persistent(|| 0);
let mut count_local = use_synced_storage::<LocalStorage, i32>("synced".to_string(), || 0);

render!(
rsx!(
div {
button {
onclick: move |_| {
Expand Down
12 changes: 0 additions & 12 deletions examples/storage_desktop/Cargo.toml

This file was deleted.

113 changes: 0 additions & 113 deletions examples/storage_desktop/src/main.rs

This file was deleted.

15 changes: 0 additions & 15 deletions examples/storage_web/Cargo.toml

This file was deleted.

7 changes: 0 additions & 7 deletions examples/storage_web/README.md

This file was deleted.

16 changes: 14 additions & 2 deletions std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,21 @@ i18n = [
]
storage = [
# Shared
"dep:dioxus",
"dep:rustc-hash",
"dep:postcard",
"dep:once_cell",
"dep:dioxus-signals",
"dep:tokio",
"dep:yazi",
"web-sys/StorageEvent",
"dep:serde",
"dep:futures-util",

# WASM
"dep:directories",

# Not WASM
"dep:wasm-bindgen",
]

# CI testing
Expand All @@ -86,7 +93,7 @@ notify-rust = { version = "4.8.0", optional = true }
uuid = { version = "1.3.2", optional = true }
async-broadcast = { version = "0.5.1", optional = true }

# Used by: geolocation
# Used by: geolocation, storage
futures = { version = "0.3.28", features = ["std"], optional = true }
futures-util = { version = "0.3.28", optional = true }

Expand All @@ -104,6 +111,7 @@ dioxus-signals = { version = "0.5.0-alpha.2", features = [
], optional = true }
tokio = { version = "1.33.0", features = ["sync"], optional = true }
yazi = { version = "0.1.4", optional = true }
tracing = "0.1.40"

# # # # # # # # #
# Windows Deps. #
Expand Down Expand Up @@ -133,6 +141,10 @@ js-sys = "0.3.62"
uuid = { version = "1.3.2", features = ["js"] }


[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
# Used by: storage
directories = { version = "4.0.1", optional = true }

# # # # #
# Docs. #
# # # # #
Expand Down
34 changes: 2 additions & 32 deletions std/src/storage/client_storage/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,6 @@ use tokio::sync::watch::{channel, Receiver};

use crate::storage::{serde_to_string, try_serde_from_string, StorageBacking, StorageSubscriber};

#[allow(clippy::needless_doctest_main)]
/// Set the directory where the storage files are located on non-wasm targets.
///
/// ```rust
/// use dioxus_std::set_dir;
///
/// fn main(){
/// // set the directory to the default location
/// set_dir!();
/// }
/// ```
/// ```rust
/// use dioxus_std::set_dir;
///
/// fn main(){
/// // set the directory to a custom location
/// set_dir!("path/to/dir");
/// }
/// ```
#[macro_export]
macro_rules! set_dir {
() => {
$crate::storage::set_dir_name(env!("CARGO_PKG_NAME"))
};
($path: literal) => {
$crate::storage::set_directory(std::path::PathBuf::from($path))
};
}
pub use set_dir;

#[doc(hidden)]
/// Sets the directory where the storage files are located.
pub fn set_directory(path: std::path::PathBuf) {
Expand Down Expand Up @@ -137,15 +107,15 @@ impl StorageSubscriber<LocalStorage> for LocalStorage {
}

fn unsubscribe(key: &<LocalStorage as StorageBacking>::Key) {
log::trace!("Unsubscribing from \"{}\"", key);
tracing::trace!("Unsubscribing from \"{}\"", key);

// Fail silently if unsubscribe is called but the subscriptions map isn't initialized yet.
if let Some(subscriptions) = SUBSCRIPTIONS.get() {
let read_binding = subscriptions.read().unwrap();

// If the subscription exists, remove it from the subscriptions map.
if read_binding.contains_key(key) {
log::trace!("Found entry for \"{}\"", key);
tracing::trace!("Found entry for \"{}\"", key);
drop(read_binding);
subscriptions.write().unwrap().remove(key);
}
Expand Down
3 changes: 1 addition & 2 deletions std/src/storage/client_storage/memory.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::any::Any;
use std::cell::RefCell;
use std::collections::HashMap;
use std::ops::{Deref, DerefMut};
use std::rc::Rc;
use std::sync::Arc;

use dioxus::prelude::RefCell;

use crate::storage::StorageBacking;

#[derive(Clone)]
Expand Down
Loading

0 comments on commit a8e4f99

Please sign in to comment.