Skip to content

Commit

Permalink
feat: working crossbeam impl for memory provider (but ticks don't fir…
Browse files Browse the repository at this point in the history
…e immediately)
  • Loading branch information
lars-berger committed Nov 18, 2024
1 parent 94096ed commit 40d3cd3
Showing 1 changed file with 11 additions and 41 deletions.
52 changes: 11 additions & 41 deletions packages/desktop/src/providers/memory/memory_provider.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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::<String>();
match receiver1.recv_timeout(std::time::Duration::from_millis(100)) {
Ok(message) => {
println!("Received from receiver1: {}", message);
}
Err(_) => {} // No message, continue
}
}
}

0 comments on commit 40d3cd3

Please sign in to comment.