sinomo-ui-reactive 0.7.0

Architecture-neutral observable signals and selectors for SUI.
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
use std::{
    fmt,
    future::Future,
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
};

use crate::{Changed, Observable, Observer, Selector, Signal, SourceId, Subscription};

/// Lifecycle of a background operation observed through a [`Task`].
#[derive(Debug, Clone, PartialEq)]
pub enum TaskState<T, E = String> {
    Idle,
    /// Running. `progress` is `None` while indeterminate, otherwise in `0..=1`.
    /// `previous` holds the last result while a [`Task::refresh`] runs.
    Loading {
        progress: Option<f32>,
        previous: Option<T>,
    },
    Ready(T),
    Failed(E),
}

impl<T, E> TaskState<T, E> {
    pub fn is_idle(&self) -> bool {
        matches!(self, Self::Idle)
    }

    pub fn is_loading(&self) -> bool {
        matches!(self, Self::Loading { .. })
    }

    /// Progress while loading; `None` when indeterminate or not loading.
    pub fn progress(&self) -> Option<f32> {
        match self {
            Self::Loading { progress, .. } => *progress,
            _ => None,
        }
    }

    pub fn ready(&self) -> Option<&T> {
        match self {
            Self::Ready(value) => Some(value),
            _ => None,
        }
    }

    /// The current result, or while refreshing, the previous one.
    pub fn latest(&self) -> Option<&T> {
        match self {
            Self::Ready(value) => Some(value),
            Self::Loading { previous, .. } => previous.as_ref(),
            _ => None,
        }
    }

    pub fn error(&self) -> Option<&E> {
        match self {
            Self::Failed(error) => Some(error),
            _ => None,
        }
    }
}

/// Observable state of a background operation: a [`Signal`] holding a
/// [`TaskState`], plus a generation counter that keeps stale workers from
/// overwriting newer results.
///
/// [`start`](Self::start) moves to `Loading` and returns a [`TaskHandle`] for
/// the worker; [`refresh`](Self::refresh) does the same but keeps the last
/// result visible. Starting again, [`reset`](Self::reset), or setting a result
/// directly retires every earlier handle, whose later reports are ignored.
///
/// For async work, [`TaskHandle::run`] wraps a future that you spawn on any
/// executor; dropping that future cancels the run.
///
/// ```ignore
/// let thumbnails: Task<Arc<Vec<Image>>> = Task::named("thumbnails");
/// let handle = thumbnails.start();
/// std::thread::spawn(move || {
///     for (index, path) in paths.iter().enumerate() {
///         handle.set_progress(index as f32 / paths.len() as f32);
///         // ...
///     }
///     handle.complete(load_all(&paths).map(Arc::new));
/// });
/// ```
///
/// Writes need no `Clone` or `PartialEq` on `T` or `E`. Observers run on the
/// reporting thread, as with any [`Signal`]. Wrap large results in `Arc` so
/// [`state`](Self::state) stays cheap, or read with [`with`](Self::with).
pub struct Task<T, E = String> {
    state: Signal<TaskState<T, E>>,
    // Changed only while `state` is write-locked, so a check and the write it
    // guards are atomic with respect to other transitions.
    generation: Arc<AtomicU64>,
}

impl<T, E> Clone for Task<T, E> {
    fn clone(&self) -> Self {
        Self {
            state: self.state.clone(),
            generation: Arc::clone(&self.generation),
        }
    }
}

impl<T, E> fmt::Debug for Task<T, E>
where
    T: fmt::Debug,
    E: fmt::Debug,
{
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Task")
            .field("state", &self.state)
            .field("generation", &self.generation.load(Ordering::Acquire))
            .finish()
    }
}

impl<T, E> Default for Task<T, E> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T, E> Task<T, E> {
    pub fn new() -> Self {
        Self::named("Task")
    }

    pub fn named(name: impl Into<Arc<str>>) -> Self {
        Self {
            state: Signal::named(name, TaskState::Idle),
            generation: Arc::new(AtomicU64::new(0)),
        }
    }

    /// Enter `Loading` with indeterminate progress and return the handle the
    /// worker reports through. Retires earlier handles and drops any result.
    pub fn start(&self) -> TaskHandle<T, E> {
        self.begin(false)
    }

    /// Like [`start`](Self::start), but keep the current result as
    /// `Loading { previous }` so the UI can show it until the new one lands.
    /// Cancelling the run restores it as `Ready`.
    pub fn refresh(&self) -> TaskHandle<T, E> {
        self.begin(true)
    }

    /// Return to `Idle`, retiring every outstanding handle.
    pub fn reset(&self) -> bool {
        self.retire_and_set(TaskState::Idle)
    }

    /// Store a result directly, retiring every outstanding handle.
    pub fn finish(&self, value: T) -> bool {
        self.retire_and_set(TaskState::Ready(value))
    }

