Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 32 additions & 6 deletions pkgs/id/crates/id-web/src/collab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ use std::{
};
use tokio::sync::{Mutex, RwLock, broadcast};

use super::collab_steps::{StepRefusal, StepSchema, check_steps, document_content_size};
use super::collab_steps::{
DocModel, StepRefusal, StepSchema, check_steps_in_doc, document_content_size,
};
use super::content_mode::{ContentMode, detect_mode_with_content};
use super::identity::{sanitize_name, short_id};
use super::markdown::{
Expand Down Expand Up @@ -260,6 +262,12 @@ pub struct Document {
/// every accepted step (#1618). Changed only under the `steps`
/// write lock, alongside the history it describes.
pub content_size: AtomicU64,
/// The step-application model of the document (#1618): the base
/// document with every accepted step applied to it, paired with
/// the base it was built from, so a replaced base rebuilds it.
/// Changed only under the `steps` write lock, alongside
/// `content_size`.
pub doc_model: RwLock<(serde_json::Value, DocModel)>,
/// Active cursor positions by client ID.
pub cursors: RwLock<HashMap<String, CursorPosition>>,
/// Broadcast channel for sending updates to clients.
Expand Down Expand Up @@ -288,9 +296,11 @@ impl Document {
pub fn with_doc_and_mode(doc: serde_json::Value, mode: ContentMode) -> Self {
let (tx, _) = broadcast::channel(256);
let content_size = document_content_size(&doc, mode);
let doc_model = DocModel::from_doc(&doc, mode);

Self {
version: AtomicU64::new(0),
doc_model: RwLock::new((doc.clone(), doc_model)),
doc: RwLock::new(doc),
mode,
steps: RwLock::new(Vec::new()),
Expand Down Expand Up @@ -331,9 +341,10 @@ impl Document {
/// [`StepsRejected::VersionMismatch`] with the current version when
/// `base_version` is stale, and [`StepsRejected::Invalid`] when a step
/// breaks the step contract (#1618): an unknown step type, a node or
/// mark outside the document's schema, or a position outside the
/// document. A rejected batch stores nothing and leaves the version
/// and content size unchanged.
/// mark outside the document's schema, a position outside the
/// document, or a step that does not apply to the document as it
/// stands at that point in the batch. A rejected batch stores nothing
/// and leaves the version and content size unchanged.
pub async fn try_apply_steps(
&self,
base_version: u64,
Expand All @@ -354,8 +365,23 @@ impl Document {
reason: format!("a {} document does not take edits", self.mode.as_str()),
}));
};
let new_size = check_steps(schema, self.content_size.load(Ordering::SeqCst), steps)
.map_err(StepsRejected::Invalid)?;
let new_size = {
let base = self.doc.read().await.clone();
let mut doc_model = self.doc_model.write().await;
if doc_model.0 != base {
// The base document was replaced wholesale (the
// checkpoint tests do this to simulate an edit made
// outside the step history): rebuild the model from
// the new base before applying anything to it.
doc_model.1 = DocModel::from_doc(&base, self.mode);
doc_model.0 = base;
}
let model = check_steps_in_doc(schema, &doc_model.1, steps)
.map_err(StepsRejected::Invalid)?;
let size = model.content_size();
doc_model.1 = model;
size
};
self.content_size.store(new_size, Ordering::SeqCst);
for step in steps {
doc_steps.push((
Expand Down
Loading