@@ -1521,12 +1521,15 @@ pub trait SignalExt: Signal {
15211521 Throttle {
15221522 signal : self . map (
15231523 move |In ( item) : In < Self :: Item > , time : Res < Time > , mut timer_option : Local < Option < Timer > > | {
1524+ bevy_log:: info!( "delta y: {:?}" , time. delta( ) ) ;
15241525 match timer_option. as_mut ( ) {
15251526 None => {
15261527 * timer_option = Some ( Timer :: new ( duration, TimerMode :: Once ) ) ;
1528+ bevy_log:: info!( "delta f: {:?}" , time. delta( ) ) ;
15271529 Some ( item)
15281530 }
15291531 Some ( timer) => {
1532+ bevy_log:: info!( "delta: {:?}" , time. delta( ) ) ;
15301533 if timer. tick ( time. delta ( ) ) . finished ( ) {
15311534 timer. reset ( ) ;
15321535 Some ( item)
@@ -1976,10 +1979,11 @@ mod tests {
19761979 // Import Bevy prelude for MinimalPlugins and other common items
19771980 use bevy:: prelude:: * ;
19781981 use bevy_platform:: sync:: * ;
1979- use bevy_time:: TimeUpdateStrategy ;
1982+ use bevy_time:: TimePlugin ;
19801983
19811984 // Add Duration
19821985 use core:: time:: Duration ;
1986+ use test_log:: test;
19831987
19841988 // Helper component and resource for testing Add Default
19851989 #[ derive( Component , Clone , Debug , PartialEq , Reflect , Default , Resource ) ]
@@ -2641,12 +2645,15 @@ mod tests {
26412645
26422646 #[ test]
26432647 fn test_throttle ( ) {
2644- let mut app = create_test_app ( ) ;
2648+ let mut app = App :: new ( ) ;
2649+ app. add_plugins ( ( MinimalPlugins . build ( ) . disable :: < TimePlugin > ( ) , JonmoPlugin ) ) ;
2650+ app. init_resource :: < Time > ( ) ; // manually managing time for the test
26452651 app. init_resource :: < SignalOutput < i32 > > ( ) ;
26462652
26472653 // A simple counter to generate a new value (1, 2, 3...) for each update.
26482654 let source_signal = SignalBuilder :: from_system ( |_: In < ( ) > , mut counter : Local < i32 > | {
26492655 * counter += 1 ;
2656+ bevy_log:: info!( "fuck" ) ;
26502657 * counter
26512658 } ) ;
26522659
@@ -2655,68 +2662,71 @@ mod tests {
26552662 . map ( capture_output)
26562663 . register ( app. world_mut ( ) ) ;
26572664
2658- // 1. Initial emission: The first value should always pass through.
2665+ // 1. Initial emission: The first update runs with near-zero delta. The first value should always
2666+ // pass.
26592667 app. update ( ) ;
26602668 assert_eq ! (
2661- get_output( & app. world( ) ) ,
2669+ get_output( app. world( ) ) ,
26622670 Some ( 1 ) ,
26632671 "First value (1) should pass immediately."
26642672 ) ;
26652673 // Clear the output to prepare for the next check.
26662674 app. world_mut ( ) . resource_mut :: < SignalOutput < i32 > > ( ) . 0 = None ;
26672675
2668- // 2. Blocked emission: Advance time by less than the duration. The next value (2) should be
2669- // blocked.
2676+ // 2. Blocked emission: Manually advance time by 50ms.
26702677 app. world_mut ( )
2671- . insert_resource ( TimeUpdateStrategy :: ManualDuration ( Duration :: from_millis ( 50 ) ) ) ;
2672- app. update ( ) ;
2678+ . resource_mut :: < Time > ( )
2679+ . advance_by ( Duration :: from_millis ( 50 ) ) ;
2680+ app. update ( ) ; // `time.delta()` is now 50ms. The timer ticks but isn't finished.
26732681 assert_eq ! (
2674- get_output:: <i32 >( & app. world( ) ) ,
2682+ get_output:: <i32 >( app. world( ) ) ,
26752683 None ,
26762684 "Value (2) emitted after 50ms should be blocked."
26772685 ) ;
26782686
2679- // 3. Another blocked emission: Update again with no time elapsed. The next value (3) should also be
2680- // blocked.
2687+ // 3. Another blocked emission: Advance time by another 40ms (total 90ms).
26812688 app. world_mut ( )
2682- . insert_resource ( TimeUpdateStrategy :: ManualDuration ( Duration :: from_millis ( 0 ) ) ) ;
2683- app. update ( ) ;
2689+ . resource_mut :: < Time > ( )
2690+ . advance_by ( Duration :: from_millis ( 40 ) ) ;
2691+ app. update ( ) ; // `time.delta()` is 40ms. Timer ticks to 90ms. Still not finished.
26842692 assert_eq ! (
2685- get_output:: <i32 >( & app. world( ) ) ,
2693+ get_output:: <i32 >( app. world( ) ) ,
26862694 None ,
2687- "Value (3) emitted immediately after should also be blocked."
2695+ "Value (3) emitted after 90ms total should be blocked."
26882696 ) ;
2689-
2690- // 4. Allowed emission: Advance time past the threshold (50ms from step 2 + 60ms now = 110ms total ).
2697+ app . world_mut ( ) . entity ( * * handle ) ;
2698+ // 4. Allowed emission: Advance time by another 20ms (total 110ms).
26912699 app. world_mut ( )
2692- . insert_resource ( TimeUpdateStrategy :: ManualDuration ( Duration :: from_millis ( 60 ) ) ) ;
2693- app. update ( ) ;
2700+ . resource_mut :: < Time > ( )
2701+ . advance_by ( Duration :: from_millis ( 20 ) ) ;
2702+ app. update ( ) ; // `time.delta()` is 20ms. Timer ticks to 110ms, which is >= 100ms.
26942703 // The source signal has been called 4 times now. The value should be 4.
26952704 assert_eq ! (
2696- get_output( & app. world( ) ) ,
2705+ get_output( app. world( ) ) ,
26972706 Some ( 4 ) ,
26982707 "Value (4) should pass after total duration > 100ms."
26992708 ) ;
27002709 app. world_mut ( ) . resource_mut :: < SignalOutput < i32 > > ( ) . 0 = None ;
27012710
2702- // 5. Blocked again: Immediately after an emission, the timer resets, so the next value (5) is
2703- // blocked.
2711+ // 5. Blocked again: The timer was reset on the last pass. A small advance is not enough.
27042712 app. world_mut ( )
2705- . insert_resource ( TimeUpdateStrategy :: ManualDuration ( Duration :: from_millis ( 10 ) ) ) ;
2713+ . resource_mut :: < Time > ( )
2714+ . advance_by ( Duration :: from_millis ( 10 ) ) ;
27062715 app. update ( ) ;
27072716 assert_eq ! (
2708- get_output:: <i32 >( & app. world( ) ) ,
2717+ get_output:: <i32 >( app. world( ) ) ,
27092718 None ,
27102719 "Value (5) immediately after a pass should be blocked again."
27112720 ) ;
27122721
27132722 // 6. Pass again: Advance time to pass the threshold again.
27142723 app. world_mut ( )
2715- . insert_resource ( TimeUpdateStrategy :: ManualDuration ( Duration :: from_millis ( 100 ) ) ) ;
2724+ . resource_mut :: < Time > ( )
2725+ . advance_by ( Duration :: from_millis ( 100 ) ) ;
27162726 app. update ( ) ;
27172727 // The source signal has now been called 6 times. The value should be 6.
27182728 assert_eq ! (
2719- get_output( & app. world( ) ) ,
2729+ get_output( app. world( ) ) ,
27202730 Some ( 6 ) ,
27212731 "Value (6) should pass again after another full duration."
27222732 ) ;
0 commit comments