mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-05-24 09:10:49 +00:00
Overhaul reaction handler to a thread spawning model (#2)
* Overhaul reaction handler to a thread spawning model * Remove limits for mods on voting and community buckets
This commit is contained in:
parent
18ab9a3434
commit
10ba2dda1d
7 changed files with 183 additions and 128 deletions
|
@ -17,7 +17,7 @@ impl ReactionWatcher {
|
|||
/// Takes a copy of Context (which you can `clone`), a pager (see "Pagination") and a target channel id.
|
||||
/// Pagination will handle all events on adding/removing an "arrow" emoji (⬅️ and ➡️).
|
||||
/// This is a blocking call - it will block the thread until duration is over.
|
||||
pub fn paginate<T: Pagination>(
|
||||
pub fn paginate<T: Pagination + Send + 'static>(
|
||||
&self,
|
||||
ctx: Context,
|
||||
channel: ChannelId,
|
||||
|
@ -25,7 +25,8 @@ impl ReactionWatcher {
|
|||
duration: std::time::Duration,
|
||||
) -> CommandResult {
|
||||
let handler = PaginationHandler::new(pager, ctx, channel)?;
|
||||
self.handle_reactions(handler, duration)
|
||||
self.handle_reactions(handler, duration, |_| {});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// A version of `paginate` that compiles for closures.
|
||||
|
@ -39,7 +40,9 @@ impl ReactionWatcher {
|
|||
duration: std::time::Duration,
|
||||
) -> CommandResult
|
||||
where
|
||||
T: for<'a> FnMut(u8, &'a mut EditMessage) -> (&'a mut EditMessage, CommandResult),
|
||||
T: for<'a> FnMut(u8, &'a mut EditMessage) -> (&'a mut EditMessage, CommandResult)
|
||||
+ Send
|
||||
+ 'static,
|
||||
{
|
||||
self.paginate(ctx, channel, pager, duration)
|
||||
}
|
||||
|
|
|
@ -51,49 +51,55 @@ impl ReactionWatcher {
|
|||
/// React! to a series of reaction
|
||||
///
|
||||
/// The reactions stop after `duration` of idle.
|
||||
pub fn handle_reactions(
|
||||
pub fn handle_reactions<H: ReactionHandler + Send + 'static>(
|
||||
&self,
|
||||
mut h: impl ReactionHandler,
|
||||
mut h: H,
|
||||
duration: std::time::Duration,
|
||||
) -> CommandResult {
|
||||
callback: impl FnOnce(H) -> () + Send + 'static,
|
||||
) {
|
||||
let (send, reactions) = bounded(0);
|
||||
{
|
||||
self.channels.lock().expect("Poisoned!").push(send);
|
||||
}
|
||||
loop {
|
||||
let timeout = after(duration);
|
||||
let r = select! {
|
||||
recv(reactions) -> r => { let (r, is_added) = r.unwrap(); h.handle_reaction(&*r, is_added) },
|
||||
recv(timeout) -> _ => break,
|
||||
};
|
||||
if let Err(v) = r {
|
||||
dbg!(v);
|
||||
std::thread::spawn(move || {
|
||||
loop {
|
||||
let timeout = after(duration);
|
||||
let r = select! {
|
||||
recv(reactions) -> r => { let (r, is_added) = r.unwrap(); h.handle_reaction(&*r, is_added) },
|
||||
recv(timeout) -> _ => break,
|
||||
};
|
||||
if let Err(v) = r {
|
||||
dbg!(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
callback(h)
|
||||
});
|
||||
}
|
||||
/// React! to a series of reaction
|
||||
///
|
||||
/// The handler will stop after `duration` no matter what.
|
||||
pub fn handle_reactions_timed(
|
||||
pub fn handle_reactions_timed<H: ReactionHandler + Send + 'static>(
|
||||
&self,
|
||||
mut h: impl ReactionHandler,
|
||||
mut h: H,
|
||||
duration: std::time::Duration,
|
||||
) -> CommandResult {
|
||||
callback: impl FnOnce(H) -> () + Send + 'static,
|
||||
) {
|
||||
let (send, reactions) = bounded(0);
|
||||
{
|
||||
self.channels.lock().expect("Poisoned!").push(send);
|
||||
}
|
||||
let timeout = after(duration);
|
||||
loop {
|
||||
let r = select! {
|
||||
recv(reactions) -> r => { let (r, is_added) = r.unwrap(); h.handle_reaction(&*r, is_added) },
|
||||
recv(timeout) -> _ => break,
|
||||
};
|
||||
if let Err(v) = r {
|
||||
dbg!(v);
|
||||
std::thread::spawn(move || {
|
||||
let timeout = after(duration);
|
||||
loop {
|
||||
let r = select! {
|
||||
recv(reactions) -> r => { let (r, is_added) = r.unwrap(); h.handle_reaction(&*r, is_added) },
|
||||
recv(timeout) -> _ => break,
|
||||
};
|
||||
if let Err(v) = r {
|
||||
dbg!(v);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
callback(h);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue