Skip to content

Commit

Permalink
fix: block subscribing skipped caused by failing to update `observed_…
Browse files Browse the repository at this point in the history
…blocks` (#410)

* fix: block subscribing skipped caused by failing to update `observed_blocks`

* chore: fix lint

* chore: bump jni version
  • Loading branch information
thorseraq authored Apr 27, 2023
1 parent 532c9be commit d0f1b1c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
2 changes: 1 addition & 1 deletion libs/jwst-binding/jwst-jni/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ publishing {
release(MavenPublication) {
groupId = 'com.toeverything'
artifactId = 'octobase'
version = '0.1.17'
version = '0.1.18'

afterEvaluate {
from components.release
Expand Down
28 changes: 16 additions & 12 deletions libs/jwst/src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,19 +161,23 @@ impl Block {
let block_id = self.block_id.clone();
let tx = block_observer_config.tx.clone();
let handle = block_observer_config.handle.clone();
let observed_blocks = block_observer_config.observed_blocks.clone();
let read_guard = observed_blocks.read().unwrap();
if !read_guard.contains(self.block_id.as_str()) {
debug!("subscribe block: {}", self.block_id);
let sub = self.block.observe_deep(move |_trx, _e| {
if handle.lock().unwrap().is_some() {
tx.send(block_id.clone())
.expect("send block observe message error");
match self.sub.read() {
Ok(sub_read_guard) => {
if sub_read_guard.is_none() {
debug!("subscribe block: {}", self.block_id);
let sub = self.block.observe_deep(move |_trx, _e| {
if handle.lock().unwrap().is_some() {
tx.send(block_id.clone())
.expect("send block observe message error");
}
});
drop(sub_read_guard);
*self.sub.write().unwrap() = Some(sub);
}
});
drop(read_guard);
observed_blocks.write().unwrap().insert(self.block_id.clone());
*self.sub.write().unwrap() = Some(sub);
}
Err(e) => {
error!("subscribe block error: {}", e);
}
}
}

Expand Down
2 changes: 0 additions & 2 deletions libs/jwst/src/workspace/block_observer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ pub struct BlockObserverConfig {
pub(super) modified_block_ids: Arc<RwLock<HashSet<String>>>,
pub(crate) handle: Arc<Mutex<Option<JoinHandle<()>>>>,
pub(crate) is_manually_tracking_block_changes: Arc<AtomicBool>,
pub(crate) observed_blocks: Arc<std::sync::RwLock<HashSet<String>>>,
pub(crate) is_observing: Arc<AtomicBool>,
}

Expand All @@ -50,7 +49,6 @@ impl BlockObserverConfig {
modified_block_ids,
handle: Arc::new(Mutex::new(None)),
is_manually_tracking_block_changes: Arc::default(),
observed_blocks: Arc::default(),
is_observing: Arc::default(),
};

Expand Down
28 changes: 27 additions & 1 deletion libs/jwst/src/workspace/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mod test {
}

#[test]
fn block_observe_callback() {
fn block_observe_callback_triggered_by_set() {
let workspace = Workspace::new("test");
workspace.set_callback(Box::new(|mut block_ids| {
block_ids.sort();
Expand All @@ -213,6 +213,32 @@ mod test {
sleep(Duration::from_millis(300));
}

#[test]
fn block_observe_callback_triggered_by_get() {
let workspace = Workspace::new("test");
workspace.set_callback(Box::new(|block_ids| {
assert_eq!(block_ids, vec!["block1".to_string()]);
}));

let block = workspace.with_trx(|mut t| {
let space = t.get_space("blocks");
let block = space.create(&mut t.trx, "block1", "text").unwrap();
block
});

drop(block);

let block = workspace.with_trx(|mut trx| {
trx.get_blocks().get(&trx.trx, "block1".to_string()).unwrap()
});

workspace.with_trx(|mut trx| {
block.set(&mut trx.trx, "key1", "value1").unwrap();
});

sleep(Duration::from_millis(300));
}

#[test]
fn manually_retrieve_modified_blocks() {
let workspace = Workspace::new("test");
Expand Down

1 comment on commit d0f1b1c

@vercel
Copy link

@vercel vercel bot commented on d0f1b1c Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.