| 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 '../animation/animated_value.dart'; | 5 import '../animation/animated_value.dart'; |
| 6 import '../fn2.dart'; | 6 import '../fn2.dart'; |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 typedef void SetterFunction(double value); | 9 typedef void SetterFunction(double value); |
| 10 | 10 |
| 11 class _AnimationEntry { |
| 12 _AnimationEntry(this.value, this.setter); |
| 13 final AnimatedValue value; |
| 14 final SetterFunction setter; |
| 15 StreamSubscription<double> subscription; |
| 16 } |
| 17 |
| 11 abstract class AnimatedComponent extends Component { | 18 abstract class AnimatedComponent extends Component { |
| 12 | 19 |
| 13 AnimatedComponent({ Object key }) : super(key: key, stateful: true); | 20 AnimatedComponent({ Object key }) : super(key: key, stateful: true); |
| 14 | 21 |
| 15 void syncFields(AnimatedComponent source) { } | 22 void syncFields(AnimatedComponent source) { } |
| 16 | 23 |
| 24 List<_AnimationEntry> _animatedFields = new List<_AnimationEntry>(); |
| 25 |
| 17 animate(AnimatedValue value, SetterFunction setter) { | 26 animate(AnimatedValue value, SetterFunction setter) { |
| 27 assert(!mounted); |
| 18 setter(value.value); | 28 setter(value.value); |
| 19 StreamSubscription<double> subscription; | 29 _animatedFields.add(new _AnimationEntry(value, setter)); |
| 20 onDidMount(() { | 30 } |
| 21 subscription = value.onValueChanged.listen((_) { | 31 |
| 22 setter(value.value); | 32 void didMount() { |
| 33 for (_AnimationEntry entry in _animatedFields) { |
| 34 entry.subscription = entry.value.onValueChanged.listen((_) { |
| 35 entry.setter(entry.value.value); |
| 23 scheduleBuild(); | 36 scheduleBuild(); |
| 24 }); | 37 }); |
| 25 }); | 38 } |
| 26 onDidUnmount(() { | 39 super.didMount(); |
| 27 if (subscription != null) { | 40 } |
| 28 subscription.cancel(); | 41 |
| 29 subscription = null; | 42 void didUnmount() { |
| 30 } | 43 for (_AnimationEntry entry in _animatedFields) { |
| 31 }); | 44 assert(entry.subscription != null); |
| 45 entry.subscription.cancel(); |
| 46 entry.subscription = null; |
| 47 } |
| 48 super.didUnmount(); |
| 32 } | 49 } |
| 33 | 50 |
| 34 } | 51 } |
| OLD | NEW |