use std::{cell::Cell, rc::Rc};
use crate::{App, Global, TestApp};
struct Watched;
impl Global for Watched {}
struct Watcher;
#[derive(Clone, Copy, Debug)]
enum Watch {
Global,
GlobalFromEntity,
Entity,
}
#[test]
fn an_observer_hears_a_change_made_after_it_was_made_behind_a_waiting_notice() {
for (watch, change_after, heard) in [
(Watch::Global, true, 1),
(Watch::Global, false, 0),
(Watch::GlobalFromEntity, true, 1),
(Watch::GlobalFromEntity, false, 0),
(Watch::Entity, true, 1),
(Watch::Entity, false, 0),
] {
let mut app = TestApp::new();
app.set_global(Watched);
let watched = app.new_entity(|_| Watched);
let watcher = app.new_entity(|_| Watcher);
let earlier_calls = Rc::new(Cell::new(0));
let counter = earlier_calls.clone();
let _earlier = app.update(|cx| match watch {
Watch::Entity => cx.observe(&watched, move |_, _| counter.set(counter.get() + 1)),
Watch::Global | Watch::GlobalFromEntity => {
cx.observe_global::<Watched>(move |_| counter.set(counter.get() + 1))
}
});
let calls = Rc::new(Cell::new(0));
app.update(|cx| {
let change = |cx: &mut App| match watch {
Watch::Entity => watched.update(cx, |_, cx| cx.notify()),
Watch::Global | Watch::GlobalFromEntity => cx.set_global(Watched),
};
change(cx);
let counter = calls.clone();
match watch {
Watch::Global => cx
.observe_global::<Watched>(move |_| counter.set(counter.get() + 1))
.detach(),
Watch::GlobalFromEntity => watcher.update(cx, |_, cx| {
cx.observe_global::<Watched>(move |_, _| counter.set(counter.get() + 1))
.detach()
}),
Watch::Entity => cx
.observe(&watched, move |_, _| counter.set(counter.get() + 1))
.detach(),
}
if change_after {
change(cx);
}
});
assert_eq!(
calls.get(),
heard,
"{watch:?}, changed again after the observer was made: {change_after}"
);
assert_eq!(
earlier_calls.get(),
1,
"{watch:?}: an observer made before the update hears it once"
);
}
}
#[test]
fn an_entity_observer_hears_a_notify_made_after_it_was_made_behind_a_waiting_notice() {
for (notify_after, heard) in [(true, 1), (false, 0)] {
let mut app = TestApp::new();
let watched = app.new_entity(|_| Watched);
let watcher = app.new_entity(|_| Watcher);
let calls = Rc::new(Cell::new(0));
app.update(|cx| {
watched.update(cx, |_, cx| cx.notify());
let counter = calls.clone();
watcher.update(cx, |_, cx| {
cx.observe(&watched, move |_, _, _| counter.set(counter.get() + 1))
.detach()
});
if notify_after {
watched.update(cx, |_, cx| cx.notify());
}
});
assert_eq!(
calls.get(),
heard,
"notified again after the observer was made: {notify_after}"
);
}
}