-
-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
async action behaves differently from 2.0.7+3. #834
Comments
This is an interesting topic to discuss. Per-documentation vs non-informed user expectationsIt seems to me that the behavior described in the docs matches non-informed user expectations only in the case of a synchronous method. In the async case, the expectation of everyone I've asked was that the following @action
Future<void> loadStuff() async {
loading = true;
stuff = await fetchStuff();
loading = false;
} would result in the But, according to the docs, these are wrong expectations. Too broad interpretation of doc's notification grouping ruleIt seems like the library implementation interprets the rule about notifications grouping even broader than it's written. Nested actions won't trigger notifications until the outermost action body is fully executedThe way it works in the library - any observable changes within nested action invocations will also not_notify until the outermost action finishes its execution. So, even if we do something like @action
void setLoading(bool value) {
loading = value;
}
@action
Future<void> loadStuff() async {
setLoading(true);
stuff = await fetchStuff();
setLoading(false);
} we still won't get the Even if the referenced action/observable_field belongs to a different state store - it won't get any value change notifications until the outermost action body is fully executed in a different state storeThis too_broad interpretation of the rule gets even worse when we consider that there is no difference in having the nested action call belonging to the same state store and a separate one. So, the behaviors of this class StateStore1 {
@observable
Object? someResult;
@observable
bool busy = false;
@action
Future<void> doStuff() async {
busy = true;
someResult = await doSomething();
busy = false;
}
} and this class BusyStateStore {
@observable
bool busy = false;
}
class StateStore1 {
final BusyStateStore busyStore;
@observable
Object? someResult;
@action
Future<void> doStuff() async {
busyStore.busy = true;
someResult = await doSomething();
busyStore.busy = false;
}
} are the same in terms of observable change notification. None of them will notify the "busy == true" before Specific common problematic case: navigation to a different page within action renders the page unable to receive any mobx notificationsThis is especially problematic in the case of trying to do navigation to a page with a different state store within the async action body: class StateStore {
@observable
Object? someResult;
@action
Future<void> doStuff(NavigatorState navigator) async {
someResult = await navigator.pushNamed('/some_page');
await Future.delayed(Duration(seconds: 2));
}
} If How my team makes sure we don't get into the trouble with async action blocking any notifications?We decided that disallowing the We've introduced a, quite hacky, yet working, alternative in form of Future<T> asyncAction<T>(Future<T> Function() function) => function(); which we use like this: @action
Future<void> loadSomething() => asyncAction(() async {
loading = true;
try {
result = await loadResult();
} finally {
loading = false;
}
}); This ensures that mobx_codegen will generate How to solve the problems described above in the library?In my opinion, the library should perform notification batching for async actions according to the following rules:
|
version 2.0.7+3 also breaks our already working code, in our case we are using |
@abodehBabelli ++ |
Also waiting for fix |
Same issue |
same issue |
Even if this is desired behavior, shall we make a user-configurable flag, such that at least can disable this and revert to old behavior? |
O mesmo problema |
same problem here |
#89
mobx: <= 2.0.7+2
values : [0, 2, 3]
#784
mobx: >= 2.0.7+3
values : [0, 3]
https://github.com/mobxjs/mobx.dart/runs/6912223161?check_suite_focus=true
https://mobx.netlify.app/api/action/
https://mobx.js.org/actions.html#updating-state-using-actions
https://mobx.js.org/actions.html#asynchronous-actions
According to the docs, the2.0.7+3
seems to be working correctly.edited:
According to the docs, the
2.0.7+2
seems to be working correctly.The text was updated successfully, but these errors were encountered: