-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dont overload xr runtime with notify spam
- Loading branch information
Showing
12 changed files
with
161 additions
and
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,3 +15,5 @@ pub mod uidev; | |
pub mod osc; | ||
|
||
pub mod overlay; | ||
|
||
pub mod task; |
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,113 @@ | ||
use std::{ | ||
cmp, | ||
collections::{BinaryHeap, VecDeque}, | ||
sync::atomic::{self, AtomicUsize}, | ||
time::Instant, | ||
}; | ||
|
||
use serde::Deserialize; | ||
|
||
use crate::state::AppState; | ||
|
||
use super::{ | ||
common::OverlaySelector, | ||
overlay::{OverlayBackend, OverlayState}, | ||
}; | ||
|
||
static TASK_AUTO_INCREMENT: AtomicUsize = AtomicUsize::new(0); | ||
|
||
struct AppTask { | ||
pub not_before: Instant, | ||
pub id: usize, | ||
pub task: TaskType, | ||
} | ||
|
||
impl PartialEq<AppTask> for AppTask { | ||
fn eq(&self, other: &Self) -> bool { | ||
self.cmp(other) == cmp::Ordering::Equal | ||
} | ||
} | ||
impl PartialOrd<AppTask> for AppTask { | ||
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
impl Eq for AppTask {} | ||
impl Ord for AppTask { | ||
fn cmp(&self, other: &Self) -> cmp::Ordering { | ||
self.not_before | ||
.cmp(&other.not_before) | ||
.then(self.id.cmp(&other.id)) | ||
.reverse() | ||
} | ||
} | ||
|
||
pub enum SystemTask { | ||
ColorGain(ColorChannel, f32), | ||
ResetPlayspace, | ||
FixFloor, | ||
} | ||
|
||
pub type OverlayTask = dyn FnOnce(&mut AppState, &mut OverlayState) + Send; | ||
pub type CreateOverlayTask = | ||
dyn FnOnce(&mut AppState) -> Option<(OverlayState, Box<dyn OverlayBackend>)> + Send; | ||
|
||
pub enum TaskType { | ||
Global(Box<dyn FnOnce(&mut AppState) + Send>), | ||
Overlay(OverlaySelector, Box<OverlayTask>), | ||
CreateOverlay(OverlaySelector, Box<CreateOverlayTask>), | ||
DropOverlay(OverlaySelector), | ||
System(SystemTask), | ||
} | ||
|
||
#[derive(Deserialize, Clone, Copy)] | ||
pub enum ColorChannel { | ||
R, | ||
G, | ||
B, | ||
All, | ||
} | ||
|
||
pub struct TaskContainer { | ||
tasks: BinaryHeap<AppTask>, | ||
} | ||
|
||
impl TaskContainer { | ||
pub fn new() -> Self { | ||
Self { | ||
tasks: BinaryHeap::new(), | ||
} | ||
} | ||
|
||
pub fn enqueue(&mut self, task: TaskType) { | ||
self.tasks.push(AppTask { | ||
not_before: Instant::now(), | ||
id: TASK_AUTO_INCREMENT.fetch_add(1, atomic::Ordering::Relaxed), | ||
task, | ||
}); | ||
} | ||
|
||
/// Enqueue a task to be executed at a specific time. | ||
/// If the time is in the past, the task will be executed immediately. | ||
/// Multiple tasks enqueued for the same instant will be executed in order of submission. | ||
pub fn enqueue_at(&mut self, task: TaskType, not_before: Instant) { | ||
self.tasks.push(AppTask { | ||
not_before, | ||
id: TASK_AUTO_INCREMENT.fetch_add(1, atomic::Ordering::Relaxed), | ||
task, | ||
}); | ||
} | ||
|
||
pub fn retrieve_due(&mut self, dest_buf: &mut VecDeque<TaskType>) { | ||
let now = Instant::now(); | ||
|
||
while let Some(task) = self.tasks.peek() { | ||
if task.not_before > now { | ||
break; | ||
} | ||
|
||
// Safe unwrap because we peeked. | ||
dest_buf.push_back(self.tasks.pop().unwrap().task); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.