Does Riverpod do Topological Sorting? #4006
-
|
So providers can use other providers, and this forms a dag of dependencies. Does Riverpod take care of topologically sorting the resulting graph? As an example, if there are three providers, A, B and C, where B uses A and C uses the other two, and A rebuilds, is it possible that C might rebuild before B, leading to an efficiency where it rebuilds again after B rebuilds? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Technically, the fact that providers can use other providers doesn't form a dag on its own. The graph could be cyclic. This enables Riverpod to guaranteee that providers will update in the optimal order. There's a bunch of tests for it Given: The only legal orders are: We can't have a chain of rebuild like Riverpod doesn't maintain a "sorted graph" though (because the graph's shape tends to change often). |
Beta Was this translation helpful? Give feedback.
Technically, the fact that providers can use other providers doesn't form a dag on its own. The graph could be cyclic.
Riverpod goes out of its way to make it an error if you make a cycle though, to ensure we do have a unidirected graph.
This enables Riverpod to guaranteee that providers will update in the optimal order. There's a bunch of tests for it
Your scenario is covered here
riverpod/packages/riverpod/test/framework/visit_states_test.dart
Line 41 in 8e1098c
Given:
The only legal orders are:
We can't have a chain of rebuild like
a > b > d + c > d, anddprovider will …