You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/perseus/src/page_state_store.rs
+32-10
Original file line number
Diff line number
Diff line change
@@ -3,23 +3,37 @@ use std::cell::RefCell;
3
3
use std::collections::HashMap;
4
4
use std::rc::Rc;
5
5
6
+
/// A key type for the `PageStateStore` that denotes both a page's state type and its URL.
7
+
#[derive(Hash,PartialEq,Eq)]
8
+
pubstructPageStateKey{
9
+
state_type:TypeId,
10
+
url:String,
11
+
}
12
+
6
13
/// A container for page state in Perseus. This is designed as a context store, in which one of each type can be stored. Therefore, it acts very similarly to Sycamore's context system,
7
14
/// though it's specifically designed for each page to store one reactive properties object. In theory, you could interact with this entirely independently of Perseus' state interface,
8
15
/// though this isn't recommended.
16
+
///
17
+
/// Note that the same pages in different locales will have different entries here. If you need to store state for a page across locales, you should use the global state system instead. For apps
18
+
/// not using i18n, the page URL will not include any locale.
9
19
// TODO Make this work with multiple pages for a single template
10
20
#[derive(Default,Clone)]
11
21
pubstructPageStateStore{
12
22
/// A map of type IDs to anything, allowing one storage of each type (each type is intended to a properties `struct` for a template). Entries must be `Clone`able becasue we assume them
13
23
/// to be `Signal`s or `struct`s composed of `Signal`s.
14
24
// Technically, this should be `Any + Clone`, but that's not possible without something like `dyn_clone`, and we don't need it because we can restrict on the methods instead!
/// Adds a new element to the state by its type. Any existing element with the same type will be silently overriden (use `.contains()` to check first if needed).
32
-
pubfnadd<T:Any + Clone>(&mutself,val:T){
45
+
/// Adds a new element to the state by its type and URL. Any existing element with the same type and URL will be silently overriden (use `.contains()` to check first if needed).
46
+
pubfnadd<T:Any + Clone>(&mutself,url:&str,val:T){
33
47
let type_id = TypeId::of::<T>();
48
+
let key = PageStateKey{
49
+
state_type: type_id,
50
+
url: url.to_string(),
51
+
};
34
52
letmut map = self.map.borrow_mut();
35
-
map.insert(type_id,Box::new(val));
53
+
map.insert(key,Box::new(val));
36
54
}
37
-
/// Checks if the state contains the element of the given type.
38
-
pubfncontains<T:Any + Clone>(&self) -> bool{
55
+
/// Checks if the state contains the element of the given type for the given page.
0 commit comments