pub struct KeyedRequests<K> { /* private fields */ }web03 only.Expand description
A set of pending requests with at most one for each key, kept alive until each completes.
This is like Requests, except that each request is stored under a key
with KeyedRequests::insert. Storing a request under a key which already
has one cancels the request stored before it, so that what is fetched for
a key is always the latest. Any number of keys can have a request in
flight at once, and a key is forgotten once its request completes.
Every request still pending is cancelled when the set is dropped or
cleared.
§Examples
use yew::prelude::*;
use musli_web::web03::prelude::*;
mod api {
use musli::{Decode, Encode};
use musli_web::api;
#[derive(Encode, Decode)]
pub struct RenameRequest<'de> {
pub id: u32,
pub name: &'de str,
}
api::define! {
pub type Rename;
impl Endpoint for Rename {
impl<'de> Request for RenameRequest<'de>;
type Response<'de> = ();
}
}
}
enum Msg {
Rename(u32, String),
Renamed(Result<ws::Packet<api::Rename>, ws::Error>),
}
#[derive(Properties, PartialEq)]
struct Props {
ws: ws::Handle,
rows: Vec<u32>,
}
struct Rows {
// At most one rename in flight for each row.
renames: ws::KeyedRequests<u32>,
}
impl Component for Rows {
type Message = Msg;
type Properties = Props;
fn create(_: &Context<Self>) -> Self {
Self {
renames: ws::KeyedRequests::new(),
}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Rename(id, name) => {
// Renaming a row again cancels its earlier rename, but
// leaves the renames of other rows alone.
self.renames.insert(
id,
ctx.props()
.ws
.request()
.body(api::RenameRequest { id, name: &name })
.on_packet(ctx.link().callback(Msg::Renamed))
.send(),
);
true
}
Msg::Renamed(_) => true,
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
{for ctx.props().rows.iter().copied().map(|id| {
let onclick = ctx.link().callback(move |_: MouseEvent| {
Msg::Rename(id, format!("Row {id}"))
});
let label = if self.renames.is_pending(&id) {
String::from("Renaming...")
} else {
format!("Rename #{id}")
};
html!(<button {onclick}>{label}</button>)
})}
}
}
}Implementations§
Source§impl<K> KeyedRequests<K>
impl<K> KeyedRequests<K>
Sourcepub fn new() -> Self
pub fn new() -> Self
Construct an empty set of requests.
§Examples
use musli_web::web03::prelude::*;
let requests = ws::KeyedRequests::<u32>::new();
assert!(requests.is_empty());Source§impl<K> KeyedRequests<K>
impl<K> KeyedRequests<K>
Sourcepub fn insert(&mut self, key: K, request: Request)
pub fn insert(&mut self, key: K, request: Request)
Keep request under key until it completes, cancelling the request
stored under key before it.
A request which is not pending, such as one which could not be sent, is dropped straight away, but still cancels the one before it.
Trait Implementations§
Source§impl<K> Debug for KeyedRequests<K>where
K: Debug,
impl<K> Debug for KeyedRequests<K>where
K: Debug,
Auto Trait Implementations§
impl<K> !RefUnwindSafe for KeyedRequests<K>
impl<K> !Send for KeyedRequests<K>
impl<K> !Sync for KeyedRequests<K>
impl<K> !UnwindSafe for KeyedRequests<K>
impl<K> Freeze for KeyedRequests<K>
impl<K> Unpin for KeyedRequests<K>
impl<K> UnsafeUnpin for KeyedRequests<K>
Blanket Implementations§
impl<Token, Builder, How> AllPropsFor<Builder, How> for Tokenwhere
Builder: Buildable<Token>,
<Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,
impl<Token, Builder, How> AllPropsFor<Builder, How> for Tokenwhere
Builder: Buildable<Token>,
<Builder as Buildable<Token>>::WrappedToken: HasAllProps<<Builder as Buildable<Token>>::Output, How>,
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
impl<T> HasAllProps<(), T> for T
impl<T> HasAllProps<(), T> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
Source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
self to a value of a Properties struct.Source§impl<T> IntoPropValue<Option<T>> for T
impl<T> IntoPropValue<Option<T>> for T
Source§fn into_prop_value(self) -> Option<T>
fn into_prop_value(self) -> Option<T>
self to a value of a Properties struct.Source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
Source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
self to a value of a Properties struct.Source§impl<T> IntoPropValue<T> for T
impl<T> IntoPropValue<T> for T
Source§fn into_prop_value(self) -> T
fn into_prop_value(self) -> T
self to a value of a Properties struct.