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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
use crate::reactive_state_access::{*, ReactiveStateAccess};

use crate::store::{ Reaction, ReactiveContext,Store, StorageKey, SlottedKey};
use std::cell::RefCell;
use std::rc::Rc;
use std::hash::Hash;
// use seed::{*,prelude};


thread_local! {
    pub static STORE: RefCell<Store> = RefCell::new(Store::new());
}

// 
//  Constructs a T atom state accessor. T is stored keyed to the provided String id.
//  The accessor always references this id therefore can you can set/update/ or get this T
//  from anywhere.
// 
//   The passed closure is only used for the first initialisation of state.
//   Subsequent evaluations of this function just returns the accessor.
//   Only one type per context can be stored in this way.
// 
//
// Typically this is created via the #[atom] attribute macro
//
pub fn atom<T: 'static , F: Fn() -> () + 'static, U,A>(current_id: StorageKey, data_fn: F)  -> ReactiveStateAccess<T,U,IsAnAtomState> {
    
    // we do not need to re-initalize the atom if it already has been stored.
    if !reactive_state_exists_for_id::<T>(&current_id) {

       
        let reaction = Reaction{
            func: Rc::new(data_fn),
        };
 
        STORE.with(|store_refcell| {   
            store_refcell
                .borrow_mut()
                .new_reaction( &current_id, reaction.clone());
        });

        (reaction.func.clone())();

        STORE.with(|store_refcell| {
            store_refcell
                .borrow_mut().add_atom(&current_id);
        })

    }
    ReactiveStateAccess::new(current_id)
}

// 
//  Constructs a T reaction state accessor. T is stored keyed to the provided String id.
//  The accessor always references this id. Typically reaction values are auto
//  created based on changes to their dependencies which could be other reaction values or an
//  atom state.
//
//   The passed closure is run whenever a dependency of the reaction state has been updated.
// 
//
// Typically this is created via the #[reaction] attribute macro
//
pub fn reaction<T:'static,U,A,F: Fn()->() + 'static>(
    current_id: StorageKey, 
    data_fn: F,
    ) -> ReactiveStateAccess<T,NoUndo,IsAReactionState> {

    if !reactive_state_exists_for_id::<T>(&current_id) {
        STORE.with(|store_refcell| {

            let key = store_refcell
                .borrow_mut()
                .primary_slotmap.insert(current_id);

            store_refcell.borrow_mut().id_to_key_map.insert(current_id, key);
        });
        
    
        let reaction = Reaction{
            func: Rc::new(data_fn),
        };
 
        STORE.with(|store_refcell| {   
            store_refcell
                .borrow_mut()
                .new_reaction( &current_id, reaction.clone());
        });

        (reaction.func.clone())();

    }


    ReactiveStateAccess::<T,NoUndo,IsAReactionState>::new(current_id)
}




pub fn undo_atom_state<T: 'static + Clone, AllowUndo,IsAnAtomState>(current_id: &StorageKey){
    
    let mut undo_vec = remove_reactive_state_with_id::<UndoVec<T>>(current_id).expect("initial undo vec should be present but its not");
    
    if undo_vec.0.len() > 1 {
        let item =  undo_vec.0.pop().expect("type to exist");    
        update_atom_state_with_id(current_id,|t| *t = item);
        
    }
    set_inert_atom_state_with_id(undo_vec, current_id) ;

}

pub fn atom_with_undo<T: 'static , F: Fn() -> () + 'static, U,A>(current_id: StorageKey, data_fn: F)  -> ReactiveStateAccess<T,AllowUndo,IsAnAtomState> where T:Clone + 'static{
    
    if !reactive_state_exists_for_id::<T>(&current_id) {

        let reaction = Reaction{
            func: Rc::new(data_fn),
        };
 
        STORE.with(|store_refcell| {   
            store_refcell
                .borrow_mut()
                .new_reaction( &current_id, reaction.clone());
        });

        (reaction.func.clone())();
        
        STORE.with(|store_refcell| {
            store_refcell
                .borrow_mut().add_atom(&current_id);
        })
    }
    ReactiveStateAccess::new(current_id)
}

pub fn unlink_dead_links(id: &StorageKey){
    let context = illicit::Env::get::<RefCell<ReactiveContext>>().expect("No #[reaction] context found, are you sure you are in one? I.e. does the current function have a #[reaction] tag?");
    if reactive_state_exists_for_id::<ReactiveContext>(id) {
    read_reactive_state_with_id::<ReactiveContext,_,()>(id, |old_context| {
        
        let ids_to_remove = old_context.reactive_state_accessors.iter().filter(|a_id| !context.borrow().reactive_state_accessors.contains(a_id));
        for id_to_remove in ids_to_remove {
            STORE.with(|store_refcell| {
                store_refcell
                    .borrow_mut()
                    .remove_dependency(id_to_remove,id);
            })
        }
    }
    ) } else {
        set_inert_atom_state_with_id::<ReactiveContext>(context.borrow().clone(), id)
    }

}






pub fn set_inert_atom_state_with_id_with_undo<T: 'static>(data: T, current_id: &StorageKey) where T:Clone {
    let item = clone_reactive_state_with_id::<T>(current_id).expect("inital state needs to be present");
    let mut  undo_vec = remove_reactive_state_with_id::<UndoVec<T>>(current_id).expect("untitlal undo vec to be present");
    undo_vec.0.push(item);
    set_inert_atom_state_with_id(undo_vec, current_id) ;
    set_inert_atom_state_with_id(data, current_id);
    
}





