From b361a3b4a8efdec33d717fb2a931be2ec47bd78c Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 21 Sep 2015 14:00:26 -0700 Subject: [PATCH] fn3 shouldn't rebuild components that don't change identity --- packages/flutter/lib/src/fn3/framework.dart | 3 ++ .../test/fn3/stateful_component_test.dart | 36 ++++++++++++++++--- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/packages/flutter/lib/src/fn3/framework.dart b/packages/flutter/lib/src/fn3/framework.dart index afa1d9b4199..ae109b96923 100644 --- a/packages/flutter/lib/src/fn3/framework.dart +++ b/packages/flutter/lib/src/fn3/framework.dart @@ -204,6 +204,9 @@ abstract class Element { } if (child != null) { + assert(child._slot == slot); + if (child._widget == updated) + return child; if (_canUpdate(child._widget, updated)) { child.update(updated); return child; diff --git a/packages/unit/test/fn3/stateful_component_test.dart b/packages/unit/test/fn3/stateful_component_test.dart index 3b65e4d3769..2a50624f2d4 100644 --- a/packages/unit/test/fn3/stateful_component_test.dart +++ b/packages/unit/test/fn3/stateful_component_test.dart @@ -32,6 +32,21 @@ class TestComponentState extends ComponentState { final BoxDecoration kBoxDecorationA = new BoxDecoration(); final BoxDecoration kBoxDecorationB = new BoxDecoration(); +class TestBuildCounter extends Component { + static int buildCount = 0; + + Widget build() { + ++buildCount; + return new DecoratedBox(decoration: kBoxDecorationA); + } +} + +void flipStatefulComponent(WidgetTester tester) { + ComponentStateElement stateElement = + tester.findElement((element) => element is ComponentStateElement); + (stateElement.state as TestComponentState).flip(); +} + void main() { test('Stateful component smoke test', () { WidgetTester tester = new WidgetTester(); @@ -63,10 +78,7 @@ void main() { checkTree(kBoxDecorationB); - ComponentStateElement stateElement = - tester.findElement((element) => element is ComponentStateElement); - (stateElement.state as TestComponentState).flip(); - + flipStatefulComponent(tester); Element.flushBuild(); checkTree(kBoxDecorationA); @@ -82,4 +94,20 @@ void main() { }); + test('Don\'t rebuild subcomponents', () { + WidgetTester tester = new WidgetTester(); + tester.pumpFrame( + new TestComponentConfig( + left: new TestBuildCounter(), + right: new DecoratedBox(decoration: kBoxDecorationB) + ) + ); + + expect(TestBuildCounter.buildCount, equals(1)); + + flipStatefulComponent(tester); + Element.flushBuild(); + + expect(TestBuildCounter.buildCount, equals(1)); + }); }