feat(native): add events feature for yocto

This commit is contained in:
DarkSky
2025-11-15 21:16:57 +08:00
parent e52bcb7fd6
commit 29cb937493
4 changed files with 15 additions and 3 deletions

View File

@@ -36,6 +36,7 @@ thiserror = { workspace = true }
[features] [features]
bench = [] bench = []
debug = [] debug = []
events = []
large_refs = [] large_refs = []
serde_json = [] serde_json = []

View File

@@ -1,5 +1,7 @@
use super::{history::StoreHistory, publisher::DocPublisher, store::StoreRef, *}; use super::{history::StoreHistory, store::StoreRef, *};
use crate::sync::{Arc, RwLock}; use crate::sync::{Arc, RwLock};
#[cfg(feature = "events")]
use publisher::DocPublisher;
#[cfg(feature = "debug")] #[cfg(feature = "debug")]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@@ -133,6 +135,7 @@ pub struct Doc {
opts: DocOptions, opts: DocOptions,
pub(crate) store: StoreRef, pub(crate) store: StoreRef,
#[cfg(feature = "events")]
pub publisher: Arc<DocPublisher>, pub publisher: Arc<DocPublisher>,
} }
@@ -158,12 +161,14 @@ impl Doc {
pub fn with_options(options: DocOptions) -> Self { pub fn with_options(options: DocOptions) -> Self {
let store = Arc::new(RwLock::new(DocStore::with_client(options.client_id))); let store = Arc::new(RwLock::new(DocStore::with_client(options.client_id)));
#[cfg(feature = "events")]
let publisher = Arc::new(DocPublisher::new(store.clone())); let publisher = Arc::new(DocPublisher::new(store.clone()));
Self { Self {
client_id: options.client_id, client_id: options.client_id,
opts: options, opts: options,
store, store,
#[cfg(feature = "events")]
publisher, publisher,
} }
} }
@@ -368,14 +373,17 @@ impl Doc {
self.store.read().unwrap().get_state_vector() self.store.read().unwrap().get_state_vector()
} }
#[cfg(feature = "events")]
pub fn subscribe(&self, cb: impl Fn(&[u8], &[History]) + Sync + Send + 'static) { pub fn subscribe(&self, cb: impl Fn(&[u8], &[History]) + Sync + Send + 'static) {
self.publisher.subscribe(cb); self.publisher.subscribe(cb);
} }
#[cfg(feature = "events")]
pub fn unsubscribe_all(&self) { pub fn unsubscribe_all(&self) {
self.publisher.unsubscribe_all(); self.publisher.unsubscribe_all();
} }
#[cfg(feature = "events")]
pub fn subscribe_count(&self) -> usize { pub fn subscribe_count(&self) -> usize {
self.publisher.count() self.publisher.count()
} }
@@ -390,7 +398,6 @@ mod tests {
use yrs::{types::ToJson, updates::decoder::Decode, Array, Map, Options, Transact}; use yrs::{types::ToJson, updates::decoder::Decode, Array, Map, Options, Transact};
use super::*; use super::*;
use crate::sync::{AtomicU8, Ordering};
#[test] #[test]
fn test_encode_state_as_update() { fn test_encode_state_as_update() {
@@ -519,8 +526,11 @@ mod tests {
} }
#[test] #[test]
#[cfg(feature = "events")]
#[ignore = "inaccurate timing on ci, need for more accurate timing testing"] #[ignore = "inaccurate timing on ci, need for more accurate timing testing"]
fn test_subscribe() { fn test_subscribe() {
use crate::sync::{AtomicU8, Ordering};
loom_model!({ loom_model!({
let doc = Doc::default(); let doc = Doc::default();
let doc_clone = doc.clone(); let doc_clone = doc.clone();

View File

@@ -4,6 +4,7 @@ mod common;
mod document; mod document;
mod hasher; mod hasher;
mod history; mod history;
#[cfg(feature = "events")]
mod publisher; mod publisher;
mod store; mod store;
mod types; mod types;

View File

@@ -14,7 +14,7 @@ crate-type = ["cdylib"]
anyhow = { workspace = true } anyhow = { workspace = true }
napi = { workspace = true, features = ["anyhow", "napi4"] } napi = { workspace = true, features = ["anyhow", "napi4"] }
napi-derive = { workspace = true } napi-derive = { workspace = true }
y-octo = { workspace = true, features = ["large_refs"] } y-octo = { workspace = true, features = ["events", "large_refs"] }
[build-dependencies] [build-dependencies]
napi-build = { workspace = true } napi-build = { workspace = true }