Skip to content

Commit

Permalink
feat: support late observables (#973)
Browse files Browse the repository at this point in the history
Co-authored-by: Pavan Podila <[email protected]>
  • Loading branch information
amondnet and pavanpodila authored Dec 27, 2023
1 parent 60f07a0 commit 00fe8b0
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 10 deletions.
4 changes: 4 additions & 0 deletions mobx_codegen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.5.0

- Support `late` observables by [@amondnet](https://github.com/amondnet). fix [#919](https://github.com/mobxjs/mobx.dart/issues/919)

## 2.4.1

- Adds `useDeepEquality` for creating observables by [@amondnet](https://github.com/amondnet)
Expand Down
1 change: 1 addition & 0 deletions mobx_codegen/lib/src/store_class_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class StoreClassVisitor extends SimpleElementVisitor {
name: element.name,
isPrivate: element.isPrivate,
isReadOnly: _isObservableReadOnly(element),
isLate: element.isLate,
equals: _getEquals(element),
);

Expand Down
30 changes: 25 additions & 5 deletions mobx_codegen/lib/src/template/observable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class ObservableTemplate {
required this.name,
this.isReadOnly = false,
this.isPrivate = false,
this.isLate = false,
this.equals,
this.useDeepEquality,
});
Expand All @@ -21,6 +22,7 @@ class ObservableTemplate {
final String name;
final bool isPrivate;
final bool isReadOnly;
final bool isLate;
final ExecutableElement? equals;
final bool? useDeepEquality;

Expand Down Expand Up @@ -53,16 +55,34 @@ class ObservableTemplate {
}''';
}

String _buildSetters() {
if (isLate) {
return '''
bool _${name}IsInitialized = false;
@override
String toString() => """
late final $atomName = Atom(name: '${storeTemplate.parentTypeName}.$name', context: context);
${_buildGetters()}
set $name($type value) {
$atomName.reportWrite(value, _${name}IsInitialized ? super.$name : null, () {
super.$name = value;
_${name}IsInitialized = true;
}${equals != null ? ', equals: ${equals!.name}' : ''});
}''';
}

return '''
@override
set $name($type value) {
$atomName.reportWrite(value, super.$name, () {
super.$name = value;
}${equals != null ? ', equals: ${equals!.name}' : ''}${useDeepEquality != null ? ', useDeepEquality: $useDeepEquality' : ''});
}""";
}''';
}

@override
String toString() => """
late final $atomName = Atom(name: '${storeTemplate.parentTypeName}.$name', context: context);
${_buildGetters()}
${_buildSetters()}""";
}
2 changes: 1 addition & 1 deletion mobx_codegen/lib/version.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated via set_version.dart. !!!DO NOT MODIFY BY HAND!!!

/// The current version as per `pubspec.yaml`.
const version = '2.4.1';
const version = '2.5.0';
2 changes: 1 addition & 1 deletion mobx_codegen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: mobx_codegen
description: Code generator for MobX that adds support for annotating your code with @observable, @computed, @action and also creating Store classes.
version: 2.4.1
version: 2.5.0

homepage: https://github.com/mobxjs/mobx.dart
issue_tracker: https://github.com/mobxjs/mobx.dart/issues
Expand Down
6 changes: 5 additions & 1 deletion mobx_codegen/test/data/valid_late_variables_output.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ mixin _$TestStore on _TestStore, Store {
return super.username;
}

bool _usernameIsInitialized = false;

@override
set username(String value) {
_$usernameAtom.reportWrite(value, super.username, () {
_$usernameAtom
.reportWrite(value, _usernameIsInitialized ? super.username : null, () {
super.username = value;
_usernameIsInitialized = true;
});
}
}
15 changes: 15 additions & 0 deletions mobx_codegen/test/generator_usage_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ abstract class _TestStore with Store {
// ignore: only_throw_errors
throw 'TEST ERROR';
}

@observable
late String lateField;
}

void main() {
Expand Down Expand Up @@ -279,4 +282,16 @@ void main() {

expect(values, equals(['', 'field1', 'field2']));
});

test('setting late fields with action works', () {
final store = TestStore('field1', field2: 'field2');

final fields = <String>[];
autorun((_) {
fields.add(store.lateField);
});
store.lateField = 'field';

expect(fields, equals(['field']));
});
}
21 changes: 21 additions & 0 deletions mobx_codegen/test/generator_usage_test.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion mobx_codegen/test/nested_store.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion mobx_codegen/test/store_with_custom_context.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 00fe8b0

Please sign in to comment.