Skip to main content

jellyflow_runtime/runtime/store/view/
document.rs

1use crate::io::{NodeGraphEditorConfig, NodeGraphViewState};
2use crate::runtime::lookups::NodeGraphLookups;
3use jellyflow_core::core::Graph;
4use jellyflow_core::ops::GraphHistory;
5
6use super::super::NodeGraphStore;
7
8impl NodeGraphStore {
9    pub fn graph(&self) -> &Graph {
10        &self.graph
11    }
12
13    pub fn graph_revision(&self) -> u64 {
14        self.graph_revision
15    }
16
17    pub fn layout_facts_revision(&self) -> u64 {
18        self.layout_facts_revision
19    }
20
21    pub fn lookups(&self) -> &NodeGraphLookups {
22        &self.lookups
23    }
24
25    pub(crate) fn lookups_mut(&mut self) -> &mut NodeGraphLookups {
26        &mut self.lookups
27    }
28
29    /// Replaces the entire graph document.
30    ///
31    /// This is a controlled-mode helper: callers that own graph state can swap the document
32    /// without going through transactions (e.g. loading a file, switching tabs).
33    ///
34    /// This emits a document replacement event, not a graph commit. Selection is sanitized against
35    /// the new graph.
36    pub fn replace_graph(&mut self, graph: Graph) {
37        let before = self.capture_document_snapshot();
38
39        self.graph = graph;
40        self.bump_graph_revision();
41        self.view_state.sanitize_for_graph(&self.graph);
42        self.lookups.rebuild_from(&self.graph);
43        self.bump_layout_facts_revision();
44        self.publish_document_replaced(before);
45    }
46
47    /// Replaces the entire document snapshot in one atomic store update.
48    ///
49    /// This is the full document reset path: graph, view state, editor config, lookups, revision,
50    /// and undo/redo history are updated together, then one `DocumentReplaced` event is emitted.
51    pub fn replace_document(
52        &mut self,
53        graph: Graph,
54        mut view_state: NodeGraphViewState,
55        editor_config: NodeGraphEditorConfig,
56    ) {
57        let before = self.capture_document_snapshot();
58
59        view_state.sanitize_for_graph(&graph);
60        self.graph = graph;
61        self.bump_graph_revision();
62        self.view_state = view_state;
63        self.interaction = editor_config.interaction;
64        self.runtime_tuning = editor_config.runtime_tuning;
65        self.history = GraphHistory::default();
66        self.lookups.rebuild_from(&self.graph);
67        self.bump_layout_facts_revision();
68        self.publish_document_replaced(before);
69    }
70}