Skip to content

Commit

Permalink
Rename Ref to Signal and Mut to SignalMut to avoid name conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Nov 26, 2024
1 parent 9f10c39 commit b2154cb
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 72 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ This crate provides a generic library that lets you define reactive components (
```rust
// Counter UI example.

use actuate::prelude::{Mut, *};
use actuate::prelude::*;
use bevy::prelude::*;

// Counter composable.
Expand All @@ -53,10 +53,10 @@ impl Compose for Counter {
.content((
spawn(Text::new(format!("High five count: {}", count))),
spawn(Text::new("Up high")).observe(move |_trigger: In<Trigger<Pointer<Click>>>| {
Mut::update(count, |x| *x += 1)
SignalMut::update(count, |x| *x += 1)
}),
spawn(Text::new("Down low")).observe(move |_trigger: In<Trigger<Pointer<Click>>>| {
Mut::update(count, |x| *x -= 1)
SignalMut::update(count, |x| *x -= 1)
}),
if *count == 0 {
Some(spawn(Text::new("Gimme five!")))
Expand Down Expand Up @@ -107,7 +107,7 @@ struct App {
impl Compose for App {
fn compose(cx: Scope<Self>) -> impl Compose {
// Get a mapped reference to the app's `name` field.
let name = Ref::map(cx.me(), |me| &me.name).into();
let name = Signal::map(cx.me(), |me| &me.name).into();

User { name }
}
Expand Down
6 changes: 3 additions & 3 deletions examples/ecs/counter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Counter UI example.

use actuate::prelude::{Mut, *};
use actuate::prelude::*;
use bevy::prelude::*;

// Counter composable.
Expand All @@ -20,10 +20,10 @@ impl Compose for Counter {
.content((
spawn(Text::new(format!("High five count: {}", count))),
spawn(Text::new("Up high")).observe(move |_trigger: In<Trigger<Pointer<Click>>>| {
Mut::update(count, |x| *x += 1)
SignalMut::update(count, |x| *x += 1)
}),
spawn(Text::new("Down low")).observe(move |_trigger: In<Trigger<Pointer<Click>>>| {
Mut::update(count, |x| *x -= 1)
SignalMut::update(count, |x| *x -= 1)
}),
if *count == 0 {
Some(spawn(Text::new("Gimme five!")))
Expand Down
9 changes: 3 additions & 6 deletions examples/ecs/http.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
// HTTP UI example

use actuate::{
executor::ExecutorContext,
prelude::{Mut, Ref, *},
};
use actuate::{executor::ExecutorContext, prelude::*};
use bevy::prelude::*;
use serde::Deserialize;
use std::collections::HashMap;
Expand Down Expand Up @@ -34,7 +31,7 @@ impl Compose for Breed<'_> {
..default()
})
.content(compose::from_iter(
Ref::map(cx.me(), |me| me.families),
Signal::map(cx.me(), |me| me.families),
|family| spawn(Text::from(family.to_string())),
)),
))
Expand Down Expand Up @@ -63,7 +60,7 @@ impl Compose for BreedList {
.await
.unwrap();

Mut::update(breeds, |breeds| *breeds = json.message);
SignalMut::update(breeds, |breeds| *breeds = json.message);
});

// Render the currently loaded breeds.
Expand Down
6 changes: 4 additions & 2 deletions examples/ecs/timer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Timer UI example.

use actuate::prelude::{Mut, *};
use actuate::prelude::*;
use bevy::prelude::*;

// Timer composable.
Expand All @@ -12,7 +12,9 @@ impl Compose for Timer {
let current_time = use_mut(&cx, Time::default);

// Use the `Time` resource from the ECS world, updating the `current_time`.
use_world(&cx, move |time: Res<Time>| Mut::set(current_time, *time));
use_world(&cx, move |time: Res<Time>| {
SignalMut::set(current_time, *time)
});

// Spawn a `Text` component, updating it when this scope is re-composed.
spawn(Text::new(format!("Elapsed: {:?}", current_time.elapsed())))
Expand Down
8 changes: 4 additions & 4 deletions src/compose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<C: Compose> Compose for Option<C> {
/// Create a composable from an iterator.
pub fn from_iter<'a, I, C>(
iter: I,
f: impl Fn(Ref<'a, I::Item>) -> C + 'a,
f: impl Fn(Signal<'a, I::Item>) -> C + 'a,
) -> FromIter<'a, I, I::Item, C>
where
I: IntoIterator + Clone + Data,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl Drop for AnyItemState {
#[must_use = "Composables do nothing unless composed or returned from other composables."]
pub struct FromIter<'a, I, Item, C> {
iter: I,
f: Box<dyn Fn(Ref<'a, Item>) -> C + 'a>,
f: Box<dyn Fn(Signal<'a, Item>) -> C + 'a>,
}

unsafe impl<I, Item, C> Data for FromIter<'_, I, Item, C>
Expand Down Expand Up @@ -161,7 +161,7 @@ where

let item_ref: &Item = &state.item;
let item_ref: &Item = unsafe { mem::transmute(item_ref) };
let compose = (cx.me().f)(Ref {
let compose = (cx.me().f)(Signal {
value: item_ref,
generation: &cx.generation as _,
});
Expand Down Expand Up @@ -250,7 +250,7 @@ where
cx.is_parent_changed.set(true);
}

Ref::map(cx.me(), |me| &me.content)
Signal::map(cx.me(), |me| &me.content)
}

fn name() -> Option<Cow<'static, str>> {
Expand Down
39 changes: 19 additions & 20 deletions src/ecs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
composer::{Composer, Update, Updater},
prelude::{Ref, *},
prelude::{Signal, *},
};
use bevy_app::{App, Plugin};
use bevy_ecs::{
Expand Down Expand Up @@ -153,22 +153,6 @@ where
}
}

#[derive(Data)]
struct CompositionContent<C> {
content: C,
target: Entity,
}

impl<C: Compose> Compose for CompositionContent<C> {
fn compose(cx: Scope<Self>) -> impl Compose {
use_provider(&cx, || SpawnContext {
parent_entity: cx.me().target,
});

Ref::map(cx.me(), |me| &me.content)
}
}

impl<C> Component for Composition<C>
where
C: Compose + Send + Sync + 'static,
Expand All @@ -181,12 +165,11 @@ where
let mut composition = world.get_mut::<Composition<C>>(entity).unwrap();

let content = composition.content.take().unwrap();

let target = composition.target.unwrap_or(entity);

let rt = world.non_send_resource_mut::<Runtime>();

let (tx, rx) = mpsc::channel();

rt.composers.borrow_mut().insert(
entity,
RuntimeComposer {
Expand All @@ -203,6 +186,22 @@ where
}
}

#[derive(Data)]
struct CompositionContent<C> {
content: C,
target: Entity,
}

impl<C: Compose> Compose for CompositionContent<C> {
fn compose(cx: Scope<Self>) -> impl Compose {
use_provider(&cx, || SpawnContext {
parent_entity: cx.me().target,
});

Signal::map(cx.me(), |me| &me.content)
}
}

fn compose(world: &mut World) {
let mut rt = world.non_send_resource_mut::<Runtime>();
rt.lock = None;
Expand Down Expand Up @@ -603,7 +602,7 @@ impl<C: Compose> Compose for Spawn<'_, C> {
}
});

Ref::map(cx.me(), |me| &me.content)
Signal::map(cx.me(), |me| &me.content)
}
}

Expand Down
Loading

0 comments on commit b2154cb

Please sign in to comment.