jellyflow_runtime/runtime/store/
events.rs1use crate::io::{NodeGraphEditorConfig, NodeGraphViewState};
4use crate::runtime::commit::NodeGraphPatch;
5use crate::runtime::events::{
6 NodeGraphDocumentSnapshot, NodeGraphGestureEvent, NodeGraphStoreEvent, ViewChange,
7};
8use jellyflow_core::core::Graph;
9
10use super::NodeGraphStore;
11
12pub(super) struct DocumentSnapshotParts {
13 graph: Graph,
14 graph_revision: u64,
15 view_state: NodeGraphViewState,
16 editor_config: NodeGraphEditorConfig,
17}
18
19impl DocumentSnapshotParts {
20 fn from_store(store: &NodeGraphStore) -> Self {
21 Self {
22 graph: store.graph.clone(),
23 graph_revision: store.graph_revision,
24 view_state: store.view_state.clone(),
25 editor_config: store.editor_config(),
26 }
27 }
28
29 fn event_snapshot(&self) -> NodeGraphDocumentSnapshot<'_> {
30 NodeGraphDocumentSnapshot {
31 graph: &self.graph,
32 graph_revision: self.graph_revision,
33 view_state: &self.view_state,
34 editor_config: &self.editor_config,
35 }
36 }
37}
38
39impl NodeGraphStore {
40 pub(super) fn bump_graph_revision(&mut self) {
41 self.graph_revision = self.graph_revision.saturating_add(1);
42 }
43
44 pub(super) fn bump_layout_facts_revision(&mut self) {
45 self.layout_facts_revision = self.layout_facts_revision.saturating_add(1);
46 }
47
48 pub(crate) fn publish_layout_facts_changed(&mut self) {
49 self.bump_layout_facts_revision();
50 self.notify_selectors();
51 }
52
53 pub(super) fn capture_document_snapshot(&self) -> DocumentSnapshotParts {
54 DocumentSnapshotParts::from_store(self)
55 }
56
57 pub(super) fn publish_document_replaced(&mut self, before: DocumentSnapshotParts) {
58 let after = self.capture_document_snapshot();
59
60 self.emit(NodeGraphStoreEvent::DocumentReplaced {
61 before: before.event_snapshot(),
62 after: after.event_snapshot(),
63 });
64 self.notify_selectors();
65 }
66
67 pub(super) fn publish_graph_commit(&mut self, patch: &NodeGraphPatch) {
68 self.emit(NodeGraphStoreEvent::GraphCommitted { patch });
69 self.notify_selectors();
70 }
71
72 pub(super) fn publish_view_changed(
73 &mut self,
74 before: &NodeGraphViewState,
75 after: &NodeGraphViewState,
76 changes: &[ViewChange],
77 ) {
78 if !changes.is_empty() {
79 self.emit(NodeGraphStoreEvent::ViewChanged {
80 before,
81 after,
82 changes,
83 });
84 }
85 self.notify_selectors();
86 }
87
88 pub(super) fn emit(&mut self, event: NodeGraphStoreEvent<'_>) {
89 self.subscriptions.emit_event(event);
90 }
91
92 pub fn emit_gesture(&mut self, event: NodeGraphGestureEvent) {
94 self.subscriptions.emit_gesture(event);
95 }
96}