SignalMapExt

Trait SignalMapExt 

Source
pub trait SignalMapExt: SignalMap {
    // Provided methods
    fn for_each<O, IOO, F, M>(self, system: F) -> ForEach<Self, O>
       where Self: Sized,
             Self::Key: 'static,
             Self::Value: 'static,
             O: Clone + 'static,
             IOO: Into<Option<O>> + 'static,
             F: IntoSystem<In<Vec<MapDiff<Self::Key, Self::Value>>>, IOO, M> + SSs { ... }
    fn map_value<O, F, M>(self, system: F) -> MapValue<Self, O>
       where Self: Sized,
             Self::Key: Clone + 'static,
             Self::Value: 'static,
             O: Clone + 'static,
             F: IntoSystem<In<Self::Value>, O, M> + SSs { ... }
    fn map_value_signal<S, F, M>(self, system: F) -> MapValueSignal<Self, S>
       where Self: Sized,
             Self::Key: Ord + Clone + SSs,
             Self::Value: 'static,
             S: Signal + Clone + 'static,
             S::Item: Clone + SSs,
             F: IntoSystem<In<Self::Value>, S, M> + SSs { ... }
    fn key(self, key: Self::Key) -> Key<Self>
       where Self: Sized,
             Self::Key: PartialEq + Clone + SSs,
             Self::Value: Clone + Send + 'static { ... }
    fn debug(self) -> Debug<Self>
       where Self: Sized,
             Self::Key: Debug + Clone + 'static,
             Self::Value: Debug + Clone + 'static { ... }
    fn boxed(self) -> Box<dyn SignalMap<Key = Self::Key, Value = Self::Value>>
       where Self: Sized { ... }
    fn boxed_clone(
        self,
    ) -> Box<dyn SignalMapDynClone<Key = Self::Key, Value = Self::Value> + Send + Sync>
       where Self: Sized + Clone { ... }
    fn register(self, world: &mut World) -> SignalHandle
       where Self: Sized { ... }
}
Expand description

Extension trait providing combinator methods for SignalMaps.

Provided Methods§

Source

fn for_each<O, IOO, F, M>(self, system: F) -> ForEach<Self, O>
where Self: Sized, Self::Key: 'static, Self::Value: 'static, O: Clone + 'static, IOO: Into<Option<O>> + 'static, F: IntoSystem<In<Vec<MapDiff<Self::Key, Self::Value>>>, IOO, M> + SSs,

Pass the “raw” Vec<MapDiff<Self::Key, Self::Value>> output of this SignalMap to a System, continuing propagation if the System returns Some or terminating for the frame if it returns None. This transforms the SignalMap into a Signal. Unlike most other SignalMap methods, .for_each, returns a Signal, not a SignalMap, since the output type need not be an Option<Vec<MapDiff>>. If the System logic is infallible, wrapping the result in an option is unnecessary.

Source

fn map_value<O, F, M>(self, system: F) -> MapValue<Self, O>
where Self: Sized, Self::Key: Clone + 'static, Self::Value: 'static, O: Clone + 'static, F: IntoSystem<In<Self::Value>, O, M> + SSs,

Pass each Value of this SignalMap to a System, transforming it.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

let mut world = World::new();
MutableBTreeMapBuilder::from([(1, 2), (3, 4)])
    .spawn(&mut world)
    .signal_map()
    .map_value(|In(x)| x * 2); // outputs `SignalMap -> {1: 2, 3: 4}`
Source

fn map_value_signal<S, F, M>(self, system: F) -> MapValueSignal<Self, S>
where Self: Sized, Self::Key: Ord + Clone + SSs, Self::Value: 'static, S: Signal + Clone + 'static, S::Item: Clone + SSs, F: IntoSystem<In<Self::Value>, S, M> + SSs,

Pass each Value of this SignalMap to a System that produces a Signal, forwarding the output of each resulting Signal.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

let mut world = World::new();
MutableBTreeMapBuilder::from([(1, 2), (3, 4)])
    .spawn(&mut world)
    .signal_map()
    .map_value_signal(|In(x)|
        SignalBuilder::from_system(move |_: In<()>| x * 2).dedupe()
    ); // outputs `SignalMap -> {1: 4, 3: 8}`
Source

fn key(self, key: Self::Key) -> Key<Self>
where Self: Sized, Self::Key: PartialEq + Clone + SSs, Self::Value: Clone + Send + 'static,

Maps this SignalMap to a Key-lookup Signal which outputs Some<Value> if the Key is present and None otherwise.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

let mut world = World::new();
MutableBTreeMapBuilder::from([(1, 2), (3, 4)])
    .spawn(&mut world)
    .signal_map()
    .key(1); // outputs `2`