pub fn set_atom_state_with_id_with_undo<T: 'static>(data: T, current_id: &StorageKey) where T:Clone {
    let item = clone_reactive_state_with_id::<T>(current_id).expect("inital state needs to be present");
    let mut  undo_vec = remove_reactive_state_with_id::<UndoVec<T>>(current_id).expect("untitlal undo vec to be present");
    undo_vec.0.push(item);
    set_inert_atom_state_with_id(undo_vec, current_id) ;
    set_inert_atom_state_with_id(data, current_id);
    execute_reaction_nodes(current_id);
    
}



/// Sets the state of type T keyed to the given TopoId
pub fn set_inert_atom_state_with_id<T: 'static>(data: T, current_id: &StorageKey) {
    STORE.with(|store_refcell| {
        store_refcell
            .borrow_mut()
            .set_state_with_id::<T>(data, current_id)
    })
}


/// Sets the state of type T keyed to the given TopoId
pub fn set_atom_state_with_id<T: 'static>(data: T, current_id: &StorageKey) {
    STORE.with(|store_refcell| {
        store_refcell
            .borrow_mut()
            .set_state_with_id::<T>(data, current_id)
    });

    execute_reaction_nodes(current_id);
}



pub fn reactive_state_exists_for_id<T: 'static>(id: &StorageKey) -> bool {
    STORE.with(|store_refcell| store_refcell.borrow().state_exists_with_id::<T>(id))
}


/// Clones the state of type T keyed to the given TopoId
pub fn clone_reactive_state_with_id<T: 'static + Clone>(id: &StorageKey) -> Option<T> {
    STORE.with(|store_refcell| {
        store_refcell
            .borrow_mut()
            .get_state_with_id::<T>(id)
            .cloned()
    })
}

pub fn remove_reactive_state_with_id<T: 'static>(id: &StorageKey) -> Option<T> {
    
    STORE.with(|store_refcell| {
        store_refcell
            .borrow_mut()
            .remove_state_with_id::<T>(id)
    })
}

#[derive(Clone)]
pub struct UndoVec<T>(pub Vec<T>);

pub fn update_atom_state_with_id_with_undo<T: 'static, F: FnOnce(&mut T) -> ()>(id: &StorageKey, func: F) where T:Clone{

    let mut item = remove_reactive_state_with_id::<T>(id)
        .expect("You are trying to update a type state that doesnt exist in this context!");

    
    let mut undo_vec = remove_reactive_state_with_id::<UndoVec<T>>(id)
        .expect("You are trying to update a type state that doesnt exist in this context!");
    undo_vec.0.push(item.clone());

    set_inert_atom_state_with_id(undo_vec, id);
    

    func(&mut item);
    set_inert_atom_state_with_id(item, id);
    
    execute_reaction_nodes(id);
}

pub fn execute_reaction_nodes(id: &StorageKey) {
    let ids_reactions = STORE.with(|refcell_store|{
        let mut borrow = refcell_store.borrow_mut();
        borrow.clone_dep_funcs_for_id(id)
    });

    for (key,reaction)in &ids_reactions {
        let cloned_reaction = reaction.clone();
        (cloned_reaction.func.clone())();
        execute_reaction_nodes(&key);
    }
}



pub fn update_atom_state_with_id<T: 'static, F: FnOnce(&mut T) -> ()>(id: &StorageKey, func: F) {
    let mut item = remove_reactive_state_with_id::<T>(id)
        .expect("You are trying to update a type state that doesnt exist in this context!");
    
    func(&mut item);
    

    set_inert_atom_state_with_id(item, id);
    
    //we need to get the associated data with this key
    execute_reaction_nodes(id);

}

pub fn read_reactive_state_with_id<T: 'static, F: FnOnce(&T) -> R, R>(id: &StorageKey, func: F) -> R {
    let item = remove_reactive_state_with_id::<T>(id)
        .expect("You are trying to read a type state that doesnt exist in this context!");
    let read = func(&item);
    set_inert_atom_state_with_id(item, id);
    read
}


pub fn try_read_reactive_state_with_id<T: 'static, F: FnOnce(Option<&T>) -> R, R>(id: &StorageKey, func: F) -> R {
    let item = remove_reactive_state_with_id::<T>(id);
    let read = func(item.as_ref());
    set_inert_atom_state_with_id(item, id);
    read
}

pub fn return_key_for_type_and_insert_if_required<T: 'static + Clone + Eq + Hash>(value: T) -> StorageKey {
    use std::collections::hash_map::DefaultHasher;
    use std::hash::{Hasher};

    let mut hasher = DefaultHasher::new();
    value.hash(&mut hasher);
    let hash_id = hasher.finish();

    let key = StorageKey::SlottedKey(SlottedKey {
        location: hash_id,
        slot: 0
    });

    STORE.with(|refcell_store|
    {
        refcell_store.borrow_mut().return_key_for_type_and_insert_if_required(key,value)
    })
}







// fn deep_collision_check_for_state_id<T: 'static>(id:StorageKey, item:T, stored_value:T) -> StorageKey {
//     if let Some() = remove_reactive_state_with_id::<Vec<(T,StorageKey)>(id) {

//     } else {
//         set_inert_atom_state_with_id(vec![], id);
//     }

// }