    /// Store a failure directly, retiring every outstanding handle.
    pub fn fail(&self, error: E) -> bool {
        self.retire_and_set(TaskState::Failed(error))
    }

    pub fn complete(&self, result: Result<T, E>) -> bool {
        self.retire_and_set(result.into())
    }

    pub fn state(&self) -> TaskState<T, E>
    where
        T: Clone,
        E: Clone,
    {
        self.state.get()
    }

    /// Read the state by reference without cloning a result.
    pub fn with<R>(&self, read: impl FnOnce(&TaskState<T, E>) -> R) -> R {
        self.state.with(read)
    }

    pub fn is_loading(&self) -> bool {
        self.with(TaskState::is_loading)
    }

    pub fn progress(&self) -> Option<f32> {
        self.with(TaskState::progress)
    }

    /// The underlying signal, for APIs that take a [`Signal`].
    pub fn signal(&self) -> &Signal<TaskState<T, E>> {
        &self.state
    }

    pub fn source_id(&self) -> SourceId {
        self.state.source_id()
    }

    pub fn version(&self) -> u64 {
        self.state.version()
    }

    pub fn subscribe(&self, observer: Observer) -> Subscription {
        self.state.subscribe(observer)
    }

    /// A future that resolves at the next state change.
    pub fn changed(&self) -> Changed {
        self.state.changed()
    }

    pub fn select<U>(
        &self,
        select: impl Fn(&TaskState<T, E>) -> U + Send + Sync + 'static,
    ) -> Selector<Signal<TaskState<T, E>>, TaskState<T, E>, U>
    where
        T: Clone + Send + Sync + 'static,
        E: Clone + Send + Sync + 'static,
        U: Clone + PartialEq + Send + Sync + 'static,
    {
        self.state.select_named(self.state.source_name(), select)
    }

    pub fn select_named<U>(
        &self,
        name: impl Into<Arc<str>>,
        select: impl Fn(&TaskState<T, E>) -> U + Send + Sync + 'static,
    ) -> Selector<Signal<TaskState<T, E>>, TaskState<T, E>, U>
    where
        T: Clone + Send + Sync + 'static,
        E: Clone + Send + Sync + 'static,
        U: Clone + PartialEq + Send + Sync + 'static,
    {
        self.state.select_named(name, select)
    }

    fn begin(&self, keep_previous: bool) -> TaskHandle<T, E> {
        let mut generation = 0;
        self.state.modify(|state| {
            generation = self.generation.fetch_add(1, Ordering::AcqRel) + 1;
            let unchanged = matches!(
                state,
                TaskState::Loading {
                    progress: None,
                    previous: None
                }
            );
            let previous = match std::mem::replace(state, TaskState::Idle) {
                TaskState::Ready(value) if keep_previous => Some(value),
                TaskState::Loading { previous, .. } if keep_previous => previous,
                _ => None,
            };
            let changed = !(unchanged && previous.is_none());
            *state = TaskState::Loading {
                progress: None,
                previous,
            };
            changed
        });
        TaskHandle {
            task: self.clone(),
            generation,
        }
    }

    fn retire_and_set(&self, next: TaskState<T, E>) -> bool {
        self.state.modify(|state| {
            self.generation.fetch_add(1, Ordering::AcqRel);
            let changed = !(state.is_idle() && next.is_idle());
            *state = next;
            changed
        })
    }

    /// Apply `write` only while `generation` is still current.
    fn write_if_current(
        &self,
        generation: u64,
        retire: bool,
        write: impl FnOnce(&mut TaskState<T, E>) -> bool,
    ) -> bool {
        self.state.modify(|state| {
            if self.generation.load(Ordering::Acquire) != generation {
                return false;
            }
            if retire {
                self.generation.fetch_add(1, Ordering::AcqRel);
            }
            write(state)
        })
    }
}

impl<T, E> From<Result<T, E>> for TaskState<T, E> {
    fn from(result: Result<T, E>) -> Self {
        match result {
            Ok(value) => Self::Ready(value),
            Err(error) => Self::Failed(error),
        }
    }
}

impl<T, E> Observable<TaskState<T, E>> for Task<T, E>
where
    T: Clone + 'static,
    E: Clone + 'static,
{
    fn source_id(&self) -> SourceId {
        self.state.source_id()
    }

    fn source_name(&self) -> Arc<str> {
        self.state.source_name()
    }

    fn get(&self) -> TaskState<T, E> {
        self.state.get()
    }

    fn read(&self, reader: &mut dyn FnMut(&TaskState<T, E>)) {
        self.state.with(|state| reader(state));
    }

    fn value_version(&self) -> Option<u64> {
        Some(self.state.version())
    }

    fn subscribe(&self, observer: Observer) -> Subscription {
        self.state.subscribe(observer)
    }
}

