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 systems to preupdate #14

Merged
merged 2 commits into from
Oct 31, 2024
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
24 changes: 14 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ pub use reqwest::{StatusCode, Version};
#[cfg(not(target_family = "wasm"))]
use {bevy::tasks::Task, futures_lite::future};

/// The [`SystemSet`] that Reqwest systems are added to.
#[derive(Debug, Hash, PartialEq, Eq, Clone, SystemSet)]
pub struct ReqwestSet;

/// Plugin that allows to send http request using the [reqwest](https://crates.io/crates/reqwest) library from
/// inside bevy.
/// The plugin uses [bevy_eventlister](https://crates.io/crates/bevy_eventlistener) to provide callback style events
/// when the http requests finishes.
/// supports both wasm and native
///
/// The plugin uses [`Observer`] systems to provide callbacks when the http requests finishes.
///
/// Supports both wasm and native.
pub struct ReqwestPlugin {
/// this enables the plugin to insert a new [`Name`] component onto the entity used to drive
/// the http request to completion, if no Name component already exists
Expand All @@ -36,9 +41,7 @@ impl Default for ReqwestPlugin {
}
impl Plugin for ReqwestPlugin {
fn build(&self, app: &mut App) {
if !app.world().contains_resource::<ReqwestClient>() {
app.init_resource::<ReqwestClient>();
}
app.init_resource::<ReqwestClient>();

if self.automatically_name_requests {
// register a hook on the component to add a name to the entity if it doesnt have one already
Expand All @@ -56,15 +59,16 @@ impl Plugin for ReqwestPlugin {
}
//
app.add_systems(
Update,
PreUpdate,
(
// These systems are chained as the callbacks are triggered in PreUpdate
// So if remove_finished_requests runs after poll_inflight_requests_to_bytes
// the entity will be removed before the callback is triggered.
Self::remove_finished_requests,
Self::poll_inflight_requests_to_bytes,
)
.chain(),
.chain()
.in_set(ReqwestSet),
);
}
}
Expand Down Expand Up @@ -116,7 +120,7 @@ impl ReqwestPlugin {
pub struct BevyReqwestBuilder<'a>(EntityCommands<'a>);

impl<'a> BevyReqwestBuilder<'a> {
/// Provide a system where the first argument is [`Trigger<ReqwestResponseEvent>`] that will run on the
/// Provide a system where the first argument is [`Trigger`] [`ReqwestResponseEvent`] that will run on the
/// response from the http request
///
/// # Examples
Expand All @@ -136,7 +140,7 @@ impl<'a> BevyReqwestBuilder<'a> {
self
}

/// Provide a system where the first argument is [`Trigger<ReqwestErrorEvent>`] that will run on the
/// Provide a system where the first argument is [`Trigger`] [`ReqwestErrorEvent`] that will run on the
/// response from the http request
///
/// # Examples
Expand Down
Loading