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>
impl<Effect, Event, Task, T> RequestBuilder<Effect, Event, Task>
pub fn new<F>(make_task: F) -> Self
pub fn map<F, U>( self, map: F, ) -> RequestBuilder<Effect, Event, impl Future<Output = U>>
Sourcepub fn then_notify<F, NextTask>(
self,
make_next_builder: F,
) -> NotificationBuilder<Effect, Event, impl Future<Output = ()>>
pub fn then_notify<F, NextTask>( self, make_next_builder: F, ) -> NotificationBuilder<Effect, Event, impl Future<Output = ()>>
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());Sourcepub 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,
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));Sourcepub 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,
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));Sourcepub fn into_future(self, ctx: CommandContext<Effect, Event>) -> Task
pub fn into_future(self, ctx: CommandContext<Effect, Event>) -> Task
Convert the RequestBuilder into a future to use in an async context
Sourcepub fn then_send<E>(self, event: E) -> Command<Effect, Event>
pub fn then_send<E>(self, event: E) -> Command<Effect, Event>
Create the command in an evented context
Sourcepub fn build(self) -> Command<Effect, Event>
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.