revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
Documentation
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! Incremental computed values for SignalVec
//!
//! Enables efficient updates to derived values from SignalVec by processing
//! individual changes instead of recomputing the entire result.
//!
//! # Example: Filtered List with Incremental Updates
//!
//! ```ignore
//! use revue::reactive::{signal_vec, IncrementalComputed, IncrementalHandlers};
//!
//! let items = signal_vec(vec![1, 2, 3, 4, 5, 6]);
//!
//! // Create a filtered list that incrementally updates
//! let evens = IncrementalComputed::new(
//!     items.clone(),
//!     // Initial computation
//!     |v| v.iter().filter(|x| *x % 2 == 0).copied().collect::<Vec<_>>(),
//!     IncrementalHandlers::new()
//!         .insert(|result, _index, value| {
//!             // Only add if the new value is even
//!             if value % 2 == 0 {
//!                 result.push(value);
//!             }
//!         })
//!         .update(|result, index, old, new| {
//!             // Handle value changes
//!             if old % 2 == 0 && new % 2 != 0 {
//!                 // Remove the old even value
//!                 result.retain(|x| *x != old);
//!             } else if old % 2 != 0 && new % 2 == 0 {
//!                 // Add the new even value
//!                 result.push(new);
//!             }
//!         })
//!         .remove(|result, _index, value| {
//!             // Remove the value if it was in the result
//!             result.retain(|x| *x != value);
//!         })
//!         .replace(|items| {
//!             // Full recomputation for batch changes
//!             items.get().iter().filter(|x| *x % 2 == 0).copied().collect()
//!         }),
//! );
//!
//! // Initial state
//! assert_eq!(evens.get(), vec![2, 4, 6]);
//!
//! // Incremental updates (efficient - no full re-filter)
//! items.push(8);   // Only checks if 8 is even
//! items.remove(1); // Only removes 2 from result
//! items.update(0, 10); // Only updates the affected values
//! ```
//!
//! # Performance Benefits
//!
//! With standard `Computed`, adding 1 item to a 1000-item filtered list would
//! require re-filtering all 1001 items. With `IncrementalComputed`, only the
//! new item is checked, resulting in ~1000x performance improvement for this case.

use super::signal_vec::SignalVec;
use super::tracker::notify_dependents;
use super::SignalId;
use crate::utils::lock::lock_or_recover;
use std::sync::{Arc, Mutex};

/// Handlers for incremental updates from a SignalVec
#[allow(clippy::type_complexity)]
pub struct IncrementalHandlers<T: 'static, R: 'static> {
    /// Handle insert operation
    pub on_insert: Arc<dyn Fn(&mut R, usize, T) + Send + Sync>,
    /// Handle update operation
    pub on_update: Arc<dyn Fn(&mut R, usize, T, T) + Send + Sync>,
    /// Handle remove operation
    pub on_remove: Arc<dyn Fn(&mut R, usize, T) + Send + Sync>,
    /// Handle replace operation (full recomputation)
    pub on_replace: Arc<dyn Fn(&SignalVec<T>) -> R + Send + Sync>,
}

impl<T: 'static, R: 'static> Clone for IncrementalHandlers<T, R> {
    fn clone(&self) -> Self {
        Self {
            on_insert: Arc::clone(&self.on_insert),
            on_update: Arc::clone(&self.on_update),
            on_remove: Arc::clone(&self.on_remove),
            on_replace: Arc::clone(&self.on_replace),
        }
    }
}

impl<T: 'static, R: 'static> IncrementalHandlers<T, R> {
    /// Create new incremental handlers
    pub fn new() -> Self {
        Self {
            on_insert: Arc::new(|_, _, _| {}),
            on_update: Arc::new(|_, _, _, _| {}),
            on_remove: Arc::new(|_, _, _| {}),
            on_replace: Arc::new(|_| panic!("Replace handler not implemented")),
        }
    }

    /// Set handler for insert operations
    pub fn insert(mut self, f: impl Fn(&mut R, usize, T) + Send + Sync + 'static) -> Self {
        self.on_insert = Arc::new(f);
        self
    }

    /// Set handler for update operations
    pub fn update(mut self, f: impl Fn(&mut R, usize, T, T) + Send + Sync + 'static) -> Self {
        self.on_update = Arc::new(f);
        self
    }

    /// Set handler for remove operations
    pub fn remove(mut self, f: impl Fn(&mut R, usize, T) + Send + Sync + 'static) -> Self {
        self.on_remove = Arc::new(f);
        self
    }

    /// Set handler for replace operations
    pub fn replace(mut self, f: impl Fn(&SignalVec<T>) -> R + Send + Sync + 'static) -> Self {
        self.on_replace = Arc::new(f);
        self
    }
}

impl<T: 'static, R: 'static> Default for IncrementalHandlers<T, R> {
    fn default() -> Self {
        Self::new()
    }
}

