pub struct CapabilityContext<Op, Event>where
Op: Operation,{ /* private fields */ }
Expand description
An interface for capabilities to interact with the app and the shell.
To use update_app
, notify_shell
or request_from_shell
, spawn a task first.
For example (from crux_time
)
pub fn get<F>(&self, callback: F)
where
F: FnOnce(TimeResponse) -> Ev + Send + Sync + 'static,
{
let ctx = self.context.clone();
self.context.spawn(async move {
let response = ctx.request_from_shell(TimeRequest).await;
ctx.update_app(callback(response));
});
}
Implementations§
source§impl<Op, Ev> CapabilityContext<Op, Ev>where
Op: Operation,
Ev: 'static,
impl<Op, Ev> CapabilityContext<Op, Ev>where
Op: Operation,
Ev: 'static,
sourcepub fn request_from_shell(&self, operation: Op) -> ShellRequest<Op::Output>
pub fn request_from_shell(&self, operation: Op) -> ShellRequest<Op::Output>
Send an effect request to the shell, expecting an output. The
provided operation
describes the effect input in a serialisable fashion,
and must implement the Operation
trait to declare the expected
output type.
request_from_shell
returns a future of the output, which can be
await
ed. You should only call this method inside an async task
created with CapabilityContext::spawn
.
source§impl<Op, Ev> CapabilityContext<Op, Ev>where
Op: Operation,
Ev: 'static,
impl<Op, Ev> CapabilityContext<Op, Ev>where
Op: Operation,
Ev: 'static,
sourcepub fn stream_from_shell(&self, operation: Op) -> ShellStream<Op::Output>
pub fn stream_from_shell(&self, operation: Op) -> ShellStream<Op::Output>
Send an effect request to the shell, expecting a stream of responses
source§impl<Op, Ev> CapabilityContext<Op, Ev>where
Op: Operation,
Ev: 'static,
impl<Op, Ev> CapabilityContext<Op, Ev>where
Op: Operation,
Ev: 'static,
sourcepub fn spawn(&self, f: impl Future<Output = ()> + 'static + Send)
pub fn spawn(&self, f: impl Future<Output = ()> + 'static + Send)
Spawn a task to do the asynchronous work. Within the task, async code can be used to interact with the Shell and the App.
sourcepub async fn notify_shell(&self, operation: Op)
pub async fn notify_shell(&self, operation: Op)
Send an effect request to the shell in a fire and forget fashion. The
provided operation
does not expect anything to be returned back.
sourcepub fn update_app(&self, event: Ev)
pub fn update_app(&self, event: Ev)
Send an event to the app. The event will be processed on the next
run of the update loop. You can call update_app
several times,
the events will be queued up and processed sequentially after your
async task either await
s or finishes.
sourcepub fn map_event<NewEv, F>(&self, func: F) -> CapabilityContext<Op, NewEv>
pub fn map_event<NewEv, F>(&self, func: F) -> CapabilityContext<Op, NewEv>
Transform the CapabilityContext into one which uses the provided function to
map each event dispatched with update_app
to a different event type.
This is useful when composing apps from modules to wrap a submodule’s event type with a specific variant of the parent module’s event, so it can be forwarded to the submodule when received.
In a typical case you would implement From
on the submodule’s Capabilities
type
impl From<&Capabilities> for child::Capabilities {
fn from(incoming: &Capabilities) -> Self {
child::Capabilities {
some_capability: incoming.some_capability.map_event(Event::Submodule),
render: incoming.render.map_event(Event::Submodule),
}
}
}
in the parent module’s update
function, you can then call .into()
on the
capabilities, before passing them down to the submodule.
Trait Implementations§
Auto Trait Implementations§
impl<Op, Event> Freeze for CapabilityContext<Op, Event>
impl<Op, Event> !RefUnwindSafe for CapabilityContext<Op, Event>
impl<Op, Event> Send for CapabilityContext<Op, Event>
impl<Op, Event> Sync for CapabilityContext<Op, Event>
impl<Op, Event> Unpin for CapabilityContext<Op, Event>
impl<Op, Event> !UnwindSafe for CapabilityContext<Op, Event>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)