/// A worker's reporting channel for one run of a [`Task`].
///
/// Every report is ignored once the run is retired: after the task is started
/// again, reset, or completed or cancelled by this or another handle. Dropping
/// a handle without completing leaves the task `Loading`; call
/// [`cancel`](Self::cancel) to abandon a run.
pub struct TaskHandle<T, E = String> {
    task: Task<T, E>,
    generation: u64,
}

impl<T, E> Clone for TaskHandle<T, E> {
    fn clone(&self) -> Self {
        Self {
            task: self.task.clone(),
            generation: self.generation,
        }
    }
}

impl<T, E> fmt::Debug for TaskHandle<T, E> {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("TaskHandle")
            .field("source_id", &self.task.source_id())
            .field("generation", &self.generation)
            .finish()
    }
}

impl<T, E> TaskHandle<T, E> {
    /// Whether this run is still the task's current one. A worker may poll
    /// this to stop early after being superseded.
    pub fn is_current(&self) -> bool {
        self.task.generation.load(Ordering::Acquire) == self.generation
    }

    /// Report determinate progress, clamped to `0..=1`. Repeating the current
    /// value does not notify. NaN reports indeterminate progress.
    pub fn set_progress(&self, progress: f32) -> bool {
        let progress = (!progress.is_nan()).then(|| progress.clamp(0.0, 1.0));
        self.task
            .write_if_current(self.generation, false, |state| match state {
                TaskState::Loading {
                    progress: current, ..
                } => {
                    let changed = *current != progress;
                    *current = progress;
                    changed
                }
                _ => {
                    *state = TaskState::Loading {
                        progress,
                        previous: None,
                    };
                    true
                }
            })
    }

    /// Abandon the run: back to `Idle`, or to the previous result after a
    /// [`Task::refresh`]. Ignored if the run was already retired.
    pub fn cancel(self) -> bool {
        self.task.write_if_current(self.generation, true, |state| {
            *state = match std::mem::replace(state, TaskState::Idle) {
                TaskState::Loading {
                    previous: Some(value),
                    ..
                } => TaskState::Ready(value),
                _ => TaskState::Idle,
            };
            true
        })
    }

    /// Wrap `work` so its output completes this run. Spawn the returned
    /// future on any executor; it resolves to whether the result was stored.
    /// Dropping it before `work` finishes [cancels](Self::cancel) the run.
    ///
    /// ```ignore
    /// let handle = results.refresh();
    /// let progress = handle.clone();
    /// wasm_bindgen_futures::spawn_local(async move {
    ///     handle
    ///         .run(async move {
    ///             progress.set_progress(0.5);
    ///             fetch_results().await
    ///         })
    ///         .await;
    /// });
    /// ```
    pub fn run<Work>(self, work: Work) -> impl Future<Output = bool> + use<T, E, Work>
    where
        Work: Future<Output = Result<T, E>>,
    {
        let mut run = CancelOnDrop(Some(self));
        async move {
            let result = work.await;
            run.0.take().is_some_and(|handle| handle.complete(result))
        }
    }

    pub fn finish(self, value: T) -> bool {
        self.complete(Ok(value))
    }

    pub fn fail(self, error: E) -> bool {
        self.complete(Err(error))
    }

    /// Store the run's result unless the run was retired. Returns whether it
    /// was stored.
    pub fn complete(self, result: Result<T, E>) -> bool {
        self.task.write_if_current(self.generation, true, |state| {
            *state = result.into();
            true
        })
    }

    pub fn task(&self) -> &Task<T, E> {
        &self.task
    }
}

/// Cancels the run if [`TaskHandle::run`]'s future is dropped unfinished.
struct CancelOnDrop<T, E>(Option<TaskHandle<T, E>>);

