use std::sync::Arc;
use std::sync::atomic::AtomicBool;
use crate::inner::{CallbackEntry, TextDocumentInner};
#[derive(Debug, Clone, PartialEq)]
pub enum DocumentEvent {
ContentsChanged {
position: usize,
chars_removed: usize,
chars_added: usize,
blocks_affected: usize,
},
FormatChanged {
position: usize,
length: usize,
kind: crate::flow::FormatChangeKind,
},
BlockCountChanged(usize),
FlowElementsInserted { flow_index: usize, count: usize },
FlowElementsRemoved { flow_index: usize, count: usize },
DocumentReset,
UndoRedoChanged { can_undo: bool, can_redo: bool },
ModificationChanged(bool),
LongOperationProgress {
operation_id: String,
percent: f64,
message: String,
},
LongOperationFinished {
operation_id: String,
success: bool,
error: Option<String>,
},
}
pub struct Subscription {
alive: Arc<AtomicBool>,
}
impl Drop for Subscription {
fn drop(&mut self) {
self.alive
.store(false, std::sync::atomic::Ordering::Relaxed);
}
}
pub(crate) fn subscribe_inner<F>(inner: &mut TextDocumentInner, callback: F) -> Subscription
where
F: Fn(DocumentEvent) + Send + Sync + 'static,
{
let alive = Arc::new(AtomicBool::new(true));
inner.callbacks.push(CallbackEntry {
alive: Arc::downgrade(&alive),
callback: Arc::new(callback),
});
Subscription { alive }
}