/// A computed value that incrementally updates from SignalVec changes
///
/// Instead of recomputing the entire result when the source vector changes,
/// this processes individual insert/update/remove operations efficiently.
///
/// # Example
///
/// ```rust,ignore
/// let items = signal_vec(vec![1, 2, 3, 4, 5]);
///
/// // Filter even numbers with incremental updates
/// let filtered = IncrementalComputed::new(
///     items.clone(),
///     |v| v.iter().filter(|x| *x % 2 == 0).copied().collect(),
///     IncrementalHandlers::new()
///         .insert(|result, index, value| {
///             if value % 2 == 0 {
///                 result.push(value);
///             }
///         })
///         .update(|result, index, old, new| {
///             if old % 2 == 0 && new % 2 != 0 {
///                 result.retain(|x| *x != old);
///             } else if old % 2 != 0 && new % 2 == 0 {
///                 result.push(new);
///             }
///         })
///         .remove(|result, _, value| {
///             result.retain(|x| *x != value);
///         })
///         .replace(|items| {
///             items.get().iter().filter(|x| *x % 2 == 0).copied().collect()
///         }),
/// );
///
/// items.push(6);  // Only checks if 6 is even, doesn't re-filter entire list
/// ```
pub struct IncrementalComputed<T: Clone + Send + Sync + 'static, R: Clone + Send + Sync + 'static> {
    /// Source signal vector
    source: SignalVec<T>,
    /// Cached result
    cached: Arc<Mutex<R>>,
    /// Unique ID
    id: SignalId,
}

impl<T: Clone + Send + Sync + 'static, R: Clone + Send + Sync + 'static> IncrementalComputed<T, R> {
    /// Create a new incremental computed value
    ///
    /// # Arguments
    /// * `source` - The source SignalVec
    /// * `init` - Initial computation function
    /// * `handlers` - Incremental update handlers
    pub fn new(
        source: SignalVec<T>,
        init: impl Fn(&[T]) -> R + Send + Sync + 'static,
        handlers: IncrementalHandlers<T, R>,
    ) -> Self {
        let id = SignalId::new();
        let initial_result = init(&source.get());

        let cached = Arc::new(Mutex::new(initial_result));

        // Clone handlers for use in the subscription
        let handlers_clone = handlers.clone();
        let source_clone = source.clone();

        // Subscribe to diff events from the source
        let cached_clone = cached.clone();
        source.subscribe_diff(move |diff| {
            if let Ok(mut result) = cached_clone.lock() {
                match diff {
                    super::signal_vec::VecDiff::Insert { index, value } => {
                        (handlers_clone.on_insert)(&mut result, index, value);
                    }
                    super::signal_vec::VecDiff::Update {
                        index,
                        old_value,
                        new_value,
                    } => {
                        (handlers_clone.on_update)(&mut result, index, old_value, new_value);
                    }
                    super::signal_vec::VecDiff::Remove { index, value } => {
                        (handlers_clone.on_remove)(&mut result, index, value);
                    }
                    super::signal_vec::VecDiff::Move {
                        old_index,
                        new_index,
                        value,
                    } => {
                        // For move, we simulate remove + insert
                        (handlers_clone.on_remove)(&mut result, old_index, value.clone());
                        (handlers_clone.on_insert)(&mut result, new_index, value);
                    }
                    super::signal_vec::VecDiff::Replace {
                        old_values: _,
                        new_values: _,
                    } => {
                        // Full recomputation for replace
                        let new_result = (handlers_clone.on_replace)(&source_clone);
                        *result = new_result;
                    }
                }
                // Notify dependents after incremental update
                notify_dependents(id);
            }
        });

        Self { source, cached, id }
    }

    /// Get the current cached value
    pub fn get(&self) -> R {
        lock_or_recover(&self.cached).clone()
    }

    /// Get the inner cached value (zero-copy with guard)
    pub fn read(&self) -> std::sync::MutexGuard<'_, R> {
        lock_or_recover(&self.cached)
    }

    /// Get the source SignalVec
    pub fn source(&self) -> &SignalVec<T> {
        &self.source
    }

    /// Invalidate cache (triggers recomputation on next get)
    pub fn invalidate(&self) {
        notify_dependents(self.id);
    }

    /// Get the ID
    pub fn id(&self) -> SignalId {
        self.id
    }
}