Examples found in repository?
examples/letters.rs (line 138)
69fn ui_root() -> JonmoBuilder {
70    let active_save_signal = SignalBuilder::from_resource::<ActiveSave>().dedupe();
71
72    JonmoBuilder::from(Node {
73        height: Val::Percent(100.0),
74        width: Val::Percent(100.0),
75        justify_content: JustifyContent::Center,
76        ..default()
77    })
78    .child(
79        JonmoBuilder::from(Node {
80            flex_direction: FlexDirection::Column,
81            row_gap: Val::Px(GAP * 2.),
82            padding: UiRect::all(Val::Px(GAP * 4.)),
83            width: Val::Px(WindowResolution::default().physical_width() as f32),
84            justify_content: JustifyContent::Center,
85            ..default()
86        })
87        .child(
88            JonmoBuilder::from(Node {
89                align_self: AlignSelf::Center,
90                width: Val::Percent(100.),
91                ..default()
92            })
93            .child(
94                JonmoBuilder::from(Node {
95                    flex_direction: FlexDirection::Row,
96                    column_gap: Val::Px(GAP * 2.),
97                    ..default()
98                })
99                .children(SAVE_CHARS.chars().map(clone!((active_save_signal) move |save_char| {
100                    save_card(save_char, active_save_signal.clone())
101                }))),
102            )
103            .child(
104                sum_container()
105                .child(
106                    text_node()
107                        .insert((TextColor(BLUE), TextFont::from_font_size(LETTER_SIZE)))
108                        .with_component::<Node>(|mut node| node.height = Val::Px(100.))
109                        .component_signal(
110                            active_save_signal
111                                .clone()
112                                .switch_signal_vec(move |In(active_save): In<ActiveSave>, save_states: Res<SaveStates>| {
113                                    get_active_map(&save_states, active_save).signal_vec_entries()
114                                })
115                                .map_in(|(_, LetterData { count, .. })| count)
116                                .sum()
117                                .dedupe()
118                                .map_in_ref(ToString::to_string)
119                                .map_in(Text)
120                                .map_in(Some),
121                        )
122                ),
123            ),
124        )
125        .children(ROWS.into_iter().map(clone!((active_save_signal) move |row| {
126            JonmoBuilder::from(Node {
127                flex_direction: FlexDirection::Row,
128                column_gap: Val::Px(GAP * 2.),
129                ..default()
130            })
131            .children(row.chars().map(
132                clone!((active_save_signal) move |l| {
133                    let letter_data = active_save_signal
134                        .clone()
135                        .switch_signal_map(move |In(active_save): In<ActiveSave>, save_states: Res<SaveStates>| {
136                            get_active_map(&save_states, active_save).signal_map()
137                        })
138                        .key(l)
139                        .map_in(Option::unwrap_or_default);
140                    letter(l, letter_data)
141                }),
142            ))
143            .child(
144                sum_container()
145                .child(
146                    text_node()
147                        .insert((TextColor(BLUE), TextFont::from_font_size(LETTER_SIZE)))
148                        .component_signal(
149                            active_save_signal
150                                .clone()
151                                .switch_signal_vec(move |In(active_save): In<ActiveSave>, save_states: Res<SaveStates>| {
152                                    get_active_map(&save_states, active_save).signal_vec_entries()
153                                })
154                                .filter(move |In((letter, _))| row.contains(letter))
155                                .map_in(|(_, LetterData { count, .. })| count)
156                                .sum()
157                                .dedupe()
158                                .map_in_ref(ToString::to_string)
159                                .map_in(Text)
160                                .map_in(Some),
161                        ),
162                ),
163            )
164        }))),
165    )
166}
Source

fn debug(self) -> Debug<Self>
where Self: Sized, Self::Key: Debug + Clone + 'static, Self::Value: Debug + Clone + 'static,

Adds debug logging to this SignalMap’s raw MapDiff outputs.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

let mut world = World::new();
let mut map = MutableBTreeMapBuilder::from([(1, 2), (3, 4)]).spawn(&mut world);
let signal = map.signal_map().debug();
// `signal` logs `[ Replace { entries: [ (1, 2), (3, 4) ] } ]`
map.write(&mut world).insert(5, 6);
// `signal` logs `[ Insert { key: 5, value: 6 } ]` on next update
Source

fn boxed(self) -> Box<dyn SignalMap<Key = Self::Key, Value = Self::Value>>
where Self: Sized,

Erases the type of this SignalMap, allowing it to be used in conjunction with SignalMaps of other concrete types.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

let mut world = World::new();
let condition = true;
let signal = if condition {
    MutableBTreeMapBuilder::from([(1, 2), (3, 4)])
        .spawn(&mut world)
        .signal_map()
        .map_value(|In(x): In<i32>| x * 2)
        .boxed() // this is a `MapValue<Source<i32, i32>>`
} else {
    MutableBTreeMapBuilder::from([(1, 2), (3, 4)])
        .spawn(&mut world)
        .signal_map()
        .map_value_signal(|In(x): In<i32>| SignalBuilder::from_system(move |_: In<()>| x * 2))
        .boxed() // this is a `MapValueSignal<Source<i32, i32>>`
}; // without the `.boxed()`, the compiler would not allow this
Source

fn boxed_clone( self, ) -> Box<dyn SignalMapDynClone<Key = Self::Key, Value = Self::Value> + Send + Sync>
where Self: Sized + Clone,

Erases the type of this SignalMap, allowing it to be used in conjunction with SignalMaps of other concrete types, particularly in cases where the consumer requires Clone, e.g. .switch_signal_map.

§Example
use bevy_ecs::prelude::*;
use jonmo::prelude::*;

SignalBuilder::from_system(|_: In<()>| true).switch_signal_map(
    |In(condition): In<bool>, world: &mut World| {
        if condition {
            MutableBTreeMapBuilder::from([(1, 2), (3, 4)])
                .spawn(world)
                .signal_map()
                .map_value(|In(x): In<i32>| x * 2)
                .boxed_clone() // this is a `MapValue<Source<i32, i32>>`
        } else {
            MutableBTreeMapBuilder::from([(1, 2), (3, 4)])
                .spawn(world)
                .signal_map()
                .map_value_signal(|In(x): In<i32>| {
                    SignalBuilder::from_system(move |_: In<()>| x * 2)
                })
                .boxed_clone() // this is a `MapValueSignal<Source<i32, i32>>`
        } // without the `.boxed_clone()`, the compiler would not allow this
    },
);
Source

fn register(self, world: &mut World) -> SignalHandle
where Self: Sized,

Activate this SignalMap and all its upstreams, causing them to be evaluated every frame until they are SignalHandle::cleanup-ed, see SignalHandle.

Implementors§

Source§

impl<T> SignalMapExt for T
where T: SignalMap + ?Sized,