44// https://www.apache.org/licenses/LICENSE-2.0>. This file may not be copied, modified, or distributed
55// except according to those terms.
66
7+ use super :: TimerSignal ;
78use crate :: utils:: get_time_range;
89use crate :: Result ;
910
@@ -25,57 +26,61 @@ use std::{thread, thread::JoinHandle};
2526#[ derive( Debug , Default ) ]
2627pub struct Timer {
2728 /// A vector to store listeners (mpsc::Sender)
28- txs : Arc < Mutex < Vec < Sender < u64 > > > > ,
29+ txs : Arc < Mutex < Vec < Sender < TimerSignal > > > > ,
2930
3031 /// Thread handle
3132 pub handle : Option < JoinHandle < Result < ( ) > > > ,
3233}
3334
3435impl Timer {
3536 /// Initialize Timer and run a thread to send events to attached listeners
36- pub fn initialize ( self ) -> Result < Self > {
37- let txs = Arc :: clone ( & self . txs ) ;
37+ pub fn initialize ( cycle : Duration ) -> Result < Self > {
38+ let txs = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ;
3839
3940 // Add Default tx
40- let ( tx, _rx) : ( Sender < u64 > , Receiver < u64 > ) = channel ( ) ;
41+ let ( tx, _rx) : ( Sender < TimerSignal > , Receiver < TimerSignal > ) = channel ( ) ;
4142 txs. lock ( ) ?. push ( tx) ;
4243
4344 // Spawn a Thread
44- let handle = Some ( thread:: spawn ( move || {
45- // Get remaining time for 10th second fire event
46- let rem = get_time_range ( 0 ) ?. rem ;
45+ let handle = Some ( {
46+ let txs = txs. clone ( ) ;
4747
48- // Sleep for rem seconds
49- thread:: sleep ( Duration :: from_secs ( rem) ) ;
48+ thread:: spawn ( move || {
49+ // Get remaining time for 10th second fire event
50+ let rem = get_time_range ( 0 ) ?. rem ;
5051
51- loop {
52- // Exit thread if there are no listeners
53- if txs. lock ( ) ?. len ( ) == 0 {
54- return Ok ( ( ) ) ;
55- }
52+ // Sleep for rem seconds
53+ thread:: sleep ( Duration :: from_secs ( rem) ) ;
5654
57- // Get current time
58- let current = get_time_range ( 0 ) ?. from ;
55+ loop {
56+ // Exit thread if there are no listeners
57+ if txs. lock ( ) ?. len ( ) == 0 {
58+ return Ok ( ( ) ) ;
59+ }
5960
60- // Iterate through Senders
61- txs. lock ( ) ?. iter ( ) . for_each ( |tx| {
62- // Send event to attached Sender
63- let _res = tx. send ( current) ;
64- } ) ;
61+ // Get current time
62+ let current = TimerSignal :: NextSnapshot ( get_time_range ( 0 ) ?. from ) ;
6563
66- // Sleep for 10s
67- thread:: sleep ( Duration :: from_millis ( 10000 ) ) ;
68- }
69- } ) ) ;
64+ // Iterate through Senders
65+ txs. lock ( ) ?. iter ( ) . for_each ( |tx| {
66+ // Send event to attached Sender
67+ let _res = tx. send ( current) ;
68+ } ) ;
69+
70+ // Sleep for 10s
71+ thread:: sleep ( cycle) ;
72+ }
73+ } )
74+ } ) ;
7075
71- Ok ( Self { handle, .. self } )
76+ Ok ( Self { handle, txs } )
7277 }
7378
7479 /// Attach an mpsc::Sender to Timer
7580 ///
7681 /// Timer will dispatch an event with the timestamp of the current instant,
7782 /// every 10th second to all attached senders
78- pub fn attach_listener ( & mut self , tx : Sender < u64 > ) -> Result < ( ) > {
83+ pub fn attach_listener ( & mut self , tx : Sender < TimerSignal > ) -> Result < ( ) > {
7984 // Push Sender to a Vector of Sender(s)
8085 let txs = Arc :: clone ( & self . txs ) ;
8186 txs. lock ( ) ?. push ( tx) ;
0 commit comments