mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-24 05:48:59 +08:00
feat(native): add events feature for yocto
This commit is contained in:
@@ -36,6 +36,7 @@ thiserror = { workspace = true }
|
||||
[features]
|
||||
bench = []
|
||||
debug = []
|
||||
events = []
|
||||
large_refs = []
|
||||
serde_json = []
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
use super::{history::StoreHistory, publisher::DocPublisher, store::StoreRef, *};
|
||||
use super::{history::StoreHistory, store::StoreRef, *};
|
||||
use crate::sync::{Arc, RwLock};
|
||||
#[cfg(feature = "events")]
|
||||
use publisher::DocPublisher;
|
||||
|
||||
#[cfg(feature = "debug")]
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -133,6 +135,7 @@ pub struct Doc {
|
||||
opts: DocOptions,
|
||||
|
||||
pub(crate) store: StoreRef,
|
||||
#[cfg(feature = "events")]
|
||||
pub publisher: Arc<DocPublisher>,
|
||||
}
|
||||
|
||||
@@ -158,12 +161,14 @@ impl Doc {
|
||||
|
||||
pub fn with_options(options: DocOptions) -> Self {
|
||||
let store = Arc::new(RwLock::new(DocStore::with_client(options.client_id)));
|
||||
#[cfg(feature = "events")]
|
||||
let publisher = Arc::new(DocPublisher::new(store.clone()));
|
||||
|
||||
Self {
|
||||
client_id: options.client_id,
|
||||
opts: options,
|
||||
store,
|
||||
#[cfg(feature = "events")]
|
||||
publisher,
|
||||
}
|
||||
}
|
||||
@@ -368,14 +373,17 @@ impl Doc {
|
||||
self.store.read().unwrap().get_state_vector()
|
||||
}
|
||||
|
||||
#[cfg(feature = "events")]
|
||||
pub fn subscribe(&self, cb: impl Fn(&[u8], &[History]) + Sync + Send + 'static) {
|
||||
self.publisher.subscribe(cb);
|
||||
}
|
||||
|
||||
#[cfg(feature = "events")]
|
||||
pub fn unsubscribe_all(&self) {
|
||||
self.publisher.unsubscribe_all();
|
||||
}
|
||||
|
||||
#[cfg(feature = "events")]
|
||||
pub fn subscribe_count(&self) -> usize {
|
||||
self.publisher.count()
|
||||
}
|
||||
@@ -390,7 +398,6 @@ mod tests {
|
||||
use yrs::{types::ToJson, updates::decoder::Decode, Array, Map, Options, Transact};
|
||||
|
||||
use super::*;
|
||||
use crate::sync::{AtomicU8, Ordering};
|
||||
|
||||
#[test]
|
||||
fn test_encode_state_as_update() {
|
||||
@@ -519,8 +526,11 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[cfg(feature = "events")]
|
||||
#[ignore = "inaccurate timing on ci, need for more accurate timing testing"]
|
||||
fn test_subscribe() {
|
||||
use crate::sync::{AtomicU8, Ordering};
|
||||
|
||||
loom_model!({
|
||||
let doc = Doc::default();
|
||||
let doc_clone = doc.clone();
|
||||
|
||||
@@ -4,6 +4,7 @@ mod common;
|
||||
mod document;
|
||||
mod hasher;
|
||||
mod history;
|
||||
#[cfg(feature = "events")]
|
||||
mod publisher;
|
||||
mod store;
|
||||
mod types;
|
||||
|
||||
@@ -14,7 +14,7 @@ crate-type = ["cdylib"]
|
||||
anyhow = { workspace = true }
|
||||
napi = { workspace = true, features = ["anyhow", "napi4"] }
|
||||
napi-derive = { workspace = true }
|
||||
y-octo = { workspace = true, features = ["large_refs"] }
|
||||
y-octo = { workspace = true, features = ["events", "large_refs"] }
|
||||
|
||||
[build-dependencies]
|
||||
napi-build = { workspace = true }
|
||||
|
||||
Reference in New Issue
Block a user