Skip to content

Commit

Permalink
docs and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
extrawurst committed Nov 3, 2024
1 parent 40f78db commit c904d73
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bevy_channel_trigger"
version = "0.1.1"
version = "0.1.2"
edition = "2021"
authors = ["Rustunit <mail@rustunit.com>"]
categories = ["game-development"]
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
[![crates.io](https://img.shields.io/crates/v/bevy_channel_trigger)](https://crates.io/crates/bevy_channel_trigger)
[![docs.rs](https://docs.rs/bevy_channel_trigger/badge.svg)](https://docs.rs/bevy_channel_trigger)

Send events via a channels form anywhere (eg. web-dom, c-ffi) to Bevy Observers.
Send events via a channel from anywhere (eg. web-dom, c-ffi) to Bevy Observers.
Inspired by [bevy_crossbeam_event](https://github.com/johanhelsing/bevy_crossbeam_event) but using [flume](https://github.com/zesterer/flume) instead of `crossbeam` as the underlying efficient unbounded channel and delivering the events via Bevy Observers instead of `EventReader`. Furthermore we lint to be guaranteed panic free.

Example uses:

* [bevy_web_drop_image_as_sprite](https://github.com/rustunit/bevy_web_drop_image_as_sprite)
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use bevy::{ecs::event::Event, prelude::*};
use flume::{Receiver, Sender, TryRecvError};

/// channel sender to share with multiple producers and offering a simple `send` function
#[derive(Resource, Clone, Debug)]
pub struct ChannelSender<T: Event>(Sender<T>);

impl<T: Event> ChannelSender<T> {
/// send `event` to our central receiver that forwards them as triggers that can be observed
pub fn send(&self, event: impl Into<T>) {
let event = event.into();
if let Err(err) = self.0.send(event) {
Expand All @@ -16,7 +18,11 @@ impl<T: Event> ChannelSender<T> {
#[derive(Resource)]
struct EventReceiver<T: Event>(Receiver<T>);

/// Extension to Bevy `App` that allows ergonomic creation of the channel
pub trait ChannelTriggerApp {
/// Spawns a channel registers the receiver as a resource and returns the `ChannelSender<T>`
/// This sender can be used from anywhere to send events into the Bevy world.
/// These triggers can be subscribed to via `app.observe`.
fn add_channel_trigger<T: Event>(&mut self) -> ChannelSender<T>;
}

Expand Down

0 comments on commit c904d73

Please sign in to comment.