Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move task handle into bevy Resource / Component #245

Merged
merged 2 commits into from
Jul 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions bevy_matchbox/src/socket.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use bevy::{
ecs::system::Command,
prelude::{Commands, Component, Deref, DerefMut, Resource, World},
prelude::{Commands, Component, Resource, World},
tasks::IoTaskPool,
};
pub use matchbox_socket;
use matchbox_socket::{
BuildablePlurality, MessageLoopFuture, SingleChannel, WebRtcSocket, WebRtcSocketBuilder,
};
use std::marker::PhantomData;
use std::{
fmt::Debug,
marker::PhantomData,
ops::{Deref, DerefMut},
};

/// A [`WebRtcSocket`] as a [`Component`] or [`Resource`].
///
Expand Down Expand Up @@ -65,8 +69,22 @@ use std::marker::PhantomData;
/// commands.remove_resource::<MatchboxSocket<SingleChannel>>();
/// }
/// ```
#[derive(Resource, Component, Debug, Deref, DerefMut)]
pub struct MatchboxSocket<C: BuildablePlurality>(WebRtcSocket<C>);
#[derive(Resource, Component, Debug)]
pub struct MatchboxSocket<C: BuildablePlurality>(WebRtcSocket<C>, Box<dyn Debug + Send + Sync>);

impl<C: BuildablePlurality> Deref for MatchboxSocket<C> {
type Target = WebRtcSocket<C>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<C: BuildablePlurality> DerefMut for MatchboxSocket<C> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

impl<C: BuildablePlurality> From<WebRtcSocketBuilder<C>> for MatchboxSocket<C> {
fn from(builder: WebRtcSocketBuilder<C>) -> Self {
Expand All @@ -77,8 +95,8 @@ impl<C: BuildablePlurality> From<WebRtcSocketBuilder<C>> for MatchboxSocket<C> {
impl<C: BuildablePlurality> From<(WebRtcSocket<C>, MessageLoopFuture)> for MatchboxSocket<C> {
fn from((socket, message_loop_fut): (WebRtcSocket<C>, MessageLoopFuture)) -> Self {
let task_pool = IoTaskPool::get();
task_pool.spawn(message_loop_fut).detach();
MatchboxSocket(socket)
let task = task_pool.spawn(message_loop_fut);
MatchboxSocket(socket, Box::new(task))
}
}

Expand Down