mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-12-01 15:53:26 -10:00
Share catalog networks under fixed IDs and fork them before a write (#4898)
* Share catalog networks under fixed IDs and fork them before a write * Fork catalog entries before signature edits collect instances and keep view state shared
This commit is contained in:
1 parent
9e9ec56aa3
commit
ebc986caa8
14 files changed
+469
-57
No files matched your search
Generated
+1
@@ -2968,6 +2968,7 @@ version = "0.0.0"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"bitflags 2.13.2",
|
||||
"blake3",
|
||||
"color",
|
||||
"core-types",
|
||||
"derivative",
|
||||
|
||||
@@ -28,6 +28,7 @@ migration-nodes = { workspace = true }
|
||||
document-graph-storage = { workspace = true, features = ["conversion"] }
|
||||
document-container = { workspace = true }
|
||||
graphene-hash = { workspace = true }
|
||||
blake3 = { workspace = true }
|
||||
interpreted-executor = { workspace = true }
|
||||
graphene-std = { workspace = true } # NOTE: `core-types` should not be added here because `graphene-std` re-exports its contents
|
||||
preprocessor = { workspace = true }
|
||||
|
||||
@@ -808,6 +808,14 @@ mod test {
|
||||
let mut editor = EditorTestUtils::create();
|
||||
editor.new_document().await;
|
||||
editor.draw_rect(0., 0., 100., 100.).await;
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
let rectangle = interface
|
||||
.document_network()
|
||||
.nodes
|
||||
.iter()
|
||||
.find_map(|(id, node)| matches!(node.implementation, graph_craft::document::DocumentNodeImplementation::Definition(_)).then_some(*id))
|
||||
.expect("the rectangle layer");
|
||||
assert!(interface.make_unique(&rectangle, &[]), "a catalog network forks on every write, so it never diverges");
|
||||
let clipboard = copy_layers_to_clipboard(&mut editor).await;
|
||||
|
||||
editor.new_document().await;
|
||||
|
||||
@@ -1611,6 +1611,14 @@ pub fn collect_node_descriptions() -> Vec<(String, String)> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The identifiers of the catalog's network nodes.
|
||||
pub fn network_node_identifiers() -> impl Iterator<Item = &'static str> {
|
||||
DOCUMENT_NODE_TYPES.keys().filter_map(|identifier| match identifier {
|
||||
DefinitionIdentifier::Network(name) => Some(name.as_str()),
|
||||
DefinitionIdentifier::ProtoNode(_) => None,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn collect_node_icons() -> Vec<(String, IconName)> {
|
||||
DOCUMENT_NODE_TYPES
|
||||
.iter()
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
mod apply;
|
||||
mod caches;
|
||||
mod catalog;
|
||||
#[cfg(test)]
|
||||
mod catalog_tests;
|
||||
#[cfg(test)]
|
||||
mod characterization_tests;
|
||||
mod deserialization;
|
||||
@@ -139,6 +142,50 @@ impl NodeNetworkInterface {
|
||||
self.migrate_metadata(NodeNetworkMetadata::move_interface_metadata_into_networks);
|
||||
}
|
||||
|
||||
/// Gives each entry under a catalog ID that differs from the shipped network a fresh ID.
|
||||
pub fn give_diverged_catalog_entries_new_ids(&mut self) {
|
||||
loop {
|
||||
let metadata_definitions = &self.network_metadata.persistent_metadata.definitions;
|
||||
let diverged: HashMap<NetworkId, NetworkId> = self
|
||||
.document_network()
|
||||
.definitions
|
||||
.iter()
|
||||
.filter(|(id, network)| {
|
||||
let Some(shipped) = catalog::entry(**id) else { return false };
|
||||
let metadata = metadata_definitions.get(id).map(|metadata| &metadata.persistent_metadata);
|
||||
shipped.network != **network || !metadata.is_some_and(|metadata| same_content(&shipped.metadata.persistent_metadata, metadata))
|
||||
})
|
||||
.map(|(id, _)| (*id, NetworkId::new()))
|
||||
.collect();
|
||||
if diverged.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.migrate_graph(|network| {
|
||||
for (old, new) in &diverged {
|
||||
if let Some(definition) = network.definitions.remove(old) {
|
||||
network.definitions.insert(*new, definition);
|
||||
}
|
||||
}
|
||||
let networks = std::iter::once(&mut network.nodes).chain(network.definitions.values_mut().map(|definition| &mut definition.nodes));
|
||||
for node in networks.flat_map(|nodes| nodes.values_mut()) {
|
||||
if let DocumentNodeImplementation::Definition(id) = &mut node.implementation
|
||||
&& let Some(new) = diverged.get(id)
|
||||
{
|
||||
*id = *new;
|
||||
}
|
||||
}
|
||||
});
|
||||
self.migrate_metadata(|metadata, _| {
|
||||
for (old, new) in &diverged {
|
||||
if let Some(definition) = metadata.persistent_metadata.definitions.remove(old) {
|
||||
metadata.persistent_metadata.definitions.insert(*new, definition);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Eventually remove this document upgrade code
|
||||
/// Moves every inline network into the definitions table, for a document that predates the table.
|
||||
pub fn lift_nested_networks(&mut self) {
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
//! The catalog's networks, shared under fixed IDs and forked before any write.
|
||||
|
||||
use super::*;
|
||||
use crate::messages::portfolio::document::node_graph::document_node_definitions::{network_node_identifiers, resolve_network_node_type};
|
||||
|
||||
thread_local! {
|
||||
// Per thread because the metadata holds caches that are not `Sync`
|
||||
static CATALOG: Catalog = Catalog::build();
|
||||
}
|
||||
|
||||
struct Catalog {
|
||||
entries: HashMap<NetworkId, CopiedDefinition>,
|
||||
/// Each catalog network as its template carries it, to recognize an unmodified one on insertion.
|
||||
inline: HashMap<&'static str, (NodeNetwork, NodeNetworkPersistentMetadata)>,
|
||||
}
|
||||
|
||||
impl Catalog {
|
||||
fn build() -> Self {
|
||||
let mut inline = HashMap::new();
|
||||
for identifier in network_node_identifiers() {
|
||||
let Some(definition) = resolve_network_node_type(identifier) else { continue };
|
||||
let (node, metadata) = definition.default_node_template().into_parts();
|
||||
if let (DocumentNodeImplementation::Network(network), Some(network_metadata)) = (node.implementation, metadata.network_metadata) {
|
||||
inline.insert(identifier, (network, network_metadata.persistent_metadata));
|
||||
}
|
||||
}
|
||||
|
||||
let mut catalog = Self { entries: HashMap::new(), inline };
|
||||
let identifiers: Vec<&'static str> = catalog.inline.keys().copied().collect();
|
||||
for identifier in identifiers {
|
||||
let (network, persistent_metadata) = catalog.inline[identifier].clone();
|
||||
let metadata = NodeNetworkMetadata {
|
||||
persistent_metadata,
|
||||
..Default::default()
|
||||
};
|
||||
catalog.lift(network_id(identifier.as_bytes()), network, metadata);
|
||||
}
|
||||
catalog
|
||||
}
|
||||
|
||||
/// Nested inline networks get IDs derived from `id` and the node holding them.
|
||||
fn lift(&mut self, id: NetworkId, mut network: NodeNetwork, mut metadata: NodeNetworkMetadata) {
|
||||
for (node_id, node) in &mut network.nodes {
|
||||
if !matches!(node.implementation, DocumentNodeImplementation::Network(_)) {
|
||||
continue;
|
||||
}
|
||||
let DocumentNodeImplementation::Network(nested) = std::mem::replace(&mut node.implementation, DocumentNodeImplementation::Extract) else {
|
||||
continue;
|
||||
};
|
||||
let node_metadata = metadata.persistent_metadata.node_metadata.entry(*node_id).or_default();
|
||||
let nested_metadata = node_metadata.persistent_metadata.network_metadata.take().unwrap_or_default();
|
||||
|
||||
let nested_id = match self.catalog_id_of(&nested, &nested_metadata.persistent_metadata) {
|
||||
Some(catalog_id) => catalog_id,
|
||||
None => {
|
||||
let nested_id = network_id(&[id.0.to_le_bytes(), node_id.0.to_le_bytes()].concat());
|
||||
self.lift(nested_id, nested, nested_metadata);
|
||||
nested_id
|
||||
}
|
||||
};
|
||||
node.implementation = DocumentNodeImplementation::Definition(nested_id);
|
||||
}
|
||||
|
||||
self.entries.insert(id, CopiedDefinition { id, network, metadata });
|
||||
}
|
||||
|
||||
fn catalog_id_of(&self, network: &NodeNetwork, metadata: &NodeNetworkPersistentMetadata) -> Option<NetworkId> {
|
||||
let identifier = metadata.reference.as_deref()?;
|
||||
let (shipped_network, shipped_metadata) = self.inline.get(identifier)?;
|
||||
(shipped_network == network && same_content(shipped_metadata, metadata)).then(|| network_id(identifier.as_bytes()))
|
||||
}
|
||||
}
|
||||
|
||||
/// The fixed ID of an unmodified catalog network.
|
||||
pub(super) fn catalog_id_of(network: &NodeNetwork, metadata: &NodeNetworkPersistentMetadata) -> Option<NetworkId> {
|
||||
CATALOG.with(|catalog| catalog.catalog_id_of(network, metadata))
|
||||
}
|
||||
|
||||
pub(super) fn entry(id: NetworkId) -> Option<CopiedDefinition> {
|
||||
CATALOG.with(|catalog| catalog.entries.get(&id).cloned())
|
||||
}
|
||||
|
||||
/// Whether `id` is a catalog entry, which writes fork first.
|
||||
pub(crate) fn is_protected(id: NetworkId) -> bool {
|
||||
CATALOG.with(|catalog| catalog.entries.contains_key(&id))
|
||||
}
|
||||
|
||||
/// Truncated blake3 under a domain tag, like graph-storage's derived IDs.
|
||||
fn network_id(key: &[u8]) -> NetworkId {
|
||||
let mut hasher = blake3::Hasher::new();
|
||||
hasher.update(b"catalog network");
|
||||
hasher.update(key);
|
||||
let mut truncated = [0_u8; 8];
|
||||
truncated.copy_from_slice(&hasher.finalize().as_bytes()[..8]);
|
||||
NetworkId(u64::from_le_bytes(truncated))
|
||||
}
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
use super::template::NodeTemplateImplementation;
|
||||
use super::{InputConnector, OutputConnector, catalog};
|
||||
use crate::messages::portfolio::document::node_graph::document_node_definitions::{network_node_identifiers, resolve_network_node_type};
|
||||
use crate::test_utils::test_prelude::*;
|
||||
use graph_craft::document::{DocumentNodeImplementation, NetworkId, NodeInput};
|
||||
use graphene_std::uuid::NodeId;
|
||||
|
||||
async fn editor_with_blends(count: usize) -> (EditorTestUtils, Vec<NodeId>) {
|
||||
let mut editor = EditorTestUtils::create();
|
||||
editor.new_document().await;
|
||||
|
||||
let template = resolve_network_node_type("Blend").expect("the catalog should hold Blend").default_node_template();
|
||||
let blends: Vec<NodeId> = (0..count).map(|_| NodeId::new()).collect();
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
for blend in &blends {
|
||||
interface.insert_node(*blend, template.clone(), &[]);
|
||||
}
|
||||
(editor, blends)
|
||||
}
|
||||
|
||||
fn entry_of(editor: &EditorTestUtils, node_id: NodeId) -> NetworkId {
|
||||
let DocumentNodeImplementation::Definition(id) = editor.active_document().network_interface.document_network().nodes[&node_id].implementation else {
|
||||
panic!("the node should name a table entry")
|
||||
};
|
||||
id
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn catalog_nodes_share_the_shipped_entry() {
|
||||
let (editor, blends) = editor_with_blends(2).await;
|
||||
|
||||
let shared = entry_of(&editor, blends[0]);
|
||||
assert_eq!(entry_of(&editor, blends[1]), shared);
|
||||
assert!(catalog::is_protected(shared));
|
||||
assert_eq!(
|
||||
editor.active_document().network_interface.document_network().definitions[&shared],
|
||||
catalog::entry(shared).expect("shipped").network
|
||||
);
|
||||
assert!(editor.active_document().network_interface.validate_invariants().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn every_catalog_template_inserts_as_its_shipped_entry() {
|
||||
let mut editor = EditorTestUtils::create();
|
||||
editor.new_document().await;
|
||||
|
||||
let unrecognized: Vec<_> = network_node_identifiers()
|
||||
.filter(|identifier| {
|
||||
let node_id = NodeId::new();
|
||||
let template = resolve_network_node_type(identifier).expect("listed identifiers resolve").default_node_template();
|
||||
if !matches!(template.implementation, NodeTemplateImplementation::Network(_)) {
|
||||
return false;
|
||||
}
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
interface.insert_node(node_id, template, &[]);
|
||||
!matches!(interface.document_network().nodes[&node_id].implementation, DocumentNodeImplementation::Definition(id) if catalog::is_protected(id))
|
||||
})
|
||||
.collect();
|
||||
assert!(unrecognized.is_empty(), "these should insert as their catalog entries: {unrecognized:?}");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn editing_the_signature_of_one_catalog_node_leaves_its_siblings_wired() {
|
||||
let (mut editor, blends) = editor_with_blends(3).await;
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
let sibling_input = InputConnector::node_at_index(blends[1], 1);
|
||||
interface.create_wire(&OutputConnector::node(blends[2], 0), &sibling_input, &[]);
|
||||
let sibling_input_count = interface.document_network().nodes[&blends[1]].inputs.len();
|
||||
|
||||
interface.remove_import(1, &[blends[0]]);
|
||||
|
||||
let interface = &editor.active_document().network_interface;
|
||||
assert!(matches!(interface.input_from_connector(&sibling_input, &[]), Some(NodeInput::Node { .. })));
|
||||
assert_eq!(interface.document_network().nodes[&blends[1]].inputs.len(), sibling_input_count);
|
||||
assert_eq!(interface.document_network().nodes[&blends[0]].inputs.len(), sibling_input_count - 1);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn navigating_inside_a_catalog_node_does_not_fork_it() {
|
||||
let (mut editor, blends) = editor_with_blends(1).await;
|
||||
let shipped = entry_of(&editor, blends[0]);
|
||||
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
interface.set_transform(glam::DAffine2::from_translation((3., 5.).into()), &[blends[0]]);
|
||||
interface.set_node_graph_width(400., &[blends[0]]);
|
||||
assert_eq!(entry_of(&editor, blends[0]), shipped);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_edit_inside_a_catalog_node_forks_only_that_node() {
|
||||
let (mut editor, blends) = editor_with_blends(2).await;
|
||||
let shipped = entry_of(&editor, blends[0]);
|
||||
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
let (inner, input_index) = interface.document_network().definitions[&shipped]
|
||||
.nodes
|
||||
.iter()
|
||||
.find_map(|(id, node)| node.inputs.iter().position(|input| matches!(input, NodeInput::Value { .. })).map(|index| (*id, index)))
|
||||
.expect("Blend should have a value input inside");
|
||||
let NodeInput::Value { tagged_value, exposed } = interface.document_network().definitions[&shipped].nodes[&inner].inputs[input_index].clone() else {
|
||||
unreachable!("the input was found as a value")
|
||||
};
|
||||
interface.set_input(&InputConnector::node_at_index(inner, input_index), NodeInput::value((*tagged_value).clone(), !exposed), &[blends[0]]);
|
||||
|
||||
let forked = entry_of(&editor, blends[0]);
|
||||
assert_ne!(forked, shipped);
|
||||
assert!(!catalog::is_protected(forked));
|
||||
assert_eq!(entry_of(&editor, blends[1]), shipped);
|
||||
assert_eq!(
|
||||
editor.active_document().network_interface.document_network().definitions[&shipped],
|
||||
catalog::entry(shipped).expect("shipped").network
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn moving_a_catalog_node_does_not_fork_it() {
|
||||
let (mut editor, blends) = editor_with_blends(1).await;
|
||||
let shipped = entry_of(&editor, blends[0]);
|
||||
|
||||
editor.active_document_mut().network_interface.shift_node(&blends[0], glam::IVec2::new(3, 5), &[]);
|
||||
assert_eq!(entry_of(&editor, blends[0]), shipped);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn repairs_on_open_do_not_fork_catalog_nodes() {
|
||||
let (mut editor, blends) = editor_with_blends(1).await;
|
||||
let shipped = entry_of(&editor, blends[0]);
|
||||
|
||||
editor.active_document_mut().finalize_storage_load();
|
||||
assert_eq!(entry_of(&editor, blends[0]), shipped);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_entry_that_diverged_under_a_catalog_id_gets_a_fresh_id_on_open() {
|
||||
let (mut editor, blends) = editor_with_blends(1).await;
|
||||
let shipped = entry_of(&editor, blends[0]);
|
||||
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
interface.migrate_graph(|network| {
|
||||
let definition = network.definitions.get_mut(&shipped).expect("the shipped entry");
|
||||
definition.exports.push(NodeInput::value(graph_craft::document::value::TaggedValue::None, false));
|
||||
});
|
||||
interface.give_diverged_catalog_entries_new_ids();
|
||||
|
||||
let renamed = entry_of(&editor, blends[0]);
|
||||
assert_ne!(renamed, shipped);
|
||||
let network = editor.active_document().network_interface.document_network();
|
||||
assert!(!network.definitions.contains_key(&shipped));
|
||||
assert!(network.definitions[&renamed].exports.len() > catalog::entry(shipped).expect("shipped").network.exports.len());
|
||||
}
|
||||
+28
-17
@@ -28,6 +28,13 @@ fn convert(editor: &EditorTestUtils) -> Registry {
|
||||
.registry
|
||||
}
|
||||
|
||||
/// The deltas a commit would stage, after settling the definitions table as a commit does.
|
||||
fn committed_deltas(editor: &mut EditorTestUtils) -> Vec<EditorDelta> {
|
||||
let interface = &mut editor.active_document_mut().network_interface;
|
||||
interface.settle_definitions();
|
||||
interface.take_deltas()
|
||||
}
|
||||
|
||||
fn construct(editor: &EditorTestUtils, deltas: &[EditorDelta], working: &Registry) -> Vec<RegistryDelta> {
|
||||
let document = editor.active_document();
|
||||
construct_batch(deltas, working, &document.resources.registry, &StorageMetadataView::new(&document.network_interface), PEER)
|
||||
@@ -247,7 +254,7 @@ async fn emitted_deltas_reproduce_the_diff() {
|
||||
network_interface.shift_node(&node, glam::IVec2::new(3, 5), &[]);
|
||||
}
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
assert!(!emitted.is_empty(), "the edits should have emitted deltas");
|
||||
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
@@ -273,7 +280,7 @@ async fn unchanged_writes_emit_nothing() {
|
||||
network_interface.shift_node(&node, glam::IVec2::ZERO, &[]);
|
||||
}
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
assert!(emitted.is_empty(), "re-writing the same values should emit nothing, got {emitted:#?}");
|
||||
}
|
||||
|
||||
@@ -293,7 +300,7 @@ async fn emitted_deltas_reproduce_the_diff_for_structural_edits() {
|
||||
let node_id = NodeId(0xDE17A);
|
||||
editor.active_document_mut().network_interface.insert_node(node_id, template, &[]);
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(&editor));
|
||||
assert_same_stored_effect(&working, constructed, diffed, "emitted node insertion");
|
||||
@@ -302,7 +309,7 @@ async fn emitted_deltas_reproduce_the_diff_for_structural_edits() {
|
||||
editor.active_document_mut().network_interface.discard_deltas();
|
||||
editor.active_document_mut().network_interface.delete_nodes(vec![node_id], true, &[]);
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(&editor));
|
||||
assert_same_stored_effect(&working, constructed, diffed, "emitted node deletion");
|
||||
@@ -339,7 +346,7 @@ async fn emitted_deltas_reproduce_the_diff_for_an_arity_change() {
|
||||
.network_interface
|
||||
.add_import(TaggedValue::Number(7.), true, -1, "Added", "An added import", &[group]);
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
assert!(!emitted.is_empty(), "adding an import should have emitted deltas");
|
||||
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
@@ -380,7 +387,7 @@ async fn an_arity_change_keeps_an_earlier_edit_to_the_same_node() {
|
||||
.network_interface
|
||||
.add_import(TaggedValue::Number(7.), true, -1, "Added", "An added import", &[group]);
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(&editor));
|
||||
assert_same_stored_effect(&working, constructed, diffed, "rename then arity change");
|
||||
@@ -508,7 +515,7 @@ async fn a_node_added_and_removed_in_one_batch_leaves_nothing_behind() {
|
||||
editor.active_document_mut().network_interface.insert_node(node_id, template, &[]);
|
||||
editor.active_document_mut().network_interface.delete_nodes(vec![node_id], true, &[]);
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(&editor));
|
||||
assert_same_stored_effect(&working, constructed, diffed, "add then remove in one batch");
|
||||
@@ -711,7 +718,7 @@ async fn output_descriptions_follow_their_exports() {
|
||||
.collect::<Vec<_>>();
|
||||
assert_eq!(descriptions, ["Third.", "Second."], "the descriptions should have moved with their exports");
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(&editor));
|
||||
assert_same_stored_effect(&working, constructed, diffed, "moved output descriptions");
|
||||
@@ -737,7 +744,7 @@ async fn exports_of_a_subgraph_without_descriptions_emit_no_description_write()
|
||||
network_interface.remove_export(last_export, &[merge]);
|
||||
}
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let description_writes = emitted
|
||||
.iter()
|
||||
.filter(|delta| {
|
||||
@@ -765,7 +772,7 @@ async fn adding_an_export_past_the_described_ones_matches_the_diff() {
|
||||
|
||||
editor.active_document_mut().network_interface.add_export(TaggedValue::None, -1, "", &[merge]);
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(&editor));
|
||||
assert_same_stored_effect(&working, constructed, diffed, "an export added past the described ones");
|
||||
@@ -792,7 +799,7 @@ async fn exports_past_the_end_of_the_description_list_emit_no_description_write(
|
||||
network_interface.remove_export(last_export, &[merge]);
|
||||
}
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
let description_writes = emitted
|
||||
.iter()
|
||||
.filter(|delta| {
|
||||
@@ -885,7 +892,7 @@ async fn an_edit_inside_a_table_entry_constructs_the_diff() {
|
||||
.network_interface
|
||||
.set_input(&InputConnector::node_at_index(inner, input_index), NodeInput::value(TaggedValue::None, false), &[layer]);
|
||||
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(&mut editor);
|
||||
|
||||
let constructed = construct(&editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(&editor));
|
||||
@@ -905,7 +912,7 @@ async fn a_template_of_a_table_node_names_its_entry() {
|
||||
assert_eq!(node.implementation, graph_craft::document::DocumentNodeImplementation::Definition(definition));
|
||||
}
|
||||
|
||||
/// A template of `node_id` that carries its table entry inline, as one from before networks moved into the table does.
|
||||
/// An inline copy of `node_id` without its catalog reference, so inserting it lifts a new entry.
|
||||
fn inline_copy(interface: &super::NodeNetworkInterface, node_id: NodeId) -> super::NodeTemplate {
|
||||
let mut node = interface.document_network().nodes[&node_id].clone();
|
||||
let mut metadata = interface.network_metadata.persistent_metadata.node_metadata[&node_id].persistent_metadata.clone();
|
||||
@@ -913,8 +920,10 @@ fn inline_copy(interface: &super::NodeNetworkInterface, node_id: NodeId) -> supe
|
||||
panic!("the node should name a table entry")
|
||||
};
|
||||
|
||||
let mut network_metadata = interface.network_metadata.persistent_metadata.definitions[&id].clone();
|
||||
network_metadata.persistent_metadata.reference = None;
|
||||
node.implementation = graph_craft::document::DocumentNodeImplementation::Network(interface.document_network().definitions[&id].clone());
|
||||
metadata.network_metadata = Some(interface.network_metadata.persistent_metadata.definitions[&id].clone());
|
||||
metadata.network_metadata = Some(network_metadata);
|
||||
super::NodeTemplate::from_parts(node, metadata)
|
||||
}
|
||||
|
||||
@@ -931,11 +940,14 @@ async fn a_write_keeps_its_network_when_the_instance_changes_implementation() {
|
||||
interface.discard_deltas();
|
||||
|
||||
interface.set_input(&InputConnector::node_at_index(inner, input_index), NodeInput::value(TaggedValue::None, false), &[layer]);
|
||||
let graph_craft::document::DocumentNodeImplementation::Definition(written) = interface.document_network().nodes[&layer].implementation else {
|
||||
panic!("the layer should name a table entry")
|
||||
};
|
||||
let mut inline_copy = inline_copy(interface, layer);
|
||||
interface.replace_implementation(&layer, &[], &mut inline_copy);
|
||||
|
||||
let deltas = interface.take_deltas();
|
||||
assert_eq!(recorded_input_write(&deltas, inner), OwnedNetworkLocation::definition(definition));
|
||||
assert_eq!(recorded_input_write(&deltas, inner), OwnedNetworkLocation::definition(written));
|
||||
}
|
||||
|
||||
/// A copy of `layer` that names the same table entry, so the two share one network.
|
||||
@@ -957,8 +969,7 @@ fn assert_stages_incrementally(editor: &mut EditorTestUtils, at: &str, edit: imp
|
||||
editor.active_document_mut().network_interface.discard_deltas();
|
||||
|
||||
edit(editor);
|
||||
editor.active_document_mut().network_interface.settle_definitions();
|
||||
let emitted = editor.active_document_mut().network_interface.take_deltas();
|
||||
let emitted = committed_deltas(editor);
|
||||
|
||||
let constructed = construct(editor, &emitted, &working);
|
||||
let diffed = compute_deltas(&working, &convert(editor));
|
||||
|
||||
+20
-10
@@ -33,7 +33,7 @@ impl NodeNetworkInterface {
|
||||
}
|
||||
|
||||
pub fn set_transform(&mut self, transform: DAffine2, network_path: &[NodeId]) {
|
||||
let Some(mut network) = self.network_mut(network_path) else {
|
||||
let Some(mut network) = self.network_view_mut(network_path) else {
|
||||
log::error!("Could not get nested network in set_transform");
|
||||
return;
|
||||
};
|
||||
@@ -44,7 +44,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
// This should be run whenever the pan ends, a zoom occurs, or the network is opened
|
||||
pub fn set_node_graph_width(&mut self, node_graph_width: f64, network_path: &[NodeId]) {
|
||||
let Some(mut network) = self.network_mut(network_path) else {
|
||||
let Some(mut network) = self.network_view_mut(network_path) else {
|
||||
log::error!("Could not get nested network in set_node_graph_width");
|
||||
return;
|
||||
};
|
||||
@@ -72,6 +72,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Inserts a new export at insert index. If the insert index is -1 it is inserted at the end. The output_name is used by the encapsulating node.
|
||||
pub fn add_export(&mut self, default_value: TaggedValue, insert_index: isize, output_name: &str, network_path: &[NodeId]) {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let inserted_index = if insert_index == -1 { self.number_of_exports(network_path) } else { insert_index as usize };
|
||||
if !self.insert_export_slot(network_path, inserted_index, NodeInput::value(default_value, true), output_name.to_string(), String::new()) {
|
||||
return;
|
||||
@@ -109,6 +110,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Inserts a new input at insert index. If the insert index is -1 it is inserted at the end. The input_name is used by the encapsulating node.
|
||||
pub fn add_import(&mut self, default_value: TaggedValue, exposed: bool, insert_index: isize, input_name: &str, input_description: &str, network_path: &[NodeId]) {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let encapsulating_nodes = self.encapsulating_nodes(network_path);
|
||||
let Some((first_path, first_node_id)) = encapsulating_nodes.first() else {
|
||||
log::error!("Cannot add import for document network");
|
||||
@@ -191,6 +193,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
// First disconnects the export, then removes it
|
||||
pub fn remove_export(&mut self, export_index: usize, network_path: &[NodeId]) {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let encapsulating_nodes = self.encapsulating_nodes(network_path);
|
||||
if encapsulating_nodes.is_empty() {
|
||||
log::error!("Cannot remove export for document network");
|
||||
@@ -235,6 +238,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
// First disconnects the import, then removes it
|
||||
pub fn remove_import(&mut self, import_index: usize, network_path: &[NodeId]) {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let encapsulating_nodes = self.encapsulating_nodes(network_path);
|
||||
let Some((first_path, first_parent_id)) = encapsulating_nodes.first() else {
|
||||
log::error!("Cannot remove import for document network");
|
||||
@@ -286,6 +290,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// The end index is before the export is removed, so moving to the end is the length of the current exports
|
||||
pub fn reorder_export(&mut self, start_index: usize, mut end_index: usize, network_path: &[NodeId]) {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let encapsulating_nodes = self.encapsulating_nodes(network_path);
|
||||
if encapsulating_nodes.is_empty() {
|
||||
log::error!("Could not reorder export for document network");
|
||||
@@ -318,6 +323,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// The end index is before the import is removed, so moving to the end is the length of the current imports
|
||||
pub fn reorder_import(&mut self, start_index: usize, mut end_index: usize, network_path: &[NodeId]) {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let encapsulating_nodes = self.encapsulating_nodes(network_path);
|
||||
if encapsulating_nodes.is_empty() {
|
||||
log::error!("Could not reorder import for document network");
|
||||
@@ -438,7 +444,7 @@ impl NodeNetworkInterface {
|
||||
self.set_upstream_chain_to_absolute(&upstream_node_id, network_path);
|
||||
}
|
||||
|
||||
let Some(mut node) = self.node_mut(NodeLocator::new(*node_id, network_path)) else {
|
||||
let Some(mut node) = self.node_interface_mut(NodeLocator::new(*node_id, network_path)) else {
|
||||
log::error!("Could not get node {node_id} in replace_inputs");
|
||||
return None;
|
||||
};
|
||||
@@ -453,7 +459,9 @@ impl NodeNetworkInterface {
|
||||
let number_of_inputs = node.inputs.len();
|
||||
let definition = self.reference(node_id, network_path).as_ref().and_then(resolve_document_node_type);
|
||||
|
||||
let Some(mut node_entry) = self.node_mut(NodeLocator::new(*node_id, network_path)) else { return };
|
||||
let Some(mut node_entry) = self.node_mut_unforked(NodeLocator::new(*node_id, network_path)) else {
|
||||
return;
|
||||
};
|
||||
node_entry.pad_input_metadata(number_of_inputs, |input_index| {
|
||||
definition.and_then(|definition| definition.node_template.input_metadata.get(input_index).cloned())
|
||||
});
|
||||
@@ -466,7 +474,7 @@ impl NodeNetworkInterface {
|
||||
};
|
||||
let number_of_exports = network.exports.len();
|
||||
|
||||
let Some(mut node) = self.node_mut(NodeLocator::new(*node_id, network_path)) else {
|
||||
let Some(mut node) = self.node_mut_unforked(NodeLocator::new(*node_id, network_path)) else {
|
||||
log::error!("Could not get node {node_id} in validate_output_names");
|
||||
return;
|
||||
};
|
||||
@@ -508,7 +516,9 @@ impl NodeNetworkInterface {
|
||||
.filter(|template| template.output_names.len() == self.number_of_outputs(node_id, network_path))
|
||||
.map(|template| template.output_descriptions.clone());
|
||||
|
||||
let Some(mut node_entry) = self.node_mut(NodeLocator::new(*node_id, network_path)) else { return };
|
||||
let Some(mut node_entry) = self.node_mut_unforked(NodeLocator::new(*node_id, network_path)) else {
|
||||
return;
|
||||
};
|
||||
for (index, description) in input_descriptions {
|
||||
node_entry.set_input_description(index, description);
|
||||
}
|
||||
@@ -1189,7 +1199,7 @@ impl NodeNetworkInterface {
|
||||
/// Replaces the full list of output port names for a node. Used by document migrations that turn a single-output node
|
||||
/// into a multi-output one, since the port labels are otherwise unnamed and fall back to the type name.
|
||||
pub fn set_output_names(&mut self, node_id: &NodeId, output_names: Vec<String>, network_path: &[NodeId]) {
|
||||
let Some(mut node) = self.node_mut(NodeLocator::new(*node_id, network_path)) else {
|
||||
let Some(mut node) = self.node_interface_mut(NodeLocator::new(*node_id, network_path)) else {
|
||||
log::error!("Could not get node {node_id} in set_output_names");
|
||||
return;
|
||||
};
|
||||
@@ -1202,7 +1212,7 @@ impl NodeNetworkInterface {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(mut node) = self.node_mut(NodeLocator::new(*encapsulating_node_id, encapsulating_network_path)) else {
|
||||
let Some(mut node) = self.node_interface_mut(NodeLocator::new(*encapsulating_node_id, encapsulating_network_path)) else {
|
||||
log::error!("Could not get encapsulating node in set_import_export_name");
|
||||
return;
|
||||
};
|
||||
@@ -1223,7 +1233,7 @@ impl NodeNetworkInterface {
|
||||
return;
|
||||
};
|
||||
|
||||
let Some(mut node) = self.node_mut(NodeLocator::new(*encapsulating_node_id, encapsulating_network_path)) else {
|
||||
let Some(mut node) = self.node_interface_mut(NodeLocator::new(*encapsulating_node_id, encapsulating_network_path)) else {
|
||||
log::error!("Could not get encapsulating node in set_import_export_description");
|
||||
return;
|
||||
};
|
||||
@@ -1408,7 +1418,7 @@ impl NodeNetworkInterface {
|
||||
}
|
||||
|
||||
fn set_previewing(&mut self, previewing: Previewing, network_path: &[NodeId]) {
|
||||
if let Some(mut network) = self.network_mut(network_path) {
|
||||
if let Some(mut network) = self.network_view_mut(network_path) {
|
||||
network.set_previewing(previewing);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -852,7 +852,7 @@ impl NodeNetworkInterface {
|
||||
}
|
||||
|
||||
pub fn set_input_override(&mut self, node_id: &NodeId, index: usize, widget_override: Option<String>, network_path: &[NodeId]) {
|
||||
let Some(mut node) = self.node_mut(NodeLocator::new(*node_id, network_path)) else {
|
||||
let Some(mut node) = self.node_interface_mut(NodeLocator::new(*node_id, network_path)) else {
|
||||
log::error!("Could not get node {node_id} in set_input_override");
|
||||
return;
|
||||
};
|
||||
|
||||
+21
-1
@@ -63,6 +63,12 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// The persistent metadata of a network, or `None` if the network is missing.
|
||||
pub(crate) fn network_mut<'a>(&'a mut self, network_path: &'a [NodeId]) -> Option<NetworkMut<'a>> {
|
||||
let location = self.locate_for_write(network_path)?;
|
||||
self.network_mut_at(location)
|
||||
}
|
||||
|
||||
/// Like [`Self::network_mut`] without forking, for the view state that catalog instances share.
|
||||
pub(crate) fn network_view_mut<'a>(&'a mut self, network_path: &'a [NodeId]) -> Option<NetworkMut<'a>> {
|
||||
let location = self.locate(network_path)?;
|
||||
self.network_mut_at(location)
|
||||
}
|
||||
@@ -73,8 +79,22 @@ impl NodeNetworkInterface {
|
||||
Some(NetworkMut::new(metadata, &mut self.deltas, location))
|
||||
}
|
||||
|
||||
/// Both halves of a node, or `None` if either is missing.
|
||||
/// Both halves of a node, or `None` if either is missing, after forking the catalog entries along the path.
|
||||
pub(crate) fn node_mut<'a>(&'a mut self, locator: NodeLocator<'a>) -> Option<NodeMut<'a>> {
|
||||
self.document_node(&locator.node_id, locator.network_path)?;
|
||||
let location = self.locate_for_write(locator.network_path)?;
|
||||
self.node_mut_at(location, locator.node_id)
|
||||
}
|
||||
|
||||
/// Like [`Self::node_mut`], but also forks the catalog entry the node names, which holds its interface.
|
||||
pub(crate) fn node_interface_mut<'a>(&'a mut self, locator: NodeLocator<'a>) -> Option<NodeMut<'a>> {
|
||||
self.document_node(&locator.node_id, locator.network_path)?;
|
||||
self.fork_catalog_entries_along(&[locator.network_path, &[locator.node_id]].concat());
|
||||
self.node_mut(locator)
|
||||
}
|
||||
|
||||
/// Like [`Self::node_mut`] without forking, for load-time repairs only.
|
||||
pub(crate) fn node_mut_unforked<'a>(&'a mut self, locator: NodeLocator<'a>) -> Option<NodeMut<'a>> {
|
||||
let location = self.locate(locator.network_path)?;
|
||||
self.node_mut_at(location, locator.node_id)
|
||||
}
|
||||
|
||||
+61
-28
@@ -55,6 +55,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Inserts an import at `index` into the network at `network_path`: its metadata once, and `input` on every node the network implements.
|
||||
pub(crate) fn insert_import_slot(&mut self, network_path: &[NodeId], index: usize, input: NodeInput, metadata: InputMetadata) -> bool {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let encapsulating_nodes = self.encapsulating_nodes(network_path);
|
||||
if encapsulating_nodes.is_empty() {
|
||||
log::error!("Cannot insert an import into the document network");
|
||||
@@ -78,6 +79,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Removes the import at `index` from the network at `network_path` and from every node the network implements, returning its metadata.
|
||||
pub(crate) fn remove_import_slot(&mut self, network_path: &[NodeId], index: usize) -> Option<InputMetadata> {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
for (path, node_id) in self.encapsulating_nodes(network_path) {
|
||||
let Some(mut node) = self.node_mut(NodeLocator::new(node_id, &path)) else {
|
||||
log::error!("Could not get node {node_id} in remove_import_slot");
|
||||
@@ -97,6 +99,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Moves the import at `from` to `to`, carrying its metadata and each node's input with it.
|
||||
pub(crate) fn move_import_slot(&mut self, network_path: &[NodeId], from: usize, to: usize) -> bool {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
let encapsulating_nodes = self.encapsulating_nodes(network_path);
|
||||
if encapsulating_nodes.is_empty() {
|
||||
return false;
|
||||
@@ -123,7 +126,7 @@ impl NodeNetworkInterface {
|
||||
/// Inserts an export at `index`, clamped to the end, along with the name and description the encapsulating
|
||||
/// node shows for it. The document network has no encapsulating node, so it carries neither.
|
||||
pub(crate) fn insert_export_slot(&mut self, network_path: &[NodeId], index: usize, export: NodeInput, output_name: String, output_description: String) -> bool {
|
||||
let Some(location) = self.locate(network_path) else {
|
||||
let Some(location) = self.locate_for_write(network_path) else {
|
||||
log::error!("Could not locate network {network_path:?} in insert_export_slot");
|
||||
return false;
|
||||
};
|
||||
@@ -160,7 +163,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Removes the export at `index` together with the encapsulating node's name and description for it.
|
||||
pub(crate) fn remove_export_slot(&mut self, network_path: &[NodeId], index: usize) -> Option<NodeInput> {
|
||||
let Some(location) = self.locate(network_path) else {
|
||||
let Some(location) = self.locate_for_write(network_path) else {
|
||||
log::error!("Could not locate network {network_path:?} in remove_export_slot");
|
||||
return None;
|
||||
};
|
||||
@@ -253,7 +256,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Writes the input at `connector`, returning the input it replaced.
|
||||
pub(crate) fn set_input_slot(&mut self, connector: &InputConnector, network_path: &[NodeId], input: NodeInput) -> Option<NodeInput> {
|
||||
let Some(location) = self.locate(network_path) else {
|
||||
let Some(location) = self.locate_for_write(network_path) else {
|
||||
log::error!("Could not locate network {network_path:?} in set_input_slot");
|
||||
return None;
|
||||
};
|
||||
@@ -287,7 +290,7 @@ impl NodeNetworkInterface {
|
||||
/// which avoids copying the value out and back. The recorded delta still carries the whole result,
|
||||
/// since the storage op is a whole-value write.
|
||||
pub(crate) fn edit_input_value(&mut self, connector: &InputConnector, network_path: &[NodeId], edit: impl FnOnce(&mut TaggedValue) -> bool) -> bool {
|
||||
let Some(location) = self.locate(network_path) else {
|
||||
let Some(location) = self.locate_for_write(network_path) else {
|
||||
log::error!("Could not locate network {network_path:?} in edit_input_value");
|
||||
return false;
|
||||
};
|
||||
@@ -324,7 +327,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Inserts a node and its metadata, returning the entry it replaced.
|
||||
pub(crate) fn insert_node_entry(&mut self, locator: NodeLocator, template: NodeTemplate) -> Option<NodeTemplate> {
|
||||
let Some(location) = self.locate(locator.network_path) else {
|
||||
let Some(location) = self.locate_for_write(locator.network_path) else {
|
||||
log::error!("Could not locate network {:?} in insert_node_entry", locator.network_path);
|
||||
return None;
|
||||
};
|
||||
@@ -341,7 +344,7 @@ impl NodeNetworkInterface {
|
||||
let (mut document_node, mut persistent_metadata) = template.into_parts();
|
||||
self.lift_into_table(&mut document_node.implementation, &mut persistent_metadata.network_metadata);
|
||||
if let DocumentNodeImplementation::Definition(id) = document_node.implementation {
|
||||
self.insert_carried_definition(id);
|
||||
self.insert_missing_definition(id);
|
||||
}
|
||||
let node = Box::new(document_node.clone());
|
||||
let metadata = Box::new(persistent_metadata.clone());
|
||||
@@ -379,7 +382,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Removes a node and its metadata, returning them joined as a template.
|
||||
pub(crate) fn remove_node_entry(&mut self, locator: NodeLocator) -> Option<NodeTemplate> {
|
||||
let Some(location) = self.locate(locator.network_path) else {
|
||||
let Some(location) = self.locate_for_write(locator.network_path) else {
|
||||
log::error!("Could not locate network {:?} in remove_node_entry", locator.network_path);
|
||||
return None;
|
||||
};
|
||||
@@ -408,7 +411,7 @@ impl NodeNetworkInterface {
|
||||
|
||||
/// Swaps in a new implementation for the node, first moving an inline network it brings into the definitions table with the node's interface.
|
||||
pub(crate) fn replace_node_implementation(&mut self, locator: NodeLocator, mut implementation: DocumentNodeImplementation, mut network_metadata: Option<NodeNetworkMetadata>) {
|
||||
let Some(location) = self.locate(locator.network_path) else {
|
||||
let Some(location) = self.locate_for_write(locator.network_path) else {
|
||||
log::error!("Could not locate network {:?} in replace_node_implementation", locator.network_path);
|
||||
return;
|
||||
};
|
||||
@@ -427,7 +430,7 @@ impl NodeNetworkInterface {
|
||||
self.lift_into_table(&mut implementation, &mut network_metadata);
|
||||
}
|
||||
if let DocumentNodeImplementation::Definition(id) = implementation {
|
||||
self.insert_carried_definition(id);
|
||||
self.insert_missing_definition(id);
|
||||
}
|
||||
|
||||
let Some(mut node) = self.node_mut_at(location, locator.node_id) else {
|
||||
@@ -440,9 +443,14 @@ impl NodeNetworkInterface {
|
||||
/// Moves an inline network `implementation` brings, and every inline network nested in it, into the definitions table under fresh IDs.
|
||||
/// Each entry is recorded before the entries and nodes that name it.
|
||||
fn lift_into_table(&mut self, implementation: &mut DocumentNodeImplementation, network_metadata: &mut Option<NodeNetworkMetadata>) {
|
||||
if !matches!(implementation, DocumentNodeImplementation::Network(_)) {
|
||||
let DocumentNodeImplementation::Network(network) = &*implementation else { return };
|
||||
if let Some(id) = network_metadata.as_ref().and_then(|metadata| catalog::catalog_id_of(network, &metadata.persistent_metadata)) {
|
||||
*implementation = DocumentNodeImplementation::Definition(id);
|
||||
*network_metadata = None;
|
||||
self.insert_missing_definition(id);
|
||||
return;
|
||||
}
|
||||
|
||||
let DocumentNodeImplementation::Network(mut network) = std::mem::replace(implementation, DocumentNodeImplementation::Extract) else {
|
||||
return;
|
||||
};
|
||||
@@ -470,7 +478,7 @@ impl NodeNetworkInterface {
|
||||
.collect();
|
||||
named.sort();
|
||||
for named in named {
|
||||
self.insert_carried_definition(named);
|
||||
self.insert_missing_definition(named);
|
||||
}
|
||||
|
||||
let location = OwnedNetworkLocation::definition(id);
|
||||
@@ -540,9 +548,15 @@ impl NodeNetworkInterface {
|
||||
}
|
||||
}
|
||||
|
||||
let held = self.document_network().definitions.get(&id).zip(self.network_metadata.persistent_metadata.definitions.get(&id));
|
||||
// A catalog ID always means the shipped network
|
||||
let shipped = catalog::entry(id).map(|shipped| (shipped.network, shipped.metadata));
|
||||
let held = shipped.or_else(|| {
|
||||
let network = self.document_network().definitions.get(&id)?.clone();
|
||||
let metadata = self.network_metadata.persistent_metadata.definitions.get(&id)?.clone();
|
||||
Some((network, metadata))
|
||||
});
|
||||
let id = match held {
|
||||
Some((held_network, held_metadata)) if *held_network != network || !same_content(&held_metadata.persistent_metadata, &metadata.persistent_metadata) => {
|
||||
Some((held_network, held_metadata)) if held_network != network || !same_content(&held_metadata.persistent_metadata, &metadata.persistent_metadata) => {
|
||||
let new_id = NetworkId::new();
|
||||
renamed.insert(id, new_id);
|
||||
new_id
|
||||
@@ -579,17 +593,48 @@ impl NodeNetworkInterface {
|
||||
true
|
||||
}
|
||||
|
||||
/// Adds the carried table entry `id` if the document does not hold it.
|
||||
fn insert_carried_definition(&mut self, id: NetworkId) {
|
||||
/// Adds `id` from the last paste or the catalog if the document lacks it.
|
||||
fn insert_missing_definition(&mut self, id: NetworkId) {
|
||||
if self.document_network().definitions.contains_key(&id) {
|
||||
return;
|
||||
}
|
||||
let Some(CopiedDefinition { network, metadata, .. }) = self.carried_definitions.get(&id).cloned() else {
|
||||
let Some(CopiedDefinition { network, metadata, .. }) = self.carried_definitions.get(&id).cloned().or_else(|| catalog::entry(id)) else {
|
||||
return;
|
||||
};
|
||||
self.insert_definition(id, network, metadata);
|
||||
}
|
||||
|
||||
/// Locates the network after forking the catalog entries along the path.
|
||||
pub(super) fn locate_for_write<'p>(&mut self, network_path: &'p [NodeId]) -> Option<NetworkLocation<'p>> {
|
||||
self.fork_catalog_entries_along(network_path);
|
||||
self.locate(network_path)
|
||||
}
|
||||
|
||||
/// Forks each catalog instance along the path, outermost first.
|
||||
pub(super) fn fork_catalog_entries_along(&mut self, network_path: &[NodeId]) {
|
||||
while let Some(depth) = self.first_catalog_instance(network_path) {
|
||||
if !self.fork_definition(NodeLocator::new(network_path[depth], &network_path[..depth])) {
|
||||
log::error!("Could not fork the catalog entry at {:?}", &network_path[..=depth]);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn first_catalog_instance(&self, network_path: &[NodeId]) -> Option<usize> {
|
||||
let document_network = self.document_network();
|
||||
let mut network = document_network;
|
||||
for (depth, segment) in network_path.iter().enumerate() {
|
||||
let implementation = &network.nodes.get(segment)?.implementation;
|
||||
if let DocumentNodeImplementation::Definition(id) = implementation
|
||||
&& catalog::is_protected(*id)
|
||||
{
|
||||
return Some(depth);
|
||||
}
|
||||
network = document_network.implementation_network(implementation)?;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Drops the definitions table entry `id` from both trees.
|
||||
pub(crate) fn remove_definition(&mut self, id: NetworkId) {
|
||||
let removed = self.network.get_mut().network_mut().definitions.remove(&id).is_some();
|
||||
@@ -696,18 +741,6 @@ impl NodeNetworkInterface {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether two networks' metadata describe the same content, ignoring this peer's view of them.
|
||||
fn same_content(first: &NodeNetworkPersistentMetadata, second: &NodeNetworkPersistentMetadata) -> bool {
|
||||
let content = |metadata: &NodeNetworkPersistentMetadata| NodeNetworkPersistentMetadata {
|
||||
previewing: Previewing::No,
|
||||
navigation_metadata: NavigationMetadata::default(),
|
||||
selection_undo_history: VecDeque::new(),
|
||||
selection_redo_history: VecDeque::new(),
|
||||
..metadata.clone()
|
||||
};
|
||||
content(first) == content(second)
|
||||
}
|
||||
|
||||
/// Whether any of `descriptions` says anything, since storage keeps the whole list then, but not a list that's all empty.
|
||||
fn has_description(descriptions: &[String]) -> bool {
|
||||
descriptions.iter().any(|description| !description.is_empty())
|
||||
|
||||
@@ -504,6 +504,31 @@ impl<'de> serde::Deserialize<'de> for Previewing {
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether two networks' metadata match, ignoring view state.
|
||||
pub(crate) fn same_content(first: &NodeNetworkPersistentMetadata, second: &NodeNetworkPersistentMetadata) -> bool {
|
||||
let content = |metadata: &NodeNetworkPersistentMetadata| {
|
||||
let mut content = metadata.clone();
|
||||
clear_view_state(&mut content);
|
||||
content
|
||||
};
|
||||
content(first) == content(second)
|
||||
}
|
||||
|
||||
fn clear_view_state(metadata: &mut NodeNetworkPersistentMetadata) {
|
||||
metadata.previewing = Previewing::No;
|
||||
metadata.navigation_metadata = NavigationMetadata::default();
|
||||
metadata.selection_undo_history.clear();
|
||||
metadata.selection_redo_history.clear();
|
||||
|
||||
let nested_networks = metadata
|
||||
.node_metadata
|
||||
.values_mut()
|
||||
.filter_map(|node_metadata| node_metadata.persistent_metadata.network_metadata.as_mut());
|
||||
for nested in nested_networks.chain(metadata.definitions.values_mut()) {
|
||||
clear_view_state(&mut nested.persistent_metadata);
|
||||
}
|
||||
}
|
||||
|
||||
/// A definitions table entry carried by a copy, so a paste into a document that lacks it can add it.
|
||||
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
|
||||
pub struct CopiedDefinition {
|
||||
|
||||
@@ -268,6 +268,7 @@ async fn build_document_from_storage(gdd: GddV1, byte_store: &ResourcesHandle, d
|
||||
}
|
||||
};
|
||||
apply_network_view_settings(&mut interface, &network_ids, gdd.network_view_settings());
|
||||
interface.give_diverged_catalog_entries_new_ids();
|
||||
// TODO: Run document upgrades here once this format has a versioned migration
|
||||
|
||||
let mut document = DocumentMessageHandler::from_storage(interface, gdd, declarations, String::new(), None);
|
||||
@@ -343,6 +344,7 @@ pub(super) fn load_legacy_document(document_serialized_content: String, byte_sto
|
||||
}
|
||||
|
||||
document.network_interface.lift_nested_networks();
|
||||
document.network_interface.give_diverged_catalog_entries_new_ids();
|
||||
document.network_interface.discard_deltas();
|
||||
document.require_whole_document_stage();
|
||||
|
||||
|
||||
Reference in new issue
Block a user