-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphCalculatable.swift
More file actions
92 lines (69 loc) · 2.96 KB
/
GraphCalculatable.swift
File metadata and controls
92 lines (69 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// GraphCalculatable.swift
//
//
// Created by Elliot Boschwitz on 6/8/24.
//
import Foundation
public protocol GraphCalculatable<Node>: AnyObject {
associatedtype Node: NodeCalculatable
typealias TopologicalData = GraphTopologicalData<Node>
var topologicalData: TopologicalData { get }
@MainActor var nodes: [Node.ID: Node] { get set }
// Which ports, after a graph runs, needs to be updated
@MainActor var portsToUpdate: Set<NodePortType<Node>> { get set }
// Should we update the preview layers after this graph run?
@MainActor var shouldResortPreviewLayers: Bool { get set }
@MainActor func getNode(id: Node.ID) -> Node?
@MainActor func getNodesToAlwaysRun() -> Set<Node.ID>
@MainActor func getAnimationNodes() -> Set<Node.ID>
@MainActor func evaluateComponentOutputs()
@MainActor func didPortsUpdate(ports: Set<NodePortType<Node>>)
@MainActor var currentGraphTime: TimeInterval { get }
}
extension GraphCalculatable {
@MainActor
func createConnections() -> Self.TopologicalData.Connections {
let inputsObservers = self.nodes.values
.flatMap { $0.inputsObservers }
return inputsObservers.reduce(into: .init()) { result, inputObserver in
if let upstreamConnection = inputObserver.upstreamOutputCoordinate {
guard var connectedInputs = result[upstreamConnection] else {
result.updateValue([inputObserver.id], forKey: upstreamConnection)
return
}
connectedInputs.insert(inputObserver.id)
result.updateValue(connectedInputs, forKey: upstreamConnection)
}
}
}
@MainActor public var connections: Self.TopologicalData.Connections {
self.topologicalData.connections
}
@MainActor public var shallowDownstreamNodes: Self.TopologicalData.ShallowDownstreamNodesDict {
self.topologicalData.shallowDownstreamNodes
}
@MainActor func setConnections(_ newConnections: Self.TopologicalData.Connections) {
self.topologicalData.connections = newConnections
}
@MainActor var nodesForNextGraphStep: Set<Node.ID> {
get {
self.topologicalData.nodesForNextGraphStep
}
set(newValue) {
self.topologicalData.nodesForNextGraphStep = newValue
}
}
@MainActor public var nodesToRunOnGraphStep: Set<Node.ID> {
self.topologicalData.nodesToRunOnGraphStep
}
@MainActor public var hasUnprocessedCycleNodes: Bool {
!self.topologicalData.cycleNodesForNextGraphStep.isEmpty
}
@MainActor public func setNodesForNextGraphStep(_ nodeIds: Set<Node.ID>) {
self.topologicalData.nodesForNextGraphStep = nodeIds
}
@MainActor public func immediatelyUpstreamNodes(for node: Node.ID) -> Set<Node.ID> {
self.topologicalData.immediatelyUpstreamNodes(for: node)
}
}