Skip to main content

RequestBuilder

Struct RequestBuilder 

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

A builder of one-off request command

Implementations§

Source§

impl<Effect, Event, Task, T> RequestBuilder<Effect, Event, Task>
where Effect: Send + 'static, Event: Send + 'static, Task: Future<Output = 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, ) -> RequestBuilder<Effect, Event, impl Future<Output = U>>
where F: FnOnce(T) -> U + Send + 'static,

Source

pub fn then_notify<F, NextTask>( self, make_next_builder: F, ) -> NotificationBuilder<Effect, Event, impl Future<Output = ()>>
where F: FnOnce(T) -> NotificationBuilder<Effect, Event, NextTask> + Send + 'static, NextTask: Future<Output = ()> + Send + 'static,

Chain a NotificationBuilder 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 NotificationBuilder, which can represent the notification to be sent before the composed future is finished.

If you want to chain a request, use Self::then_request instead. 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 RequestBuilder and returns a NotificationBuilder that represents the composition.

§Example
let mut cmd: Command<Effect, Event> =
    Command::request_from_shell(AnOperation::Request(10))
    .then_notify(|response| {
        let AnOperationOutput::Response(_response) = response else {
            panic!("Invalid output!")
        };

        // possibly do something with the response

        Command::notify_shell(AnOperation::Notify)
    })
    .build();

let effect = cmd.effects().next().unwrap();
let Effect::AnEffect(mut request) = effect;

assert_eq!(request.operation, AnOperation::Request(10));

request
    .resolve(AnOperationOutput::Response("ten".to_string()))
    .expect("should work");

assert!(cmd.events().next().is_none());
let effect = cmd.effects().next().unwrap();
let Effect::AnEffect(request) = effect;

assert_eq!(request.operation, AnOperation::Notify);
assert!(cmd.is_done());
Source

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

Chain another RequestBuilder 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 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 RequestBuilder and returns a new one that represents the composition.

§Example
let mut cmd: Command<Effect, Event> = Command::request_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: FnOnce(T) -> StreamBuilder<Effect, Event, NextTask> + Send + 'static, NextTask: Stream<Item = U> + Send + 'static,

Chain a StreamBuilder to run after completion of this RequestBuilder, 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 RequestBuilder and returns a StreamBuilder that represents the composition.

§Example
let mut cmd: Command<Effect, Event> = Command::request_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 into_future(self, ctx: CommandContext<Effect, Event>) -> Task

Convert the RequestBuilder into a future to use in an async context

Source

pub fn then_send<E>(self, event: E) -> Command<Effect, Event>
where E: FnOnce(T) -> Event + Send + 'static, Task: Future<Output = T> + Send + 'static,

Create the command in an evented context

Source

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

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

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

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

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

§

impl<Effect, Event, Task> !UnwindSafe for RequestBuilder<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.