impl<T, E> Drop for CancelOnDrop<T, E> {
    fn drop(&mut self) {
        if let Some(handle) = self.0.take() {
            handle.cancel();
        }
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::AtomicUsize;

    use super::*;

    fn count_notifications<T, E>(task: &Task<T, E>) -> (Arc<AtomicUsize>, Subscription) {
        let notifications = Arc::new(AtomicUsize::new(0));
        let count = Arc::clone(&notifications);
        let subscription = task.subscribe(Observer::new(move |_| {
            count.fetch_add(1, Ordering::Relaxed);
        }));
        (notifications, subscription)
    }

    #[test]
    fn task_reports_progress_and_result_from_a_worker() {
        let task: Task<u32> = Task::named("load");
        let (notifications, _subscription) = count_notifications(&task);
        let handle = task.start();
        assert_eq!(
            task.state(),
            TaskState::Loading {
                progress: None,
                previous: None
            }
        );

        std::thread::spawn(move || {
            assert!(handle.set_progress(0.5));
            assert!(!handle.set_progress(0.5), "equal progress is deduplicated");
            assert!(handle.set_progress(2.0));
            assert!(handle.finish(7));
        })
        .join()
        .unwrap();

        assert_eq!(task.state(), TaskState::Ready(7));
        // start, 0.5, 1.0 (clamped), ready
        assert_eq!(notifications.load(Ordering::Relaxed), 4);
    }

    #[test]
    fn restarting_retires_the_previous_handle() {
        let task: Task<&'static str> = Task::new();
        let stale = task.start();
        let current = task.start();

        assert!(!stale.is_current());
        assert!(!stale.set_progress(0.9));
        assert!(!stale.clone().finish("stale"));
        assert!(task.is_loading());

        let progress = current.clone();
        assert!(current.finish("fresh"));
        assert!(!progress.set_progress(0.1), "completion retires clones too");
        assert_eq!(task.state(), TaskState::Ready("fresh"));
    }

    #[test]
    fn reset_cancels_a_running_worker() {
        let task: Task<u32> = Task::new();
        let handle = task.start();
        assert!(task.reset());
        assert!(!task.reset(), "already idle");
        assert!(!handle.fail("late".to_string()));
        assert!(task.with(TaskState::is_idle));
    }

    #[test]
    fn task_state_needs_no_clone_or_equality_to_write() {
        struct Opaque;
        struct OpaqueError;

        let task: Task<Opaque, OpaqueError> = Task::new();
        let handle = task.start();
        assert!(handle.complete(Err(OpaqueError)));
        assert!(task.with(|state| state.error().is_some()));
        assert!(task.finish(Opaque));
        assert!(task.with(|state| state.ready().is_some()));
    }

    #[test]
    fn selector_tracks_task_progress() {
        let task: Task<u32> = Task::new();
        let percent = task.select(|state| state.progress().map(|p| (p * 100.0) as u32));
        let handle = task.start();
        handle.set_progress(0.25);
        assert_eq!(percent.get(), Some(25));
    }

    #[test]
    fn refresh_keeps_the_previous_result_until_replaced_or_cancelled() {
        let task: Task<&'static str> = Task::new();
        task.finish("first");

        let handle = task.refresh();
        assert_eq!(task.with(|state| state.latest().copied()), Some("first"));
        assert!(handle.set_progress(0.5));
        assert_eq!(task.progress(), Some(0.5));
        assert_eq!(task.with(|state| state.latest().copied()), Some("first"));
        assert!(handle.cancel());
        assert_eq!(task.state(), TaskState::Ready("first"));

        let handle = task.refresh();
        assert!(handle.finish("second"));
        assert_eq!(task.state(), TaskState::Ready("second"));

        // A plain start drops the result.
        task.start();
        assert_eq!(task.with(|state| state.latest().copied()), None);
    }

    /// Poll a future to completion on this thread, parking between polls.
    fn block_on<F: Future>(future: F) -> F::Output {
        struct ThreadWaker(std::thread::Thread);
        impl std::task::Wake for ThreadWaker {
            fn wake(self: Arc<Self>) {
                self.0.unpark();
            }
        }
        let waker = std::task::Waker::from(Arc::new(ThreadWaker(std::thread::current())));
        let mut context = std::task::Context::from_waker(&waker);
        let mut future = std::pin::pin!(future);
        loop {
            if let std::task::Poll::Ready(output) = future.as_mut().poll(&mut context) {
                return output;
            }
            std::thread::park();
        }
    }

    #[test]
    fn run_completes_the_task_from_a_future() {
        let task: Task<u32> = Task::new();
        let handle = task.start();
        let progress = handle.clone();
        let stored = block_on(handle.run(async move {
            progress.set_progress(0.5);
            Ok(42)
        }));
        assert!(stored);
        assert_eq!(task.state(), TaskState::Ready(42));
    }

    #[test]
    fn dropping_a_run_future_cancels_the_run() {
        let task: Task<u32> = Task::new();
        task.finish(1);
        let run = task.refresh().run(std::future::pending());
        assert!(task.is_loading());
        drop(run);
        assert_eq!(task.state(), TaskState::Ready(1));

        // A superseded run's drop leaves the newer run alone.
        let stale = task.start().run(std::future::pending());
        let current = task.start();
        drop(stale);
        assert!(task.is_loading());
        assert!(current.finish(2));
    }

    #[test]
    fn changed_resolves_when_a_worker_finishes() {
        let task: Task<u32> = Task::new();
        let handle = task.start();
        let changed = task.changed();
        let worker = std::thread::spawn(move || {
            std::thread::sleep(std::time::Duration::from_millis(20));
            handle.finish(5);
        });
        let change = block_on(changed);
        assert_eq!(change.source_id, task.source_id());
        assert_eq!(task.state(), TaskState::Ready(5));
        worker.join().unwrap();
    }
}