From 40d3cd33992a13880bde3192ba996c7828537d46 Mon Sep 17 00:00:00 2001 From: Lars Berger Date: Mon, 18 Nov 2024 23:11:02 +0800 Subject: [PATCH] feat: working crossbeam impl for memory provider (but ticks don't fire immediately) --- .../src/providers/memory/memory_provider.rs | 52 ++++--------------- 1 file changed, 11 insertions(+), 41 deletions(-) diff --git a/packages/desktop/src/providers/memory/memory_provider.rs b/packages/desktop/src/providers/memory/memory_provider.rs index 63bfc9a5..d13ff488 100644 --- a/packages/desktop/src/providers/memory/memory_provider.rs +++ b/packages/desktop/src/providers/memory/memory_provider.rs @@ -1,8 +1,10 @@ +use std::time::Duration; + +use crossbeam::channel::tick; use serde::{Deserialize, Serialize}; -use crate::{ - common::SyncInterval, - providers::{CommonProviderState, Provider, RuntimeType}, +use crate::providers::{ + CommonProviderState, Provider, ProviderInputMsg, RuntimeType, }; #[derive(Deserialize, Debug)] @@ -62,53 +64,21 @@ impl Provider for MemoryProvider { } fn start_sync(&mut self) { - let mut interval = tokio::time::interval(Duration::from_secs(1)); - interval - .set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); + let ticker = tick(Duration::from_millis(self.config.refresh_interval)); loop { crossbeam::select! { - recv(interval) -> _ => { + recv(ticker) -> _ => { let output = self.run_interval(); self.common.emitter.emit_output(output); } - recv(self.common.message_rx) -> cmd => { - match cmd { - IncomingProviderMessage::Stop => break, - IncomingProviderMessage::Function(function, response_tx) => { - let result = self.call_function_sync(function); - let _ = response_tx.send(result); - } + recv(self.common.input.sync_rx) -> input => { + match input { + Ok(ProviderInputMsg::Stop) => break, + _ => {} } } } } - - // let mut interval = SyncInterval::new(self.config.refresh_interval); - // let (tick_tx, tick_rx) = mpsc::channel(); - - // Spawn timer thread - // std::thread::spawn(move || { - // let interval = - // Duration::from_millis(self.config.refresh_interval); loop { - // std::thread::sleep(interval); - // if tick_tx.send(()).is_err() { - // break; - // } - // } - // }); - - while let Ok(message) = self.common.input.try_recv() { - let output = self.run_interval(); - self.common.emitter.emit_output(output); - } - - let (sender1, receiver1) = std::sync::mpsc::channel::(); - match receiver1.recv_timeout(std::time::Duration::from_millis(100)) { - Ok(message) => { - println!("Received from receiver1: {}", message); - } - Err(_) => {} // No message, continue - } } }