Skip to content

Commit a2c3041

Browse files
committed
Support nested transactions.
Support node shifting.
1 parent 8e1f42f commit a2c3041

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

include/react/detail/graph_impl.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ class ReactGraph
180180

181181
LinkCache linkCache_;
182182

183-
bool isTransactionActive_ = false;
183+
int transactionLevel_ = 0;
184184
bool allowLinkedTransactionMerging_ = false;
185185
};
186186

@@ -195,17 +195,17 @@ void ReactGraph::PushInput(NodeId nodeId, F&& inputCallback)
195195

196196
changedInputs_.push_back(nodeId);
197197

198-
if (!isTransactionActive_)
198+
if (transactionLevel_ == 0)
199199
Propagate();
200200
}
201201

202202
template <typename F>
203203
void ReactGraph::DoTransaction(F&& transactionCallback)
204204
{
205205
// Transaction callback may add multiple inputs.
206-
isTransactionActive_ = true;
206+
++transactionLevel_;
207207
std::forward<F>(transactionCallback)();
208-
isTransactionActive_ = false;
208+
--transactionLevel_;
209209

210210
Propagate();
211211
}

src/detail/graph_impl.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ void ReactGraph::Propagate()
7272
auto& node = nodeData_[nodeId];
7373
auto* nodePtr = node.nodePtr;
7474

75-
if (nodePtr->Update(0u) == UpdateResult::changed)
75+
UpdateResult res = nodePtr->Update(0u);
76+
77+
if (res == UpdateResult::changed)
7678
{
7779
changedNodes_.push_back(nodePtr);
7880
ScheduleSuccessors(node);
@@ -87,10 +89,12 @@ void ReactGraph::Propagate()
8789
auto& node = nodeData_[nodeId];
8890
auto* nodePtr = node.nodePtr;
8991

92+
// A predecessor of this node has shifted to a lower level?
9093
if (node.level < node.newLevel)
9194
{
92-
// Re-schedule this node
95+
// Re-schedule this node.
9396
node.level = node.newLevel;
97+
9498
RecalculateSuccessorLevels(node);
9599
scheduledNodes_.Push(nodeId, node.level);
96100
continue;
@@ -103,7 +107,18 @@ void ReactGraph::Propagate()
103107
continue;
104108
}
105109

106-
if (nodePtr->Update(0u) == UpdateResult::changed)
110+
UpdateResult res = nodePtr->Update(0u);
111+
112+
// Topology changed?
113+
if (res == UpdateResult::shifted)
114+
{
115+
// Re-schedule this node.
116+
RecalculateSuccessorLevels(node);
117+
scheduledNodes_.Push(nodeId, node.level);
118+
continue;
119+
}
120+
121+
if (res == UpdateResult::changed)
107122
{
108123
changedNodes_.push_back(nodePtr);
109124
ScheduleSuccessors(node);

0 commit comments

Comments
 (0)