Skip to content

Commit f7e102a

Browse files
authored
Add SyncStore and use_store_sync (#5014)
1 parent 84efc00 commit f7e102a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

packages/stores/src/store.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use dioxus_core::{
88
use dioxus_signals::{
99
read_impls, write_impls, BorrowError, BorrowMutError, BoxedSignalStorage, CopyValue,
1010
CreateBoxedSignalStorage, Global, InitializeFromFunction, MappedMutSignal, ReadSignal,
11-
Readable, ReadableExt, ReadableRef, Storage, UnsyncStorage, Writable, WritableExt, WritableRef,
12-
WriteSignal,
11+
Readable, ReadableExt, ReadableRef, Storage, SyncStorage, UnsyncStorage, Writable, WritableExt,
12+
WritableRef, WriteSignal,
1313
};
1414
use std::marker::PhantomData;
1515

@@ -27,6 +27,9 @@ pub type ReadStore<T, S = UnsyncStorage> = Store<T, ReadSignal<T, S>>;
2727
/// A type alias for a boxed writable-only store.
2828
pub type WriteStore<T, S = UnsyncStorage> = Store<T, WriteSignal<T, S>>;
2929

30+
/// A type alias for a store backed by SyncStorage.
31+
pub type SyncStore<T> = Store<T, CopyValue<T, SyncStorage>>;
32+
3033
/// Stores are a reactive type built for nested data structures. Each store will lazily create signals
3134
/// for each field/member of the data structure as needed.
3235
///
@@ -404,6 +407,14 @@ pub fn use_store<T: 'static>(init: impl FnOnce() -> T) -> Store<T> {
404407
use_hook(move || Store::new(init()))
405408
}
406409

410+
/// Create a new [`SyncStore`]. Stores are a reactive type built for nested data structures.
411+
/// `SyncStore` is a Store backed by `SyncStorage`.
412+
///
413+
/// Like [`use_store`], but produces `SyncStore<T>` instead of `Store<T>`
414+
pub fn use_store_sync<T: Send + Sync + 'static>(init: impl FnOnce() -> T) -> SyncStore<T> {
415+
use_hook(|| Store::new_maybe_sync(init()))
416+
}
417+
407418
/// A type alias for global stores
408419
///
409420
/// # Example

0 commit comments

Comments
 (0)