utils: Return the old value in the set method of ChannelObservable

This commit is contained in:
Damir Jelić
2024-06-17 14:24:20 +02:00
parent 7fee66da11
commit a819dd568d

View File

@@ -76,10 +76,16 @@ impl<T: 'static + Send + Clone> ChannelObservable<T> {
}
/// Set the underlying data to the new value.
pub(crate) fn set(&self, new_value: T) {
*self.value.write().unwrap() = new_value.to_owned();
pub(crate) fn set(&self, new_value: T) -> T {
let old_value = {
let mut guard = self.value.write().unwrap();
std::mem::replace(&mut (*guard), new_value.clone())
};
// We're ignoring the error case where no receivers exist.
let _ = self.channel.send(new_value);
old_value
}
/// Get the current value of the underlying data.
@@ -183,3 +189,17 @@ impl IntoRawStateEventContent for &Box<RawJsonValue> {
self.clone().into_raw_state_event_content()
}
}
#[cfg(test)]
mod test {
#[cfg(feature = "e2e-encryption")]
#[test]
fn test_channel_observable_get_set() {
let observable = super::ChannelObservable::new(0);
assert_eq!(observable.get(), 0);
assert_eq!(observable.set(1), 0);
assert_eq!(observable.set(10), 1);
assert_eq!(observable.get(), 10);
}
}