Skip to main content

StreamBuilder

Struct StreamBuilder 

Source
pub struct StreamBuilder<Effect, Event, Task> { /* private fields */ }
Expand description

A builder of stream command

Implementations§

Source§

impl<Effect, Event, Task, T> StreamBuilder<Effect, Event, Task>
where Effect: Send + 'static, Event: Send + 'static, Task: Stream<Item = T> + Send + 'static,

Source

pub fn new<F>(make_task: F) -> Self
where F: FnOnce(CommandContext<Effect, Event>) -> Task + Send + 'static,

Source

pub fn map<F, U>( self, map: F, ) -> StreamBuilder<Effect, Event, impl Stream<Item = U>>
where F: FnMut(T) -> U + Send + 'static,

Source

pub fn then_request<F, U, NextTask>( self, make_next_builder: F, ) -> StreamBuilder<Effect, Event, impl Stream<Item = U>>
where F: Fn(T) -> RequestBuilder<Effect, Event, NextTask> + Send + 'static, NextTask: Future<Output = U> + Send + 'static,

Chain a RequestBuilder to run after completion of this StreamBuilder, passing the result to the provided closure make_next_builder.

The returned value of the closure must be a RequestBuilder, which can represent some more work to be done before the composed future is finished.

If you want to chain a subscription, use Self::then_stream instead.

The closure make_next_builder is only run after successful completion of the self future.

Note that this function consumes the receiving StreamBuilder and returns a new one that represents the composition.

§Example
let mut cmd: Command<Effect, Event> = Command::stream_from_shell(AnOperation::More(1))
    .then_request(|first| {
        let AnOperationOutput::Other(first) = first else {
            panic!("Invalid output!")
        };

        let second = first + 1;
        Command::request_from_shell(AnOperation::More(second))
    })
    .then_send(Event::Completed);

let Effect::AnEffect(mut request) = cmd.effects().next().unwrap();
assert_eq!(request.operation, AnOperation::More(1));

request
   .resolve(AnOperationOutput::Other(1))
   .expect("to resolve");

let Effect::AnEffect(mut request) = cmd.effects().next().unwrap();
assert_eq!(request.operation, AnOperation::More(2));
Source

pub fn then_stream<F, U, NextTask>( self, make_next_builder: F, ) -> StreamBuilder<Effect, Event, impl Stream<Item = U>>
where F: Fn(T) -> StreamBuilder<Effect, Event, NextTask> + Send + 'static, NextTask: Stream<Item = U> + Send + 'static,

Chain another StreamBuilder to run after completion of this one, passing the result to the provided closure make_next_builder.

The returned value of the closure must be a StreamBuilder, which can represent some more work to be done before the composed future is finished.

If you want to chain a request, use Self::then_request instead.

The closure make_next_builder is only run after successful completion of the self future.

Note that this function consumes the receiving StreamBuilder and returns a new one that represents the composition.

§Example
let mut cmd: Command<Effect, Event> = Command::stream_from_shell(AnOperation::More(1))
   .then_stream(|first| {
      let AnOperationOutput::Other(first) = first else {
         panic!("Invalid output!")
     };

     let second = first + 1;
     Command::stream_from_shell(AnOperation::More(second))
   })
   .then_send(Event::Completed);

let Effect::AnEffect(mut request) = cmd.effects().next().unwrap();
assert_eq!(request.operation, AnOperation::More(1));

request
  .resolve(AnOperationOutput::Other(1))
  .expect("to resolve");

let Effect::AnEffect(mut request) = cmd.effects().next().unwrap();
assert_eq!(request.operation, AnOperation::More(2));
Source

pub fn then_send<E>(self, event: E) -> Command<Effect, Event>
where E: Fn(T) -> Event + Send + 'static,

Create the command in an evented context

Source

pub fn into_stream(self, ctx: CommandContext<Effect, Event>) -> Task

Convert the StreamBuilder into a stream to use in an async context

Source

pub fn build(self) -> Command<Effect, Event>

Convert the StreamBuilder into a Command to use in an sync context

Note: You might be looking for then_send instead, which will send each item in the stream back into the app with an event.

The command created in this function will ignore the output of the stream so may not be very useful. It may be useful when using a 3rd party capability and you don’t care about the stream output.

Auto Trait Implementations§

§

impl<Effect, Event, Task> Freeze for StreamBuilder<Effect, Event, Task>

§

impl<Effect, Event, Task> !RefUnwindSafe for StreamBuilder<Effect, Event, Task>

§

impl<Effect, Event, Task> Send for StreamBuilder<Effect, Event, Task>

§

impl<Effect, Event, Task> !Sync for StreamBuilder<Effect, Event, Task>

§

impl<Effect, Event, Task> Unpin for StreamBuilder<Effect, Event, Task>

§

impl<Effect, Event, Task> UnsafeUnpin for StreamBuilder<Effect, Event, Task>

§

impl<Effect, Event, Task> !UnwindSafe for StreamBuilder<Effect, Event, Task>

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.