Web: async improvements (#3805)
- Internal: Fix dropping `Notifier` without sending a result causing `Future`s to never complete. This should never happen anyway, but now we get a panic instead of nothing if we hit a bug. - Internal: Remove a bunch of `unwrap()`s that aren't required when correctly using `MainThreadMarker`. - `Window::canvas()` is now able to return a reference instead of an owned value. Extracted from #3801.
This commit is contained in:
parent
5ec934b1b0
commit
ef580b817d
12 changed files with 238 additions and 219 deletions
|
|
@ -33,7 +33,6 @@ unsafe impl<V> Send for Value<V> {}
|
|||
unsafe impl<V> Sync for Value<V> {}
|
||||
|
||||
impl<V, S: Clone + Send, E> Wrapper<V, S, E> {
|
||||
#[track_caller]
|
||||
pub fn new<R: Future<Output = ()>>(
|
||||
_: MainThreadMarker,
|
||||
value: V,
|
||||
|
|
@ -41,7 +40,7 @@ impl<V, S: Clone + Send, E> Wrapper<V, S, E> {
|
|||
receiver: impl 'static + FnOnce(Arc<RefCell<Option<V>>>) -> R,
|
||||
sender_data: S,
|
||||
sender_handler: fn(&S, E),
|
||||
) -> Option<Self> {
|
||||
) -> Self {
|
||||
let value = Arc::new(RefCell::new(Some(value)));
|
||||
|
||||
wasm_bindgen_futures::spawn_local({
|
||||
|
|
@ -52,12 +51,7 @@ impl<V, S: Clone + Send, E> Wrapper<V, S, E> {
|
|||
}
|
||||
});
|
||||
|
||||
Some(Self {
|
||||
value: Value { value, local: PhantomData },
|
||||
handler,
|
||||
sender_data,
|
||||
sender_handler,
|
||||
})
|
||||
Self { value: Value { value, local: PhantomData }, handler, sender_data, sender_handler }
|
||||
}
|
||||
|
||||
pub fn send(&self, event: E) {
|
||||
|
|
@ -68,9 +62,8 @@ impl<V, S: Clone + Send, E> Wrapper<V, S, E> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn value(&self) -> Option<Ref<'_, V>> {
|
||||
MainThreadMarker::new()
|
||||
.map(|_| Ref::map(self.value.value.borrow(), |value| value.as_ref().unwrap()))
|
||||
pub fn value(&self, _: MainThreadMarker) -> Ref<'_, V> {
|
||||
Ref::map(self.value.value.borrow(), |value| value.as_ref().unwrap())
|
||||
}
|
||||
|
||||
pub fn with_sender_data<T>(&self, f: impl FnOnce(&S) -> T) -> T {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue