tears 0.10.2

A simple and elegant framework for building TUI applications using The Elm Architecture (TEA)
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
//! `Subscription`, `SubscriptionSource`, and `SubscriptionId`, split out from
//! `subscription.rs` so the parent module can stay `pub` (hosting the
//! `http`/`mock`/`signal`/`terminal`/`time`/`websocket` submodules) while
//! closing the `subscription::{Subscription, SubscriptionId,
//! SubscriptionSource}` paths. See `runtime::frame_rate` for the same
//! pattern applied to `Runtime`'s scheduling input.

use std::any::{TypeId, type_name};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::panic::AssertUnwindSafe;

use futures::{StreamExt, stream::BoxStream};

use crate::structural_key::{ScopePath, StructuralKey};

/// A subscription represents an ongoing source of messages.
///
/// Subscriptions are used to listen to external events that occur over time, such as:
/// - Keyboard and mouse input
/// - Timer ticks
/// - WebSocket messages
/// - File system changes
/// - Network events
///
/// Unlike commands which are one-time operations, subscriptions continue to produce
/// messages until they are cancelled.
///
/// # Example
///
/// ```
/// use std::num::NonZeroU64;
/// use tears::Subscription;
/// use tears::subscription::time::Timer;
///
/// enum Message {
///     Tick,
/// }
///
/// // Create a subscription that sends a message every second
/// let sub = Subscription::new(Timer::new(NonZeroU64::new(1000).expect("non-zero")))
///     .map(|_| Message::Tick);
/// ```
pub struct Subscription<Msg: 'static> {
    pub(super) id: SubscriptionId,
    pub(super) spawn: Box<dyn FnOnce() -> BoxStream<'static, Msg> + Send>,
}

impl<Msg: 'static> Subscription<Msg> {
    /// Create a new subscription from a type implementing [`SubscriptionSource`].
    ///
    /// # Examples
    ///
    /// ```
    /// use std::num::NonZeroU64;
    /// use tears::Subscription;
    /// use tears::subscription::time::Timer;
    ///
    /// let sub = Subscription::new(Timer::new(NonZeroU64::new(1000).expect("non-zero")));
    /// ```
    #[must_use]
    pub fn new<Source>(source: Source) -> Self
    where
        Source: SubscriptionSource<Output = Msg> + 'static,
    {
        let id = SubscriptionId::from_source::<Source>(source.key());

        Self {
            id,
            spawn: Box::new(move || source.stream().boxed()),
        }
    }

    /// Transform the messages produced by this subscription.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::num::NonZeroU64;
    /// use tears::Subscription;
    /// use tears::subscription::time::Timer;
    ///
    /// enum AppMessage { TimerTick }
    ///
    /// let sub = Subscription::new(Timer::new(NonZeroU64::new(1000).expect("non-zero")))
    ///     .map(|_| AppMessage::TimerTick);
    /// ```
    #[must_use]
    pub fn map<F, NewMsg>(self, f: F) -> Subscription<NewMsg>
    where
        F: Fn(Msg) -> NewMsg + Send + 'static,
        Msg: 'static,
        NewMsg: 'static,
    {
        let spawn = self.spawn;
        Subscription {
            id: self.id,
            spawn: Box::new(move || {
                let stream = spawn();
                stream.map(f).boxed()
            }),
        }
    }

    /// Qualifies this subscription's identity with one structural scope
    /// segment, expressing that this instance belongs to a distinct child
    /// composition boundary (see RFC 0005 section 4.2).
    ///
    /// Chaining `scoped` calls nests segments in call order: the last call
    /// becomes the outermost segment. Reversing the order of two unequal
    /// scope values produces a different identity, so
    /// `sub.scoped(a).scoped(b)` and `sub.scoped(b).scoped(a)` are distinct
    /// subscriptions when `a != b`. `scoped` and [`Subscription::map`]
    /// commute: applying them in either order around the same call produces
    /// the same identity and output.
    ///
    /// Applying the same scope to two composed child instances that reuse
    /// the same local source and key still aliases them; give each instance
    /// its own scope value (for example a per-instance id) to keep them
    /// independent.
    ///
    /// # Examples
    ///
    /// ```
    /// use std::num::NonZeroU64;
    /// use tears::Subscription;
    /// use tears::subscription::time::Timer;
    ///
    /// enum Message {
    ///     Tick(u32),
    /// }
    ///
    /// let pane_id = 1;
    /// let sub = Subscription::new(Timer::new(NonZeroU64::new(1000).expect("non-zero")))
    ///     .map(move |_| Message::Tick(pane_id))
    ///     .scoped(pane_id);
    /// ```
    #[must_use = "scoped returns a modified subscription and does not mutate in place"]
    pub fn scoped<Scope>(mut self, scope: Scope) -> Self
    where
        Scope: Eq + Hash + Send + Sync + 'static,
    {
        self.id.scope = AssertUnwindSafe(self.id.scope.appended(scope));
        self
    }

    /// The subscription's declared identity, read without starting its source.
    /// Used by [`TestStore::subscription_ids`](crate::testing::TestStore::subscription_ids).
    pub(crate) const fn id(&self) -> &SubscriptionId {
        &self.id
    }
}

