Skip to main content

Subscription

Struct Subscription 

Source
pub struct Subscription<Message> { /* private fields */ }
Expand description

The sources an application wants to hear from, for as long as it keeps declaring them.

Nothing reaches update from a source the application did not declare — terminal input included. After each update the runtime reconciles the declaration against what it is running: a declared source whose Key is already running keeps running, one whose key is not running starts, and a running one whose key is no longer declared stops.

The function each constructor takes turns the source’s own value into the application’s message; the runtime does not know that type and cannot deliver without one. That function is not part of a source’s identity, so a source declared again keeps running and passes its messages through the most recent declaration’s function from then on.

use std::time::Duration;

use urushi_tui_app::{Input, Subscription};

enum Message {
    Input(Input),
    Tick,
}

let subscription = Subscription::batch([
    Subscription::input(Message::Input),
    Subscription::interval("clock", Duration::from_secs(1), |_| Message::Tick),
]);

Implementations§

Source§

impl<Message> Subscription<Message>

Source

pub fn none() -> Self

No source.

Source

pub fn input<F>(f: F) -> Self
where F: Fn(Input) -> Message + Send + Sync + 'static,

Terminal key and text input.

Input is a subscription rather than a method the runtime always calls, so a program that must not receive input in some state — while an external editor owns the terminal, say — declares none in that state.

Source

pub fn surface<F>(f: F) -> Self
where F: Fn(Surface) -> Message + Send + Sync + 'static,

Observations of the surface the application draws on.

Source

pub fn interval<F>(key: impl Into<Key>, period: Duration, f: F) -> Self
where F: Fn(Instant) -> Message + Send + Sync + 'static,

A timer that fires every period, for as long as it is declared.

An interval is the one runtime source an application can want more than one of, so it names its own: two timers of one period are two timers when their keys differ. Declaring the same key with a different period restarts that timer at the new period.

A timer that should fire once is Effect::after rather than an interval the application stops declaring.

Source

pub fn signal<F>(signal: Signal, f: F) -> Self
where F: Fn(Signal) -> Message + Send + Sync + 'static,

One process signal.

A handler is installed only for a signal that is declared.

Source

pub fn terminal_errors<F>(f: F) -> Self
where F: Fn(Error) -> Message + Send + Sync + 'static,

Failures the terminal reports while drawing.

A draw that fails ends the run unless this is declared. With it, the failure arrives as a message, the frame that failed is not committed, and the application decides what to do — ignore it, record it, save and shut down.

Source

pub fn stream<S>(key: impl Into<Key>, stream: S) -> Self
where S: Stream<Item = Message> + Send + 'static, Message: Send + 'static,

An application-defined source that is a stream, under the default admission.

Source

pub fn stream_with<S>( key: impl Into<Key>, admission: Admission, stream: S, ) -> Self
where S: Stream<Item = Message> + Send + 'static, Message: Send + 'static,

An application-defined source that is a stream, under admission.

Source

pub fn run<F, Fut>(key: impl Into<Key>, f: F) -> Self
where F: FnOnce(Sender<Message>) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static, Message: Send + 'static,

An application-defined asynchronous source, under the default admission.

Source

pub fn run_with<F, Fut>(key: impl Into<Key>, admission: Admission, f: F) -> Self
where F: FnOnce(Sender<Message>) -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static, Message: Send + 'static,

An application-defined asynchronous source, under admission.

Source

pub fn run_blocking<F>(key: impl Into<Key>, f: F) -> Self
where F: FnOnce(Sender<Message>) + Send + 'static, Message: Send + 'static,

An application-defined blocking source, run on its own thread, under the default admission.

Source

pub fn run_blocking_with<F>( key: impl Into<Key>, admission: Admission, f: F, ) -> Self
where F: FnOnce(Sender<Message>) + Send + 'static, Message: Send + 'static,

An application-defined blocking source, run on its own thread, under admission.

Source

pub fn batch(subscriptions: impl IntoIterator<Item = Self>) -> Self

Several sources.

Source

pub fn map<To>( self, f: impl Fn(Message) -> To + Send + Sync + 'static, ) -> Subscription<To>
where Message: Send + 'static, To: Send + 'static,

The same sources with their messages passed through f.

A parent program declares a child’s subscriptions mapped into its own message, so the child stays a program of its own.

Trait Implementations§

Source§

impl<Message> Debug for Subscription<Message>

Source§

fn fmt(&self, formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<Message> Default for Subscription<Message>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<Message> Freeze for Subscription<Message>

§

impl<Message> !RefUnwindSafe for Subscription<Message>

§

impl<Message> Send for Subscription<Message>

§

impl<Message> !Sync for Subscription<Message>

§

impl<Message> Unpin for Subscription<Message>

§

impl<Message> UnsafeUnpin for Subscription<Message>

§

impl<Message> !UnwindSafe for Subscription<Message>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.