-
Notifications
You must be signed in to change notification settings - Fork 932
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Web: improve custom cursor handling and add animated cursors (#3384)
- Loading branch information
Showing
18 changed files
with
1,078 additions
and
412 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use std::error::Error; | ||
use std::fmt::{self, Display, Formatter}; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::sync::Arc; | ||
use std::task::{Context, Poll}; | ||
|
||
use pin_project::pin_project; | ||
|
||
use super::AtomicWaker; | ||
|
||
#[pin_project] | ||
pub struct Abortable<F: Future> { | ||
#[pin] | ||
future: F, | ||
shared: Arc<Shared>, | ||
} | ||
|
||
impl<F: Future> Abortable<F> { | ||
pub fn new(handle: AbortHandle, future: F) -> Self { | ||
Self { | ||
future, | ||
shared: handle.0, | ||
} | ||
} | ||
} | ||
|
||
impl<F: Future> Future for Abortable<F> { | ||
type Output = Result<F::Output, Aborted>; | ||
|
||
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
if self.shared.aborted.load(Ordering::Relaxed) { | ||
return Poll::Ready(Err(Aborted)); | ||
} | ||
|
||
if let Poll::Ready(value) = self.as_mut().project().future.poll(cx) { | ||
return Poll::Ready(Ok(value)); | ||
} | ||
|
||
self.shared.waker.register(cx.waker()); | ||
|
||
if self.shared.aborted.load(Ordering::Relaxed) { | ||
return Poll::Ready(Err(Aborted)); | ||
} | ||
|
||
Poll::Pending | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct Shared { | ||
waker: AtomicWaker, | ||
aborted: AtomicBool, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct AbortHandle(Arc<Shared>); | ||
|
||
impl AbortHandle { | ||
pub fn new() -> Self { | ||
Self(Arc::new(Shared { | ||
waker: AtomicWaker::new(), | ||
aborted: AtomicBool::new(false), | ||
})) | ||
} | ||
|
||
pub fn abort(&self) { | ||
self.0.aborted.store(true, Ordering::Relaxed); | ||
self.0.waker.wake() | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct DropAbortHandle(AbortHandle); | ||
|
||
impl DropAbortHandle { | ||
pub fn new(handle: AbortHandle) -> Self { | ||
Self(handle) | ||
} | ||
|
||
pub fn handle(&self) -> AbortHandle { | ||
self.0.clone() | ||
} | ||
} | ||
|
||
impl Drop for DropAbortHandle { | ||
fn drop(&mut self) { | ||
self.0.abort() | ||
} | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)] | ||
pub struct Aborted; | ||
|
||
impl Display for Aborted { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
write!(f, "`Abortable` future has been aborted") | ||
} | ||
} | ||
|
||
impl Error for Aborted {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use std::cell::RefCell; | ||
use std::ops::Deref; | ||
use std::task::Waker; | ||
|
||
#[derive(Debug)] | ||
pub struct AtomicWaker(RefCell<Option<Waker>>); | ||
|
||
impl AtomicWaker { | ||
pub const fn new() -> Self { | ||
Self(RefCell::new(None)) | ||
} | ||
|
||
pub fn register(&self, waker: &Waker) { | ||
let mut this = self.0.borrow_mut(); | ||
|
||
if let Some(old_waker) = this.deref() { | ||
if old_waker.will_wake(waker) { | ||
return; | ||
} | ||
} | ||
|
||
*this = Some(waker.clone()); | ||
} | ||
|
||
pub fn wake(&self) { | ||
if let Some(waker) = self.0.borrow_mut().take() { | ||
waker.wake(); | ||
} | ||
} | ||
} | ||
|
||
// SAFETY: Wasm without the `atomics` target feature is single-threaded. | ||
unsafe impl Send for AtomicWaker {} | ||
// SAFETY: Wasm without the `atomics` target feature is single-threaded. | ||
unsafe impl Sync for AtomicWaker {} |
Oops, something went wrong.