Skip to content
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

Observer.optimized #952

Merged
merged 9 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions flutter_mobx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
## 2.2.0
- `Observer` is updated with the new `Observer.withChild` constructor, so you can exclude child branch from the re-rendering. - [@subzero911](https://github.com/subzero911) \
In case if you use `Builder.withChild`, you should provide two parameters: `builderWithChild` and `child`:
```dart
Observer.withChild(
builderWithChild: (context, child) => FooWidget(foo: foo, child: child),
child: BarWidget(), // is not rebuilt
),
```

## 2.1.1

- refactor: export `MultiReactionBuilder` from `flutter_mobx.dart` by [@amondnet](https://github.com/amondnet)
Expand Down
53 changes: 37 additions & 16 deletions flutter_mobx/lib/src/observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,59 @@ bool debugAddStackTraceInObserverName = true;
/// See also:
///
/// - [Builder], which is the same thing but for [StatelessWidget] instead.
class Observer extends StatelessObserverWidget
// Implements Builder to import the documentation of `builder`
implements
// ignore: avoid_implementing_value_types
Builder {
class Observer extends StatelessObserverWidget {
// ignore: prefer_const_constructors_in_immutables
Observer({
Key? key,
required this.builder,
String? name,
bool? warnWhenNoObservables,
}) : debugConstructingStackFrame = debugFindConstructingStackFrame(),
builderWithChild = null,
child = null,
assert(builder != null),
super(
key: key,
name: name,
warnWhenNoObservables: warnWhenNoObservables,
);

@override
final WidgetBuilder builder;
/// Observer which excludes the child branch
// ignore: prefer_const_constructors_in_immutables
Observer.withChild({
Key? key,
required this.builderWithChild,
required this.child,
String? name,
bool? warnWhenNoObservables,
}) : debugConstructingStackFrame = debugFindConstructingStackFrame(),
builder = null,
assert(builderWithChild != null && child != null),
super(
key: key,
name: name,
warnWhenNoObservables: warnWhenNoObservables,
);

final WidgetBuilder? builder;

final TransitionBuilder? builderWithChild;

/// The child widget to pass to the [builderWithChild].
final Widget? child;

/// The stack frame pointing to the source that constructed this instance.
final String? debugConstructingStackFrame;

@override
String getName() =>
super.getName() +
(debugConstructingStackFrame != null
? '\n$debugConstructingStackFrame'
: '');
super.getName() +
(debugConstructingStackFrame != null
? '\n$debugConstructingStackFrame'
: '');

@override
Widget build(BuildContext context) => builder(context);
Widget build(BuildContext context) => builderWithChild?.call(context, child) ?? builder!.call(context);

/// Matches constructor stack frames, in both VM and web environments.
static final _constructorStackFramePattern = RegExp(r'\bnew\b');
Expand Down Expand Up @@ -77,14 +97,15 @@ class Observer extends StatelessObserverWidget
.skip(3)
// Search for the first non-constructor frame
.firstWhere(
(frame) => !_constructorStackFramePattern.hasMatch(frame),
(frame) =>
!_constructorStackFramePattern.hasMatch(frame),
orElse: () => '');

final stackFrameCore =
_stackFrameCleanUpPattern.firstMatch(rawStackFrame)?.group(1);
_stackFrameCleanUpPattern.firstMatch(rawStackFrame)?.group(1);
final cleanedStackFrame = stackFrameCore == null
? null
: 'Observer constructed from: $stackFrameCore';
? null
: 'Observer constructed from: $stackFrameCore';

stackFrame = cleanedStackFrame;
}
Expand Down
2 changes: 1 addition & 1 deletion flutter_mobx/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.1.1';
const version = '2.2.0';
2 changes: 1 addition & 1 deletion flutter_mobx/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_mobx
description:
Flutter integration for MobX. It provides a set of Observer widgets that automatically rebuild
when the tracked observables change.
version: 2.1.1
version: 2.2.0

homepage: https://github.com/mobxjs/mobx.dart
issue_tracker: https://github.com/mobxjs/mobx.dart/issues
Expand Down
41 changes: 41 additions & 0 deletions flutter_mobx/test/flutter_mobx_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,47 @@ void main() {
expect(renderCount, equals(1));
});

testWidgets("Observer.withChild's child doesn't re-render", (tester) async {
final message = Observable('Click');
final key1 = UniqueKey();
final key2 = UniqueKey();
final key3 = UniqueKey();

await tester.pumpWidget(
MaterialApp(
home: Observer.withChild(
builderWithChild: (context, child) {
return Column(
children: [
ElevatedButton(onPressed: () => message.value = 'Clicked', child: Container()),
Text(message.value, key: key1),
child!,
Builder(
builder: (context) {
return Text(message.value, key: key3);
}
),
],
);
},
child: Text(message.value, key: key2),
),
),
);

expect(tester.widget<Text>(find.byKey(key1)).data, equals('Click'));
expect(tester.widget<Text>(find.byKey(key2)).data, equals('Click'));
expect(tester.widget<Text>(find.byKey(key3)).data, equals('Click'));

await tester.tap(find.byType(ElevatedButton));
expect(message.value, equals('Clicked'));

await tester.pump();
expect(tester.widget<Text>(find.byKey(key1)).data, equals('Clicked')); // Observer rebuilt the Text1
expect(tester.widget<Text>(find.byKey(key2)).data, equals('Click')); // child Text2 did not change
expect(tester.widget<Text>(find.byKey(key3)).data, equals('Clicked')); // Builder does not preserve from rebuild
});

testWidgets('Observer build should call reaction.track', (tester) async {
final mock = MockReaction();
when(() => mock.hasObservables).thenReturn(true);
Expand Down