| 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'; | 5 import 'dart:async'; |
| 6 | 6 |
| 7 import '../animation/animated_value.dart'; | 7 import '../animation/animated_value.dart'; |
| 8 import 'basic.dart'; | 8 import 'basic.dart'; |
| 9 | 9 |
| 10 typedef void SetterFunction(double value); | |
| 11 | |
| 12 class _AnimationEntry { | 10 class _AnimationEntry { |
| 13 _AnimationEntry(this.value, this.setter); | 11 _AnimationEntry(this.value); |
| 14 final AnimatedValue value; | 12 final AnimatedValue value; |
| 15 final SetterFunction setter; | |
| 16 StreamSubscription<double> subscription; | 13 StreamSubscription<double> subscription; |
| 17 } | 14 } |
| 18 | 15 |
| 19 abstract class AnimatedComponent extends Component { | 16 abstract class AnimatedComponent extends Component { |
| 20 | 17 |
| 21 AnimatedComponent({ String key }) : super(key: key, stateful: true); | 18 AnimatedComponent({ String key }) : super(key: key, stateful: true); |
| 22 | 19 |
| 23 void syncFields(AnimatedComponent source) { } | 20 void syncFields(AnimatedComponent source) { } |
| 24 | 21 |
| 25 List<_AnimationEntry> _animatedFields = new List<_AnimationEntry>(); | 22 List<_AnimationEntry> _animatedFields = new List<_AnimationEntry>(); |
| 26 | 23 |
| 27 animate(AnimatedValue value, SetterFunction setter) { | 24 watch(AnimatedValue value) { |
| 28 assert(!mounted); | 25 assert(!mounted); |
| 29 setter(value.value); | 26 // TODO(ianh): we really should assert that we're not doing this |
| 30 _animatedFields.add(new _AnimationEntry(value, setter)); | 27 // in the constructor since doing it there is pointless and |
| 28 // expensive, since we'll be doing it for every copy of the object |
| 29 // even though only the first one will use it (since we're |
| 30 // stateful, the others will all be discarded early). |
| 31 _animatedFields.add(new _AnimationEntry(value)); |
| 31 } | 32 } |
| 32 | 33 |
| 33 void didMount() { | 34 void didMount() { |
| 34 for (_AnimationEntry entry in _animatedFields) { | 35 for (_AnimationEntry entry in _animatedFields) { |
| 35 entry.subscription = entry.value.onValueChanged.listen((_) { | 36 entry.subscription = entry.value.onValueChanged.listen((_) { |
| 36 entry.setter(entry.value.value); | |
| 37 scheduleBuild(); | 37 scheduleBuild(); |
| 38 }); | 38 }); |
| 39 } | 39 } |
| 40 super.didMount(); | 40 super.didMount(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void didUnmount() { | 43 void didUnmount() { |
| 44 for (_AnimationEntry entry in _animatedFields) { | 44 for (_AnimationEntry entry in _animatedFields) { |
| 45 assert(entry.subscription != null); | 45 assert(entry.subscription != null); |
| 46 entry.subscription.cancel(); | 46 entry.subscription.cancel(); |
| 47 entry.subscription = null; | 47 entry.subscription = null; |
| 48 } | 48 } |
| 49 super.didUnmount(); | 49 super.didUnmount(); |
| 50 } | 50 } |
| 51 | 51 |
| 52 } | 52 } |
| OLD | NEW |