impl<A: SubscriptionSource<Output = Msg> + 'static, Msg> From<A> for Subscription<Msg> {
    fn from(value: A) -> Self {
        Self::new(value)
    }
}

/// Trait for types that can be used as subscription sources.
///
/// # Example
///
/// ```
/// use tears::SubscriptionSource;
/// use tears::BoxStream;
/// use futures::{StreamExt, stream};
///
/// struct MySubscription {
///     interval_ms: u64,
/// }
///
/// impl SubscriptionSource for MySubscription {
///     type Output = ();
///     type Key = u64;
///
///     fn stream(&self) -> BoxStream<'static, Self::Output> {
///         stream::empty().boxed()
///     }
///
///     fn key(&self) -> Self::Key {
///         self.interval_ms
///     }
/// }
/// ```
pub trait SubscriptionSource: Send {
    /// The type of messages this subscription produces.
    type Output;

    /// The owned structural key for one subscription lifecycle.
    ///
    /// This key must be stable across equivalent evaluations of a source. A
    /// changing key expresses a new lifecycle, causing the old subscription to
    /// stop and a new one to start during reconciliation.
    type Key: Eq + Hash + Send + Sync + 'static;

    /// Create the stream of messages for this subscription.
    fn stream(&self) -> BoxStream<'static, Self::Output>;

    /// Get the owned structural key for this subscription.
    ///
    /// The framework combines this value with the concrete source type when it
    /// constructs the opaque subscription identity.
    ///
    /// This method must return equal keys for the same logical source identity
    /// across calls, including when fresh source values are constructed by
    /// successive [`Application::subscriptions`](crate::Application::subscriptions)
    /// evaluations. A per-instance source must generate its instance token
    /// once, store it, and return the stored token here. Generating a fresh key
    /// on each evaluation aborts and respawns the subscription during
    /// reconciliation.
    fn key(&self) -> Self::Key;
}

/// A unique identifier for a subscription.
///
/// Two subscriptions with the same ID are considered identical.
///
/// IDs compare their concrete source type and original structural key. Hashing
/// is used only for indexing and never makes unequal keys equal.
pub struct SubscriptionId {
    pub(super) source_type_id: TypeId,
    source_type_name: &'static str,
    key: AssertUnwindSafe<StructuralKey>,
    scope: AssertUnwindSafe<ScopePath>,
}

impl Clone for SubscriptionId {
    fn clone(&self) -> Self {
        Self {
            source_type_id: self.source_type_id,
            source_type_name: self.source_type_name,
            key: AssertUnwindSafe(self.key.0.clone()),
            scope: AssertUnwindSafe(self.scope.0.clone()),
        }
    }
}

impl SubscriptionId {
    fn from_source<Source>(key: Source::Key) -> Self
    where
        Source: SubscriptionSource + 'static,
    {
        Self {
            source_type_id: TypeId::of::<Source>(),
            source_type_name: type_name::<Source>(),
            key: AssertUnwindSafe(StructuralKey::new(key)),
            scope: AssertUnwindSafe(ScopePath::empty()),
        }
    }
}

impl fmt::Debug for SubscriptionId {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("SubscriptionId")
            .field("source", &self.source_type_name)
            .field("key", &self.key.0.type_name())
            .finish_non_exhaustive()
    }
}

impl PartialEq for SubscriptionId {
    fn eq(&self, other: &Self) -> bool {
        // One concrete source type has exactly one associated `Key` type, so
        // the source namespace already fixes the erased key type.
        self.source_type_id == other.source_type_id
            && self.key.0.value_eq(&other.key.0)
            && self.scope.0 == other.scope.0
    }
}

impl Eq for SubscriptionId {}

impl Hash for SubscriptionId {
    fn hash<H: Hasher>(&self, state: &mut H) {
        // Hash the source namespace once; its associated `Key` type does not
        // need a second type namespace in this identity.
        self.source_type_id.hash(state);
        self.key.0.hash_value(state);
        self.scope.0.hash(state);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::hash_map::DefaultHasher;
    use std::panic::{RefUnwindSafe, UnwindSafe};

    use futures::{StreamExt, stream};

    struct SourceA(u8);
    struct SourceB(u8);

    impl SubscriptionSource for SourceA {
        type Output = ();
        type Key = u8;

        fn stream(&self) -> BoxStream<'static, Self::Output> {
            stream::empty().boxed()
        }

        fn key(&self) -> Self::Key {
            self.0
        }
    }

    impl SubscriptionSource for SourceB {
        type Output = ();
        type Key = u8;

        fn stream(&self) -> BoxStream<'static, Self::Output> {
            stream::empty().boxed()
        }

