use std::cell::Cell;
use crabshell_reactive::{Owner, Runtime, Signal};
use crate::common::{PanicsOnDrop, count_drop, expect_drops, must_hold};
pub const PROBES_PER_WINDOW: usize = 3;
pub fn expect_drops_at_thread_exit(expected: usize) {
struct Check(Cell<usize>);
impl Drop for Check {
fn drop(&mut self) {
expect_drops(self.0.get());
}
}
thread_local! {
static CHECK: Check = const { Check(Cell::new(0)) };
}
CHECK.with(|check| check.0.set(expected));
}
struct ChildWidget(Owner);
impl Drop for ChildWidget {
fn drop(&mut self) {
count_drop();
self.0.dispose();
}
}
struct SignalProbe(Signal<i32>);
impl Drop for SignalProbe {
fn drop(&mut self) {
count_drop();
let signal = self.0;
signal.set(2);
signal.update(|n| *n += 1);
must_hold(signal.try_get().is_none() && !signal.try_set(4) && !signal.try_update(|n| *n += 1));
}
}
pub fn build_window_and_leave() {
let rt = Runtime::current();
let window = rt.new_owner();
let count = window.signal(1);
let child = ChildWidget(window.child());
let probe = SignalProbe(count);
window.signal(PanicsOnDrop);
window.effect(move || {
let _keep = (&child, &probe);
count.get();
});
rt.flush();
}