use rs_store::{BackpressurePolicy, DispatchOp, FnReducer, FnSubscriber, StoreBuilder};
use std::sync::Arc;
fn main() {
let predicate = Box::new(|value: &i32| {
println!("droppable {} ? {}", *value, *value < 5);
*value < 5 });
let policy = BackpressurePolicy::DropOldestIf(Some(predicate));
let store = StoreBuilder::new(0)
.with_reducer(Box::new(FnReducer::from(|state: &i32, action: &i32| {
std::thread::sleep(std::time::Duration::from_millis(200));
println!("reducer: {} + {}", state, action);
DispatchOp::Dispatch(state + action, vec![])
})))
.with_capacity(2) .with_policy(policy)
.build()
.unwrap();
store
.add_subscriber(Arc::new(FnSubscriber::from(|state: &i32, action: &i32| {
println!("subscriber: state: {}, action: {}", state, action);
})))
.unwrap();
println!("=== Predicate 기반 Backpressure 테스트 ===");
println!("채널 capacity: 2");
println!("predicate: 5보다 작은 값들은 drop");
println!("reducer 지연: 200ms");
println!();
let actions = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for action in actions {
println!("dispatch: {}", action);
match store.dispatch(action) {
Ok(_) => println!(" -> 성공"),
Err(e) => println!(" -> 실패: {:?}", e),
}
std::thread::sleep(std::time::Duration::from_millis(50));
}
match store.stop() {
Ok(_) => println!("store stopped"),
Err(e) => {
println!("store stop failed : {:?}", e);
panic!("store stop failed : {:?}", e);
}
}
let final_state = store.get_state();
println!();
println!("최종 상태: {}", final_state);
}