Skip to content

Commit

Permalink
update rw
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Aug 4, 2024
1 parent a4bd814 commit febc646
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
23 changes: 8 additions & 15 deletions src/asyncv/attribute/message/rw_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,20 @@ impl<TYPE: MessagePayloadManager> MessageAttributeRw<TYPE> {
Ok(self)
}

// wait_change()
// wait_change_then()
pub async fn wait_change(&self) {
let change_notifier = self.inner.lock().await.base.clone_change_notifier();
change_notifier.notified().await
}

pub fn when_change<F>(&self, function: F)
pub async fn wait_change_then<F>(&self, function: F)
where
F: Future<Output = ()> + Send + 'static,
// F: Future<Output = Result<bool, ()>> + Send + 'static,
{
tokio::spawn(async move {
sleep(Duration::from_secs(1)).await;
function.await
});
let change_notifier = self.inner.lock().await.base.clone_change_notifier();
change_notifier.notified().await;
function.await;
}

// pub async fn on_change_handler(&self, handler: Box<dyn OnChangeHandler>) {
// self.inner.lock().await.on_change_handler(handler);
// }
// pub async fn on_change(&self, handler: OnChangeHandlerFunction) {
// self.inner.lock().await.on_change(handler);
// }

/// Set the value of the attribute
///
pub async fn set<I: Into<TYPE>>(&self, value: I) -> Result<(), AttributeError> {
Expand Down
2 changes: 1 addition & 1 deletion src/asyncv/attribute/message/rw_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use tokio::sync::Notify;
/// This inner implementation allow the public part to be cloneable easly
pub struct MessageAttributeRwInner<TYPE: MessagePayloadManager> {
/// Rw is based on Ro
base: MessageAttributeRoInner<TYPE>,
pub base: MessageAttributeRoInner<TYPE>,

/// The topic for commands
topic_cmd: String,
Expand Down
20 changes: 20 additions & 0 deletions src/examples/test_async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ async fn main() {
.finish_with_message_type::<BooleanMessage>()
.await;

let rw_bool = reactor
.create_new_attribute()
.with_topic("o")
.with_rw_access()
.finish_with_message_type::<BooleanMessage>()
.await;

rw_bool.set(true).await.unwrap();

// Wait then execute the function once
let ro_bool_bis = ro_bool.clone();
ro_bool
Expand All @@ -42,5 +51,16 @@ async fn main() {
}
});

// Task that run an action every time the value of the attribute change
tokio::spawn(async move {
loop {
rw_bool
.wait_change_then(async move {
println!("cooucou depuis o");
})
.await;
}
});

sleep(Duration::from_secs(60)).await;
}

0 comments on commit febc646

Please sign in to comment.