| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:async'; | |
| 6 | |
| 7 import '../animation/animated_value.dart'; | |
| 8 import '../animation/animation_performance.dart'; | 5 import '../animation/animation_performance.dart'; |
| 9 import 'basic.dart'; | 6 import 'basic.dart'; |
| 10 | 7 |
| 11 class _AnimationEntry { | |
| 12 _AnimationEntry(this.value); | |
| 13 final AnimatedValue value; | |
| 14 StreamSubscription<double> subscription; | |
| 15 } | |
| 16 | |
| 17 abstract class AnimatedComponent extends StatefulComponent { | 8 abstract class AnimatedComponent extends StatefulComponent { |
| 18 | 9 |
| 19 AnimatedComponent({ String key }) : super(key: key); | 10 AnimatedComponent({ String key }) : super(key: key); |
| 20 | 11 |
| 21 void syncFields(AnimatedComponent source) { } | 12 void syncFields(AnimatedComponent source) { } |
| 22 | 13 |
| 23 final List<_AnimationEntry> _animatedFields = new List<_AnimationEntry>(); | |
| 24 final List<AnimationPerformance> _watchedPerformances = new List<AnimationPerf
ormance>(); | 14 final List<AnimationPerformance> _watchedPerformances = new List<AnimationPerf
ormance>(); |
| 25 | 15 |
| 26 void watch(AnimatedValue value) { | |
| 27 // TODO(ianh): we really should assert that we're not doing this | |
| 28 // in the constructor since doing it there is pointless and | |
| 29 // expensive, since we'll be doing it for every copy of the object | |
| 30 // even though only the first one will use it (since we're | |
| 31 // stateful, the others will all be discarded early). | |
| 32 assert(!mounted); | |
| 33 _animatedFields.add(new _AnimationEntry(value)); | |
| 34 } | |
| 35 | |
| 36 void watchPerformance(AnimationPerformance performance) { | 16 void watchPerformance(AnimationPerformance performance) { |
| 37 assert(!_watchedPerformances.contains(performance)); | 17 assert(!_watchedPerformances.contains(performance)); |
| 38 _watchedPerformances.add(performance); | 18 _watchedPerformances.add(performance); |
| 39 if (mounted) | 19 if (mounted) |
| 40 performance.addListener(scheduleBuild); | 20 performance.addListener(scheduleBuild); |
| 41 } | 21 } |
| 42 | 22 |
| 43 void didMount() { | 23 void didMount() { |
| 44 for (_AnimationEntry entry in _animatedFields) { | |
| 45 entry.subscription = entry.value.onValueChanged.listen((_) { | |
| 46 scheduleBuild(); | |
| 47 }); | |
| 48 } | |
| 49 | |
| 50 for (AnimationPerformance performance in _watchedPerformances) | 24 for (AnimationPerformance performance in _watchedPerformances) |
| 51 performance.addListener(scheduleBuild); | 25 performance.addListener(scheduleBuild); |
| 52 | |
| 53 super.didMount(); | 26 super.didMount(); |
| 54 } | 27 } |
| 55 | 28 |
| 56 void didUnmount() { | 29 void didUnmount() { |
| 57 for (_AnimationEntry entry in _animatedFields) { | |
| 58 assert(entry.subscription != null); | |
| 59 entry.subscription.cancel(); | |
| 60 entry.subscription = null; | |
| 61 } | |
| 62 | |
| 63 for (AnimationPerformance performance in _watchedPerformances) | 30 for (AnimationPerformance performance in _watchedPerformances) |
| 64 performance.removeListener(scheduleBuild); | 31 performance.removeListener(scheduleBuild); |
| 65 | |
| 66 super.didUnmount(); | 32 super.didUnmount(); |
| 67 } | 33 } |
| 68 | 34 |
| 69 } | 35 } |
| OLD | NEW |