Skip to content

Commit 8e8696d

Browse files
authored
fix: clear incoming edges in propagate and evaluate methods (#9)
1 parent 79ed3af commit 8e8696d

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

pearls/2025-08-27-mini-adapton/index.mbt.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,8 @@ fn propagate(self: Node) -> Unit {
159159
dependency.propagate()
160160
// If a dependency's value changed, the node needs to be recomputed
161161
if dependency.is_changed() {
162-
// remove all outgoing_edges, since they will be reconstructed during evaluate
162+
// remove all incoming_edges and outgoing_edges, since they will be reconstructed during evaluate
163+
self.incoming_edges().clear()
163164
self.outgoing_edges().clear()
164165
self.evaluate()
165166
return
@@ -288,13 +289,25 @@ impl[A : Eq] Node for Thunk[A] with set_dirty(self, new_dirty) {
288289
}
289290
290291
impl[A : Eq] Node for Thunk[A] with evaluate(self) {
292+
// push self into node_stack top
293+
// now self is active target
291294
node_stack.push(self)
295+
// `self.thunk` might contains `source.get()`,
296+
// such as `s1.get()`, `s2.get()` and `s3.get()`
297+
//
298+
// when call `Thunk::get` or `Cell::get`,
299+
// they will treat `node_stack.last()` as themself's target.
300+
// if source is `Cell`, then it only record `incoming_edges`.
301+
// if source is `Thunk`, then it record `incoming_edges` and `outgoing_edges`, connect each other.
302+
//
292303
let value = (self.thunk)()
293304
self.is_changed = match self.value {
294305
None => true
295306
Some(v) => v != value
296307
}
297308
self.value = Some(value)
309+
// pop self from node_stack
310+
// now self is no longer active target
298311
node_stack.unsafe_pop() |> ignore
299312
}
300313
```
@@ -327,6 +340,7 @@ fn &Node::propagate(self : &Node) -> Unit {
327340
for dependency in self.outgoing_edges() {
328341
dependency.propagate()
329342
if dependency.is_changed() {
343+
self.incoming_edges().clear()
330344
self.outgoing_edges().clear()
331345
self.evaluate()
332346
return

0 commit comments

Comments
 (0)