44 lines
1.1 KiB
Rust
44 lines
1.1 KiB
Rust
use std::collections::VecDeque;
|
|
|
|
use bevy::prelude::Resource;
|
|
|
|
/// Command queue which allows both synchronous and asynchronous queuing.
|
|
#[derive(Resource)]
|
|
pub struct CommandQueue<T> {
|
|
sync_commands: VecDeque<T>,
|
|
async_sender: async_channel::Sender<T>,
|
|
async_receiver: async_channel::Receiver<T>,
|
|
}
|
|
|
|
impl<T> CommandQueue<T> {
|
|
pub fn add(&mut self, command: T) {
|
|
self.sync_commands.push_back(command)
|
|
}
|
|
|
|
pub fn get_async_sender(&self) -> async_channel::Sender<T> {
|
|
self.async_sender.clone()
|
|
}
|
|
|
|
pub fn get_commands(&mut self) -> Vec<T> {
|
|
let mut commands = vec![];
|
|
|
|
while let Ok(command) = self.async_receiver.try_recv() {
|
|
commands.push(command);
|
|
}
|
|
|
|
commands.extend(self.sync_commands.drain(0..self.sync_commands.len()));
|
|
|
|
commands
|
|
}
|
|
}
|
|
|
|
impl<T> Default for CommandQueue<T> {
|
|
fn default() -> Self {
|
|
let (async_sender, async_receiver) = async_channel::unbounded::<T>();
|
|
Self {
|
|
sync_commands: Default::default(),
|
|
async_sender,
|
|
async_receiver,
|
|
}
|
|
}
|
|
} |