1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
use crate::state_access::CloneState;
use crate::state_access::StateAccess;
use crate::reactive_state_functions::{STORE,execute_reaction_nodes};
use crate::store::{StorageKey, SlottedKey, ReactiveContext,TopoKey};
use crate::unmount::Unmount;
use std::cell::RefCell;
use std::collections::HashSet;
#[topo::nested]
pub fn use_state<T: 'static, F: FnOnce() -> T>(data_fn: F) -> StateAccess<T> {
use_state_current(data_fn)
}
pub fn use_state_current<T: 'static, F: FnOnce() -> T>(data_fn: F) -> StateAccess<T> {
let id = topo::Id::current();
let ctx = get_state_slotted_key_struct_if_in_context();
let id = TopoKey {
ctx,
id,
};
if !state_exists_for_topo_id::<T>(id) {
set_state_with_topo_id::<T>(data_fn(), id);
}
StateAccess::new(id)
}
#[topo::nested]
pub fn new_state<T: 'static, F: FnOnce() -> T>(data_fn: F) -> StateAccess<T> {
let count = use_state(|| 0);
count.update(|c| *c += 1);
topo::call_in_slot(count.get(), || use_state_current(data_fn))
}
pub fn set_state_with_topo_id<T: 'static>(data: T, current_id: TopoKey) {
STORE.with(|store_refcell| {
store_refcell
.borrow_mut()
.set_state_with_id::<T>(data, &StorageKey::TopoKey(current_id))
});
execute_reaction_nodes(&StorageKey::TopoKey(current_id));
}
pub fn state_exists_for_topo_id<T: 'static>(id: TopoKey) -> bool {
STORE.with(|store_refcell| store_refcell.borrow().state_exists_with_id::<T>(&StorageKey::TopoKey(id)))
}
fn get_state_slotted_key_struct_if_in_context() -> Option<SlottedKey>{
illicit::Env::get::<RefCell<ReactiveContext>>().map(|ctx|
if let StorageKey::SlottedKey(key) = ctx.borrow_mut().key {
key}
else {
panic!("Should never be a topo key as the context")
}
)
}
pub fn clone_state_with_topo_id<T: 'static + Clone>(id: TopoKey) -> Option<T> {
STORE.with(|store_refcell| {
store_refcell
.borrow_mut()
.get_state_with_id::<T>(&StorageKey::TopoKey(id))
.cloned()
})
}
pub fn remove_state_with_topo_id<T: 'static>(id: TopoKey) -> Option<T> {
STORE.with(|store_refcell| {
store_refcell
.borrow_mut()
.remove_state_with_id::<T>(&StorageKey::TopoKey(id))
})
}
pub fn update_state_with_topo_id<T: 'static, F: FnOnce(&mut T) -> ()>(id: TopoKey, func: F) {
let mut item = remove_state_with_topo_id::<T>(id)
.expect("You are trying to update a type state that doesnt exist in this context!");
func(&mut item);
set_state_with_topo_id(item, id);
execute_reaction_nodes(&StorageKey::TopoKey(id));
}
pub fn read_state_with_topo_id<T: 'static, F: FnOnce(&T) -> R, R>(id: TopoKey, func: F) -> R {
let item = remove_state_with_topo_id::<T>(id)
.expect("You are trying to read a type state that doesnt exist in this context!");
let read = func(&item);
set_state_with_topo_id(item, id);
read
}
pub fn reset_unseen_id_list() {
STORE.with(|store_refcell| {
let mut store_mut = store_refcell.borrow_mut();
store_mut.unseen_ids = HashSet::new();
let ids = store_mut.id_to_key_map.keys()
.filter_map(|k| match k {
StorageKey::SlottedKey(_)=> None,
StorageKey::TopoKey(key)=>Some(key)}).cloned().collect::<Vec<_>>();
for id in ids {
store_mut.unseen_ids.insert(id);
}
})
}
pub fn unseen_ids() -> Vec<TopoKey> {
STORE.with(|store_refcell| {
let store_mut = store_refcell.borrow_mut();
store_mut.unseen_ids.iter().cloned().collect::<Vec<_>>()
})
}
pub fn execute_and_remove_unmounts() {
for id in unseen_ids() {
if state_exists_for_topo_id::<Unmount>(id) {
read_state_with_topo_id::<Unmount, _, _>(id, |dt| dt.execute_if_activated());
remove_state_with_topo_id::<Unmount>(id);
}
}
}
#[topo::nested]
pub fn on_unmount<F: Fn() -> () + 'static>(unmount_fn: F) -> StateAccess<Unmount> {
use_state(|| Unmount::new(unmount_fn))
}