-
-
Notifications
You must be signed in to change notification settings - Fork 310
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
Map Observables not working in v2.1.4 #903
Comments
hmm could you please provide a minimal reproducing sample? |
@fzyzcjy here is the repo, you can try running the same https://github.com/Gaurav-CareMonitor/mobx_test |
@Gaurav-CareMonitor untitled.webm |
I am currently working on a pretty large codebase and it'll be very hard to migrate all of the map or list variables to observable. Is there any workaround with which I can keep using same functionality with updated version? |
@fzyzcjy ? |
I'm surprised that using Map was working for you before v2.1.4. |
It was and it works now as well after downgrading to the previous version |
Hi @DanMossa, It will work in previous version and not in the latest version as new version has object matching condition before it reports and it will return if it matches with the old value. because of this @Gaurav-CareMonitor 's code will not work in latest version as he is assigning the value of same variable to it self. |
I recommend updating to 2.1.4 and changing the @observable
Map @observable
ObservableMap Conversely, in 150k lines of code, changing an observable map to a map took about a minute. |
@Gaurav-CareMonitor |
Just rollback to previous version of mobx in pubscpec.yaml, remove ^ from version like that: mobx: 2.1.1. |
@Gaurav-CareMonitor did you experience the same problem with Objects, not Map? |
I believe that many codebases will start to break because MobX worked differently before: it would react to a new value assigned to an observable, even if the new value was equal to the previous value. |
@ibrahimdevs @sezabass https://github.com/mobxjs/mobx.dart#observables The
Tested on mobx 2.1.3. import 'package:mobx/mobx.dart';
void main() {
test('should not notify', () {
final object = TestClass();
final Observable<TestClass> observable = Observable(object);
object.value = 1;
bool executed = false;
final dispose = reaction((_) => observable.value, (v) {
executed = true;
});
runInAction(() => observable.value = object);
expect(executed, false);
dispose();
});
test('should notify', () {
final object1 = TestClass();
final object2 = TestClass();
final Observable<TestClass> observable = Observable(object1);
bool executed = false;
final dispose = reaction(
(_) => observable.value,
(v) {
executed = true;
},
);
runInAction(() => observable.value = object2);
expect(executed, true);
dispose();
});
}
class TestClass {
int value = 0;
} |
@amondnet I didn't understand "Yeah I didn't notice that many people misuse @observable" |
There are people who were exploiting this bug. For example, mobx does not react when a list item changes without changing the length of the list. E.g. |
Can you give me a code example, that hasn't changed from 2.1.4. The The test below succeeds in 2.1.4: import 'package:mobx/mobx.dart';
import 'package:test/test.dart';
void main() {
test('observable list test', () {
final object1 = TestClass(0);
final ObservableList<TestClass> observable = ObservableList.of([object1]);
bool executed = false;
final dispose = reaction(
(_) => observable.first,
(v) {
executed = true;
},
);
runInAction(() => observable[0] = TestClass(2));
expect(executed, true);
dispose();
});
test('observable list test2', () {
final ObservableList<String> observable = ObservableList.of(['a']);
bool executed = false;
final dispose = reaction(
(_) => observable.first,
(v) {
executed = true;
},
);
runInAction(() => observable[0] = 'ab');
expect(executed, true);
dispose();
});
}
class TestClass {
int value = 0;
TestClass(this.value);
} |
What if we did something like this? class TestClass {
@alwaysNotify
MyData? value;
@observable
int value2 = 0;
@MakeObservable(equals: 'oldValue != newValue')
int value3 = 0;
} |
The mobx reacts when the value changes ( Can you give me a simple example? Generally, I use one of the following methods.
NestedStore import 'package:mobx/mobx.dart';
import 'package:test/test.dart';
part 'nested_store.g.dart';
class MainStore = _MainStore with _$MainStore;
abstract class _MainStore with Store {
@observable
NestedStore value = NestedStore();
}
class NestedStore = _NestedStore with _$NestedStore;
abstract class _NestedStore with Store {
@observable
String? name;
}
void main() {
test('react property changed', () {
final store = MainStore();
bool executed = false;
reaction((_) => store.value.name, (v) {
executed = true;
});
store.value.name = 'john';
expect(executed, isTrue);
});
} |
@amondnet I put an example below, when I call the refreshVersion() I want to rebuild my Text() widget.
If I want to change this to Nested Store;
If I call refreshVersion() function, will be triger to rebuild Text widget? Is this the correct usage? Thanks for your time and effort. |
@ibrahimdevs The language is different, but you may find the documentation in mobx helpful. |
@amondnet I want to make it clear, in my current usage;
if I call refreshVersion, It won't triger and rebuild the TextWidget. But If I change it to nested Store;
This time, when I call refreshVersion, It will triger and rebuild the TextWidget. Am I correct? |
Yes, that's correct. But the second time, the widget doesn't rebuild, because the value of the observable doesn't change. Prior to 2.1.4, rebuilds were triggered even if the value did not change. If you do something like this @action
void refreshVersion(String newVersion) {
appSettings.version = newVersion;
} refreshVersion('1.0.0'); // version: 1.0.0, newVersion: 1.0.0, not rebuild
refreshVersion('1.0.1'); // version: 1.0.0, newVersion: 1.0.1, rebuild
refreshVersion('1.0.1'); // version: 1.0.1, newVersion: 1.0.1, not rebuild
|
@amondnet thanks for detailed response and it helped me to understand the correct mechanism better. So what do you think about next version? Could you decide how to continue? I mean, will it work like prior to 2.1.4 or we have to change our mechanisms? Thanks, |
@amondnet If someone wants to trigger the build update manually by using
Is there an issue with that? |
@amondnet thanks for resolve this issue. But what is the final decision? Should we change anything to work this package like before? |
@ibrahimdevs The previous behavior is a clear bug and completely against the principles of mobx, there is no benefit at all. However, you can make it behave the same as before by providing a custom Use |
@amondnet [@alwaysNotify] is not working, you can test it in the sample code. |
@Gaurav-CareMonitor This code was created 3 months ago. |
I did, Also deleted the old generated file |
Ah okay, My bad. Got it thanks a lot for the quick reply. |
State of app is not changing after updating version to 2.1.4
The text was updated successfully, but these errors were encountered: