Skip to content

Commit

Permalink
fix(Rust): Republish patches to other subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
nokome committed Oct 9, 2021
1 parent 51259b4 commit a02b40e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
19 changes: 17 additions & 2 deletions rust/src/documents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,8 +666,23 @@ impl Document {
/// - `node_id`: the id of the node at the origin of the patch; defaults to `root`
/// - `patch`: the patch to apply
pub fn patch(&mut self, node_id: Option<String>, patch: &Patch) -> Result<()> {
let mut pointer = Self::resolve(&mut self.root, &self.addresses, node_id)?;
pointer.patch(patch)
let mut pointer = Self::resolve(&mut self.root, &self.addresses, node_id.clone())?;
pointer.patch(patch)?;

// TODO: Only generate and publish a DomPatch if there are subscribers
let dom_patch = DomPatch::new(&patch, node_id);
publish(
&["documents:", &self.id, ":patched"].concat(),
&DocumentEvent {
type_: DocumentEventType::Patched,
document: self.clone(),
content: None,
format: None,
patch: Some(dom_patch),
},
);

Ok(())
}

/// Execute the document, optionally providing a [`Patch`] to apply before execution, and
Expand Down
17 changes: 17 additions & 0 deletions rust/src/patches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ impl DomOperation {
$(to_html!($type);)*
}
}

// Types roughly ordered by expected incidence (more commonly used types in
// patches first)
to_html!(
Expand All @@ -693,6 +694,15 @@ impl DomOperation {
Null
);

// The value may be a JSON value (if this patch was sent from a client)
if let Some(value) = value.downcast_ref::<serde_json::Value>() {
if let Some(str) = value.as_str() {
return str.to_string()
} else {
return value.to_string()
}
}

tracing::error!("Unhandled value type when generating HTML for `DomOperation`");
"<span class=\"todo\">TODO</span>".to_string()
}
Expand All @@ -716,6 +726,8 @@ impl DomOperation {
$(to_json!($type);)*
}
}

// As above, types roughly ordered by expected incidence
to_json!(
InlineContent
BlockContent
Expand All @@ -730,6 +742,11 @@ impl DomOperation {
Null
);

// The value may be a JSON value (if this patch was sent from a client)
if let Some(value) = value.downcast_ref::<serde_json::Value>() {
return value.clone()
}

tracing::error!("Unhandled value type when generating JSON for `DomOperation`");
serde_json::Value::String("TODO".to_string())
}
Expand Down

0 comments on commit a02b40e

Please sign in to comment.