Skip to content

Commit

Permalink
feat: support late observables
Browse files Browse the repository at this point in the history
  • Loading branch information
amondnet committed Dec 19, 2023
1 parent a00d4da commit 4cab8c6
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.0

- Require `analyzer: ^5.12.0`
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
32 changes: 27 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,
});

Expand All @@ -20,6 +21,7 @@ class ObservableTemplate {
final String name;
final bool isPrivate;
final bool isReadOnly;
final bool isLate;
final ExecutableElement? equals;

/// Formats the `name` from `_foo_bar` to `foo_bar`
Expand Down Expand Up @@ -51,16 +53,36 @@ 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}' : ''});
}""";
}''';
}

@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.0';
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.0
version: 2.5.0

homepage: https://github.com/mobxjs/mobx.dart
issue_tracker: https://github.com/mobxjs/mobx.dart/issues
Expand Down
4 changes: 3 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,9 +8,11 @@ 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;
});
}
Expand Down
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 4cab8c6

Please sign in to comment.