Following this question, the solution to resolve data binding is to use DFS and Topological Sorting. I am not really sure exactly what that means. Here is an animation that roughly demonstrates what I am considering.
Say we have a data binding model like this:
●
↓
● → ● → ● → ● ● ← ●
↑ ↑ ↓
● → ● → ● ← ● → ●
↓ ↓ ↑
● ● → ● ← ●
↓ ↓
● ●
↑
●
And then we change one field.
●
↓
● → ● → ● → ● ● ← ●
↑ ↑ ↓
◌ → ● → ● ← ● → ●
↓ ↓ ↑
● ● → ● ← ●
↓ ↓
● ●
↑
●
Then it propagates:
●
↓
● → ● → ◌ → ● ● ← ●
↑ ↑ ↓
○ → ◌ → ● ← ● → ●
↓ ↓ ↑
◌ ● → ● ← ●
↓ ↓
● ●
↑
●
More...
●
↓
● → ● → ○ → ◌ ● ← ●
↑ ↑ ↓
○ → ○ → ◌ ← ● → ●
↓ ↓ ↑
○ ● → ● ← ●
↓ ↓
● ●
↑
●
...
●
↓
● → ● → ○ → ○ ◌ ← ●
↑ ↑ ↓
○ → ○ → ○ ← ● → ●
↓ ↓ ↑
○ ◌ → ● ← ●
↓ ↓
● ●
↑
●
●
↓
● → ● → ○ → ○ ○ ← ●
↑ ↑ ↓
○ → ○ → ○ ← ● → ●
↓ ↓ ↑
○ ○ → ◌ ← ●
↓ ↓
◌ ●
↑
●
●
↓
● → ● → ○ → ○ ○ ← ●
↑ ↑ ↓
○ → ○ → ○ ← ◌ → ●
↓ ↓ ↑
○ ○ → ○ ← ●
↓ ↓
○ ◌
↑
●
●
↓
● → ● → ○ → ○ ○ ← ●
↑ ↑ ↓
○ → ○ → ○ ← ☀ → ◌
↓ ↓ ↑
○ ○ → ○ ← ●
↓ ↓
○ ○
↑
●
Sometimes it encounters a recent update, as shown with ☀. But it keeps going:
●
↓
● → ● → ○ → ○ ○ ← ●
↑ ↑ ↓
○ → ○ → ○ ← ○ → ○
↓ ↓ ↑
○ ○ → ○ ← ●
↓ ↓
○ ○
↑
●
Now it has had no more changes, so it goes back into the unchanged state.
●
↓
● → ● → ● → ● ● ← ●
↑ ↑ ↓
● → ● → ● ← ● → ●
↓ ↓ ↑
● ● → ● ← ●
↓ ↓
● ●
↑
●
So in this process, there are a few things:
- All the nodes go into a "changed" state, until all nodes have been changed.
- Somehow it figures out that all nodes that will be changed have been changed. This allows it to go back into the "unchanged" state.
This graph is pretty simplified because in reality there could be many complicated cycles and many more bindings per node.
However, given that the arrows indicate the direction in this Directed Cyclic Graph, I am wondering how this algorithm can efficiently perform this update propagation.
The naïve approach would iterate through all of the nodes that have changed each step, to see if there is anything left to change. Maybe there is a third state, "changed and no more propagation". Once everything is in that third state (by checking every node every step), then it would be done. Wondering if there is an efficient way to accomplish this, so it doesn't need to iterate through every node each step somehow.
Also, it is possible that multiple values are changed at once, so it should be able to handle that as well, i.e.:
●
↓
● → ● → ● → ● ● ← ◌
↑ ↑ ↓
◌ → ● → ● ← ● → ●
↓ ↓ ↑
● ● → ● ← ●
↓ ↓
● ◌
↑
●