        fn key(&self) -> Self::Key {
            self.0
        }
    }

    #[derive(Eq, PartialEq)]
    struct Collision(u8);

    impl Hash for Collision {
        fn hash<H: Hasher>(&self, state: &mut H) {
            0_u8.hash(state);
        }
    }

    struct CollisionSource(Collision);

    impl SubscriptionSource for CollisionSource {
        type Output = ();
        type Key = Collision;

        fn stream(&self) -> BoxStream<'static, Self::Output> {
            stream::empty().boxed()
        }

        fn key(&self) -> Self::Key {
            Collision(self.0.0)
        }
    }

    fn hash(id: &SubscriptionId) -> u64 {
        let mut hasher = DefaultHasher::new();
        id.hash(&mut hasher);
        hasher.finish()
    }

    #[test]
    fn subscription_ids_are_structural_and_source_namespaced() {
        let first = Subscription::new(SourceA(1)).id;
        let equal = Subscription::new(SourceA(1)).id;
        let different_key = Subscription::new(SourceA(2)).id;
        let different_source = Subscription::new(SourceB(1)).id;

        assert_eq!(first, equal);
        assert_ne!(first, different_key);
        assert_ne!(first, different_source);
        assert_eq!(hash(&first), hash(&equal));
        assert_eq!(first, first.clone());
    }

    #[test]
    fn hash_collisions_do_not_make_subscription_ids_equal() {
        let first = Subscription::new(CollisionSource(Collision(1))).id;
        let second = Subscription::new(CollisionSource(Collision(2))).id;

        assert_eq!(hash(&first), hash(&second));
        assert_ne!(first, second);
    }

    #[test]
    fn debug_only_reports_type_names() {
        let first = format!("{:?}", Subscription::new(SourceA(1)).id);
        let second = format!("{:?}", Subscription::new(SourceA(2)).id);

        assert_eq!(first, second);
        assert!(first.contains("SourceA"));
        assert!(!first.contains('1'));
    }

    #[test]
    fn subscription_id_preserves_marker_traits() {
        fn assert_traits<T: Send + Sync + UnwindSafe + RefUnwindSafe>() {}
        assert_traits::<SubscriptionId>();
    }

    #[test]
    fn unscoped_tuple_local_key_does_not_alias_a_scoped_identity() {
        struct TupleKeyedSource;

        impl SubscriptionSource for TupleKeyedSource {
            type Output = ();
            type Key = (&'static str, u8);

            fn stream(&self) -> BoxStream<'static, Self::Output> {
                stream::empty().boxed()
            }

            fn key(&self) -> Self::Key {
                ("pane-1", 1)
            }
        }

        let tupled = Subscription::new(TupleKeyedSource).id;
        let scoped = Subscription::new(SourceA(1)).scoped("pane-1").id;

        assert_ne!(tupled, scoped);
    }

    #[test]
    fn scoped_makes_independent_child_instances_distinct() {
        let first = Subscription::new(SourceA(1)).scoped("pane-1").id;
        let second = Subscription::new(SourceA(1)).scoped("pane-2").id;

        assert_ne!(first, second);
    }

    #[test]
    fn scoped_with_equal_scope_and_local_key_is_equal() {
        let first = Subscription::new(SourceA(1)).scoped("pane-1").id;
        let second = Subscription::new(SourceA(1)).scoped("pane-1").id;

        assert_eq!(first, second);
        assert_eq!(hash(&first), hash(&second));
    }

    #[test]
    fn scoped_differs_from_unscoped() {
        let unscoped = Subscription::new(SourceA(1)).id;
        let scoped = Subscription::new(SourceA(1)).scoped("pane-1").id;

        assert_ne!(unscoped, scoped);
    }

    #[test]
    fn scope_segment_type_differences_affect_equality() {
        let as_str = Subscription::new(SourceA(1)).scoped("1").id;
        let as_u32 = Subscription::new(SourceA(1)).scoped(1_u32).id;

        assert_ne!(as_str, as_u32);
    }

    #[test]
    fn reversing_two_unequal_scope_segments_changes_identity() {
        let forward = Subscription::new(SourceA(1))
            .scoped("inner")
            .scoped("outer")
            .id;
        let backward = Subscription::new(SourceA(1))
            .scoped("outer")
            .scoped("inner")
            .id;

        assert_ne!(forward, backward);
    }

    #[test]
    fn reversing_two_equal_scope_segments_preserves_identity() {
        let forward = Subscription::new(SourceA(1))
            .scoped("pane")
            .scoped("pane")
            .id;
        let backward = Subscription::new(SourceA(1))
            .scoped("pane")
            .scoped("pane")
            .id;

        assert_eq!(forward, backward);
    }

    #[test]
    fn map_and_scoped_placement_are_equivalent() {
        let before = Subscription::new(SourceA(1))
            .map(|()| 1)
            .scoped("pane-1")
            .id;
        let after = Subscription::new(SourceA(1))
            .scoped("pane-1")
            .map(|()| 1)
            .id;

        assert_eq!(before, after);
    }

    #[test]
    fn scope_hash_collisions_do_not_make_scoped_ids_equal() {
        let first = Subscription::new(SourceA(1)).scoped(Collision(1)).id;
        let second = Subscription::new(SourceA(1)).scoped(Collision(2)).id;

        assert_eq!(hash(&first), hash(&second));
        assert_ne!(first, second);
    }
}