impl<T: Clone + Send + Sync + 'static, R: Clone + Send + Sync + 'static> Clone
    for IncrementalComputed<T, R>
{
    fn clone(&self) -> Self {
        Self {
            source: self.source.clone(),
            cached: Arc::clone(&self.cached),
            id: self.id,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_incremental_handlers_new() {
        let _handlers: IncrementalHandlers<i32, Vec<i32>> = IncrementalHandlers::new();
        // Just verify it compiles
        assert!(true);
    }

    #[test]
    fn test_incremental_handlers_builder() {
        let _handlers = IncrementalHandlers::<i32, Vec<i32>>::new()
            .insert(|result, index, value| {
                result.insert(index, value);
            })
            .update(|_result, _index, _old, _new| {
                // Update handler
            })
            .remove(|_result, _index, _value| {
                // Remove handler
            })
            .replace(|_source: &SignalVec<i32>| vec![]);

        // Verify handlers are set
        assert!(true);
    }

    #[test]
    fn test_incremental_computed_new() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3, 4, 5]);

        let filtered: IncrementalComputed<i32, Vec<i32>> = IncrementalComputed::new(
            items.clone(),
            |v| v.iter().filter(|x| *x % 2 == 0).copied().collect(),
            IncrementalHandlers::new(),
        );

        let result = filtered.get();
        assert_eq!(result, vec![2, 4]);
    }

    #[test]
    fn test_incremental_handlers_default() {
        let _handlers: IncrementalHandlers<i32, Vec<i32>> = IncrementalHandlers::default();
        // Just verify it works
        assert!(true);
    }

    #[test]
    fn test_incremental_handlers_clone() {
        let handlers = IncrementalHandlers::<i32, Vec<i32>>::new()
            .insert(|_, _, _| {})
            .update(|_, _, _, _| {})
            .remove(|_, _, _| {})
            .replace(|_| vec![]);

        let _cloned = handlers.clone();
        // Verify cloning works
    }

    #[test]
    fn test_incremental_computed_get() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);

        let computed =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        assert_eq!(computed.get(), 3);
    }

    #[test]
    fn test_incremental_computed_source() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);

        let computed =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        // Verify we can access the source
        let _source_ref = computed.source();
    }

    #[test]
    fn test_incremental_computed_clone() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);

        let computed1 =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        let computed2 = computed1.clone();

        // Both should return the same result
        assert_eq!(computed1.get(), 3);
        assert_eq!(computed2.get(), 3);
    }

    #[test]
    fn test_incremental_computed_invalidate() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);

        let computed =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        computed.invalidate();
        // Just verify it doesn't panic
    }

    #[test]
    fn test_incremental_computed_id() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);

        let computed1 =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        let computed2 =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        // IDs should be different
        assert_ne!(computed1.id(), computed2.id());
    }

    #[test]
    fn test_incremental_computed_read() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);

        let computed =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        // Read should work
        let guard = computed.read();
        assert_eq!(*guard, 3);
    }

    #[test]
    fn test_incremental_handlers_with_string() {
        use crate::reactive::signal_vec;

        let _items = signal_vec(vec!["a", "b", "c"]);

        let _handlers = IncrementalHandlers::<&str, String>::new()
            .insert(|result, _, value| {
                result.push_str(value);
            })
            .update(|result, _, old, new| {
                *result = result.replace(old, new);
            })
            .remove(|result, _, value| {
                *result = result.replace(value, "");
            })
            .replace(|source| source.get().join(","));

        // Just verify it compiles and works
        assert!(true);
    }

    #[test]
    fn test_incremental_computed_with_vec_result() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);

        let computed =
            IncrementalComputed::new(items.clone(), |v| v.to_vec(), IncrementalHandlers::new());

        assert_eq!(computed.get(), vec![1, 2, 3]);
    }

    #[test]
    fn test_incremental_computed_with_count_result() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3, 4, 5]);

        let computed =
            IncrementalComputed::new(items.clone(), |v| v.len(), IncrementalHandlers::new());

        assert_eq!(computed.get(), 5);
    }

    #[test]
    fn test_incremental_computed_with_sum_result() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3, 4, 5]);

        let computed = IncrementalComputed::new(
            items.clone(),
            |v| v.iter().sum::<i32>(),
            IncrementalHandlers::new(),
        );

        assert_eq!(computed.get(), 15);
    }

    #[test]
    fn test_incremental_handlers_insert_method() {
        let mut result = Vec::new();
        let handlers = IncrementalHandlers::<i32, Vec<i32>>::new().insert(|res, index, value| {
            res.insert(index, value);
        });

        // Call the insert handler
        (handlers.on_insert)(&mut result, 0, 42);
        assert_eq!(result, vec![42]);
    }

    #[test]
    fn test_incremental_handlers_update_method() {
        let mut result = vec![1, 2, 3];
        let handlers =
            IncrementalHandlers::<i32, Vec<i32>>::new().update(|res, index, _old, new| {
                res[index] = new;
            });

        // Call the update handler
        (handlers.on_update)(&mut result, 1, 2, 20);
        assert_eq!(result, vec![1, 20, 3]);
    }

    #[test]
    fn test_incremental_handlers_remove_method() {
        let mut result = vec![1, 2, 3];
        let handlers = IncrementalHandlers::<i32, Vec<i32>>::new().remove(|res, index, _value| {
            res.remove(index);
        });

        // Call the remove handler
        (handlers.on_remove)(&mut result, 1, 2);
        assert_eq!(result, vec![1, 3]);
    }

    #[test]
    fn test_incremental_handlers_replace_method() {
        use crate::reactive::signal_vec;

        let items = signal_vec(vec![1, 2, 3]);
        let handlers =
            IncrementalHandlers::<i32, usize>::new().replace(|source| source.get().len());

        // Call the replace handler
        let result = (handlers.on_replace)(&items);
        assert_eq!(result, 3);
    }
}