/// Initialize Timer and run a thread to send events to attached listeners
Initialize Timer and run a thread to send events to attached listeners
Initialize Timer and run a thread to send events to attached listeners
pub fn initialize(cycle: Duration) -> Result<Self> {
        let txs = Arc::new(Mutex::new(Vec::new()));
        // Add Default tx
        let (tx, _rx): (Sender<TimerSignal>, Receiver<TimerSignal>) = channel();
        txs.lock()?.push(tx);
        // Spawn a Thread
        let handle = Some({
            let txs = txs.clone();
            thread::spawn(move || {
                // Get remaining time for 10th second fire event
                let rem = get_time_range(0)?.rem;
                // Sleep for rem seconds
                thread::sleep(Duration::from_secs(rem));
                loop {
                    // Exit thread if there are no listeners
                    if txs.lock()?.len() == 0 {
                        return Ok(());
                    }
                    // Get current time
                    let current = TimerSignal::NextSnapshot(get_time_range(0)?.from);
                    // Iterate through Senders
                    txs.lock()?.iter().for_each(|tx| {
                        // Send event to attached Sender
                        let _res = tx.send(current);
                    });
                    // Sleep for 10s
                    thread::sleep(cycle);
                }
            })
        });
        Ok(Self { handle, txs })
    }
pub fn initialize ( cycle : Duration ) -> Result < Self > { let txs = Arc :: new ( Mutex :: new ( Vec :: new ( ) ) ) ; // Add Default tx let ( tx , _rx ) : ( Sender < TimerSignal > , Receiver < TimerSignal > ) = channel ( ) ; txs . lock ( ) ? . push ( tx ) ; // Spawn a Thread let handle = Some ( { let txs = txs . clone ( ) ; thread :: spawn ( move | | { // Get remaining time for 10th second fire event let rem = get_time_range ( 0 ) ? . rem ; // Sleep for rem seconds thread :: sleep ( Duration :: from_secs ( rem ) ) ; loop { // Exit thread if there are no listeners if txs . lock ( ) ? . len ( ) == 0 { return Ok ( ( ) ) ; } // Get current time let current = TimerSignal :: NextSnapshot ( get_time_range ( 0 ) ? . from ) ; // Iterate through Senders txs . lock ( ) ? . iter ( ) . for_each ( | tx | { // Send event to attached Sender let _res = tx . send ( current ) ; } ) ; // Sleep for 10s thread :: sleep ( cycle ) ; } } ) } ) ; Ok ( Self { handle , txs } ) }
Initialize Timer and run a thread to send events to attached listeners
Initialize Timer and run a thread to send events to attached listeners
cycle
Duration