Fix subscriptions for read only signals passed as props (#3173)

* add a fuzzing test for reference counted generational boxes

* Fix subscriptions for readonlysignal passed as props

* Don't subscribe to dropped contexts

---------

Co-authored-by: Jonathan Kelley <jkelleyrtp@gmail.com>
This commit is contained in:
Evan AlmloffandJonathan Kelley authored and GitHub committed 2024-11-10 21:35:33 -05:00
1 parent 5186d8dded
commit 45a1147ffd
8 files changed
+519 -191

No files matched your search

+14 -6
View File
@@ -5,7 +5,7 @@ use crate::{
Runtime,
};
use futures_channel::mpsc::UnboundedReceiver;
use generational_box::{GenerationalBox, SyncStorage};
use generational_box::{BorrowMutError, GenerationalBox, SyncStorage};
use std::{
cell::RefCell,
collections::HashSet,
@@ -243,11 +243,19 @@ impl ReactiveContext {
/// Subscribe to this context. The reactive context will automatically remove itself from the subscriptions when it is reset.
pub fn subscribe(&self, subscriptions: Arc<Mutex<HashSet<ReactiveContext>>>) {
subscriptions.lock().unwrap().insert(*self);
self.inner
.write()
.subscribers
.insert(PointerHash(subscriptions));
match self.inner.try_write() {
Ok(mut inner) => {
subscriptions.lock().unwrap().insert(*self);
inner.subscribers.insert(PointerHash(subscriptions));
}
// If the context was dropped, we don't need to subscribe to it anymore
Err(BorrowMutError::Dropped(_)) => {}
Err(expect) => {
panic!(
"Expected to be able to write to reactive context to subscribe, but it failed with: {expect:?}"
);
}
}
}
/// Get the scope that inner CopyValue is associated with